View Javadoc
1   package org.apache.commons.jcs3.auxiliary.disk.indexed;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Map;
23  
24  import junit.framework.TestCase;
25  
26  /**
27   * This ensures that the jcs version of the LRU map is as fast as the commons
28   * version. It has been testing at .6 to .7 times the commons LRU.
29   */
30  public class LRUMapSizeVsCount
31      extends TestCase
32  {
33      /** The put ration after the test */
34      double ratioPut;
35  
36      /** The ratio after the test */
37      double ratioGet;
38  
39      /** put size / count  ratio */
40      float targetPut = 1.2f;
41  
42      /** get size / count ratio */
43      float targetGet = 1.2f;
44  
45      /** Time to loop */
46      int loops = 20;
47  
48      /** items to put and get per loop */
49      int tries = 100000;
50  
51      /**
52       * @param testName
53       */
54      public LRUMapSizeVsCount( final String testName )
55      {
56          super( testName );
57      }
58  
59      /**
60       * A unit test for JUnit
61       *
62       * @throws Exception
63       *                Description of the Exception
64       */
65      public void testSimpleLoad()
66          throws Exception
67      {
68          doWork();
69          assertTrue( this.ratioPut < targetPut );
70          assertTrue( this.ratioGet < targetGet );
71      }
72  
73      /**
74       *
75       */
76      public void doWork()
77      {
78          long start = 0;
79          long end = 0;
80          long time = 0;
81          float tPer = 0;
82  
83          long putTotalCount = 0;
84          long getTotalCount = 0;
85          long putTotalSize = 0;
86          long getTotalSize = 0;
87  
88          long minTimeSizePut = Long.MAX_VALUE;
89          long minTimeSizeGet = Long.MAX_VALUE;
90          long minTimeCountPut = Long.MAX_VALUE;
91          long minTimeCountGet = Long.MAX_VALUE;
92  
93          String cacheName = "LRUMap";
94          String cache2Name = "";
95  
96          try
97          {
98          	final IndexedDiskCacheAttributes cattr = new IndexedDiskCacheAttributes();
99          	cattr.setName("junit");
100         	cattr.setCacheName("junit");
101         	cattr.setDiskPath(".");
102         	final IndexedDiskCache<String, String> idc = new IndexedDiskCache<>(cattr);
103 
104 			final Map<String, IndexedDiskElementDescriptor> cacheCount = idc.new LRUMapCountLimited( tries );
105 			final Map<String, IndexedDiskElementDescriptor> cacheSize = idc.new LRUMapSizeLimited( tries/1024/2 );
106 
107             for ( int j = 0; j < loops; j++ )
108             {
109                 cacheName = "LRU Count           ";
110                 start = System.currentTimeMillis();
111                 for ( int i = 0; i < tries; i++ )
112                 {
113                     cacheCount.put( "key:" + i,  new IndexedDiskElementDescriptor(i, i) );
114                 }
115                 end = System.currentTimeMillis();
116                 time = end - start;
117                 putTotalCount += time;
118                 minTimeCountPut = Math.min(time, minTimeCountPut);
119                 tPer = Float.intBitsToFloat( (int) time ) / Float.intBitsToFloat( tries );
120                 System.out.println( cacheName + " put time for " + tries + " = " + time + "; millis per = " + tPer );
121 
122                 start = System.currentTimeMillis();
123                 for ( int i = 0; i < tries; i++ )
124                 {
125                     cacheCount.get( "key:" + i );
126                 }
127                 end = System.currentTimeMillis();
128                 time = end - start;
129                 getTotalCount += time;
130                 minTimeCountGet = Math.min(minTimeCountGet, time);
131                 tPer = Float.intBitsToFloat( (int) time ) / Float.intBitsToFloat( tries );
132                 System.out.println( cacheName + " get time for " + tries + " = " + time + "; millis per = " + tPer );
133 
134                 ///////////////////////////////////////////////////////////////
135                 cache2Name = "LRU Size            ";
136                 //or LRUMapJCS
137                 //cache2Name = "Hashtable";
138                 //Hashtable cache2 = new Hashtable();
139                 start = System.currentTimeMillis();
140                 for ( int i = 0; i < tries; i++ )
141                 {
142                     cacheSize.put( "key:" + i, new IndexedDiskElementDescriptor(i, i) );
143                 }
144                 end = System.currentTimeMillis();
145                 time = end - start;
146                 putTotalSize += time;
147                 minTimeSizePut = Math.min(minTimeSizePut, time);
148 
149                 tPer = Float.intBitsToFloat( (int) time ) / Float.intBitsToFloat( tries );
150                 System.out.println( cache2Name + " put time for " + tries + " = " + time + "; millis per = " + tPer );
151 
152                 start = System.currentTimeMillis();
153                 for ( int i = 0; i < tries; i++ )
154                 {
155                     cacheSize.get( "key:" + i );
156                 }
157                 end = System.currentTimeMillis();
158                 time = end - start;
159                 getTotalSize += time;
160                 minTimeSizeGet = Math.min(minTimeSizeGet, time);
161 
162                 tPer = Float.intBitsToFloat( (int) time ) / Float.intBitsToFloat( tries );
163                 System.out.println( cache2Name + " get time for " + tries + " = " + time + "; millis per = " + tPer );
164 
165                 System.out.println( "\n" );
166             }
167         }
168         catch ( final Exception e )
169         {
170             e.printStackTrace( System.out );
171             System.out.println( e );
172         }
173 
174         final long putAvCount = putTotalCount / loops;
175         final long getAvCount = getTotalCount / loops;
176         final long putAvSize = putTotalSize / loops;
177         final long getAvSize = getTotalSize / loops;
178 
179         System.out.println( "Finished " + loops + " loops of " + tries + " gets and puts" );
180 
181         System.out.println( "\n" );
182         System.out.println( "Put average for " + cacheName +  " = " + putAvCount );
183         System.out.println( "Put average for " + cache2Name + " = " + putAvSize );
184         ratioPut = (putAvSize *1.0) / putAvCount;
185         System.out.println( cache2Name.trim() + " puts took " + ratioPut + " times the " + cacheName.trim() + ", the goal is <" + targetPut
186             + "x" );
187 
188         System.out.println( "\n" );
189         System.out.println( "Put minimum for " + cacheName +  " = " + minTimeCountPut );
190         System.out.println( "Put minimum for " + cache2Name + " = " + minTimeSizePut );
191         ratioPut = (minTimeSizePut * 1.0) / minTimeCountPut;
192         System.out.println( cache2Name.trim() + " puts took " + ratioPut + " times the " + cacheName.trim() + ", the goal is <" + targetPut
193             + "x" );
194 
195         System.out.println( "\n" );
196         System.out.println( "Get average for " + cacheName + " = " + getAvCount );
197         System.out.println( "Get average for " + cache2Name + " = " + getAvSize );
198         ratioGet = Float.intBitsToFloat( (int) getAvCount ) / Float.intBitsToFloat( (int) getAvSize );
199         ratioGet = (getAvSize * 1.0) / getAvCount;
200         System.out.println( cache2Name.trim() + " gets took " + ratioGet + " times the " + cacheName.trim() + ", the goal is <" + targetGet
201             + "x" );
202 
203         System.out.println( "\n" );
204         System.out.println( "Get minimum for " + cacheName +  " = " + minTimeCountGet );
205         System.out.println( "Get minimum for " + cache2Name + " = " + minTimeSizeGet );
206         ratioPut = (minTimeSizeGet * 1.0) / minTimeCountGet;
207         System.out.println( cache2Name.trim() + " puts took " + ratioPut + " times the " + cacheName.trim() + ", the goal is <" + targetGet
208             + "x" );
209     }
210 }