View Javadoc
1   package org.apache.commons.jcs.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.Test;
25  import junit.framework.TestCase;
26  import junit.framework.TestSuite;
27  
28  /**
29   * This ensures that the jcs version of the LRU map is as fast as the commons
30   * version. It has been testing at .6 to .7 times the commons LRU.
31   * <p>
32   * @author aaronsm
33   *
34   */
35  public class LRUMapSizeVsCount
36      extends TestCase
37  {
38      /** The put put ration after the test */
39      double ratioPut = 0;
40  
41      /** The ratio after the test */
42      double ratioGet = 0;
43  
44      /** put size / count  ratio */
45      float targetPut = 1.2f;
46  
47      /** get size / count ratio */
48      float targetGet = 1.2f;
49  
50      /** Time to loop */
51      int loops = 20;
52  
53      /** items to put and get per loop */
54      int tries = 100000;
55  
56      /**
57       * @param testName
58       */
59      public LRUMapSizeVsCount( String testName )
60      {
61          super( testName );
62      }
63  
64      /**
65       * A unit test suite for JUnit
66       * <p>
67       * @return The test suite
68       */
69      public static Test suite()
70      {
71          return new TestSuite( LRUMapSizeVsCount.class );
72      }
73  
74      /**
75       * A unit test for JUnit
76       *
77       * @throws Exception
78       *                Description of the Exception
79       */
80      public void testSimpleLoad()
81          throws Exception
82      {
83          doWork();
84          assertTrue( this.ratioPut < targetPut );
85          assertTrue( this.ratioGet < targetGet );
86      }
87  
88      /**
89       *
90       */
91      public void doWork()
92      {
93          long start = 0;
94          long end = 0;
95          long time = 0;
96          float tPer = 0;
97  
98          long putTotalCount = 0;
99          long getTotalCount = 0;
100         long putTotalSize = 0;
101         long getTotalSize = 0;
102 
103         long minTimeSizePut = Long.MAX_VALUE;
104         long minTimeSizeGet = Long.MAX_VALUE;
105         long minTimeCountPut = Long.MAX_VALUE;
106         long minTimeCountGet = Long.MAX_VALUE;
107 
108         String cacheName = "LRUMap";
109         String cache2Name = "";
110 
111         try
112         {
113         	IndexedDiskCacheAttributes cattr = new IndexedDiskCacheAttributes();
114         	cattr.setName("junit");
115         	cattr.setCacheName("junit");
116         	cattr.setDiskPath(".");
117         	IndexedDiskCache<String, String> idc = new IndexedDiskCache<String, String>(cattr);
118 
119 			Map<String, IndexedDiskElementDescriptor> cacheCount = idc.new LRUMapCountLimited( tries );
120 			Map<String, IndexedDiskElementDescriptor> cacheSize = idc.new LRUMapSizeLimited( tries/1024/2 );
121 
122             for ( int j = 0; j < loops; j++ )
123             {
124                 cacheName = "LRU Count           ";
125                 start = System.currentTimeMillis();
126                 for ( int i = 0; i < tries; i++ )
127                 {
128                     cacheCount.put( "key:" + i,  new IndexedDiskElementDescriptor(i, i) );
129                 }
130                 end = System.currentTimeMillis();
131                 time = end - start;
132                 putTotalCount += time;
133                 minTimeCountPut = Math.min(time, minTimeCountPut);
134                 tPer = Float.intBitsToFloat( (int) time ) / Float.intBitsToFloat( tries );
135                 System.out.println( cacheName + " put time for " + tries + " = " + time + "; millis per = " + tPer );
136 
137                 start = System.currentTimeMillis();
138                 for ( int i = 0; i < tries; i++ )
139                 {
140                     cacheCount.get( "key:" + i );
141                 }
142                 end = System.currentTimeMillis();
143                 time = end - start;
144                 getTotalCount += time;
145                 minTimeCountGet = Math.min(minTimeCountGet, time);
146                 tPer = Float.intBitsToFloat( (int) time ) / Float.intBitsToFloat( tries );
147                 System.out.println( cacheName + " get time for " + tries + " = " + time + "; millis per = " + tPer );
148 
149                 ///////////////////////////////////////////////////////////////
150                 cache2Name = "LRU Size            ";
151                 //or LRUMapJCS
152                 //cache2Name = "Hashtable";
153                 //Hashtable cache2 = new Hashtable();
154                 start = System.currentTimeMillis();
155                 for ( int i = 0; i < tries; i++ )
156                 {
157                     cacheSize.put( "key:" + i, new IndexedDiskElementDescriptor(i, i) );
158                 }
159                 end = System.currentTimeMillis();
160                 time = end - start;
161                 putTotalSize += time;
162                 minTimeSizePut = Math.min(minTimeSizePut, time);
163 
164                 tPer = Float.intBitsToFloat( (int) time ) / Float.intBitsToFloat( tries );
165                 System.out.println( cache2Name + " put time for " + tries + " = " + time + "; millis per = " + tPer );
166 
167                 start = System.currentTimeMillis();
168                 for ( int i = 0; i < tries; i++ )
169                 {
170                     cacheSize.get( "key:" + i );
171                 }
172                 end = System.currentTimeMillis();
173                 time = end - start;
174                 getTotalSize += time;
175                 minTimeSizeGet = Math.min(minTimeSizeGet, time);
176 
177                 tPer = Float.intBitsToFloat( (int) time ) / Float.intBitsToFloat( tries );
178                 System.out.println( cache2Name + " get time for " + tries + " = " + time + "; millis per = " + tPer );
179 
180                 System.out.println( "\n" );
181             }
182         }
183         catch ( Exception e )
184         {
185             e.printStackTrace( System.out );
186             System.out.println( e );
187         }
188 
189         long putAvCount = putTotalCount / loops;
190         long getAvCount = getTotalCount / loops;
191         long putAvSize = putTotalSize / loops;
192         long getAvSize = getTotalSize / loops;
193 
194         System.out.println( "Finished " + loops + " loops of " + tries + " gets and puts" );
195 
196         System.out.println( "\n" );
197         System.out.println( "Put average for " + cacheName +  " = " + putAvCount );
198         System.out.println( "Put average for " + cache2Name + " = " + putAvSize );
199         ratioPut = (putAvSize *1.0) / putAvCount;
200         System.out.println( cache2Name.trim() + " puts took " + ratioPut + " times the " + cacheName.trim() + ", the goal is <" + targetPut
201             + "x" );
202 
203         System.out.println( "\n" );
204         System.out.println( "Put minimum for " + cacheName +  " = " + minTimeCountPut );
205         System.out.println( "Put minimum for " + cache2Name + " = " + minTimeSizePut );
206         ratioPut = (minTimeSizePut * 1.0) / minTimeCountPut;
207         System.out.println( cache2Name.trim() + " puts took " + ratioPut + " times the " + cacheName.trim() + ", the goal is <" + targetPut
208             + "x" );
209 
210         System.out.println( "\n" );
211         System.out.println( "Get average for " + cacheName + " = " + getAvCount );
212         System.out.println( "Get average for " + cache2Name + " = " + getAvSize );
213         ratioGet = Float.intBitsToFloat( (int) getAvCount ) / Float.intBitsToFloat( (int) getAvSize );
214         ratioGet = (getAvSize * 1.0) / getAvCount;
215         System.out.println( cache2Name.trim() + " gets took " + ratioGet + " times the " + cacheName.trim() + ", the goal is <" + targetGet
216             + "x" );
217 
218         System.out.println( "\n" );
219         System.out.println( "Get minimum for " + cacheName +  " = " + minTimeCountGet );
220         System.out.println( "Get minimum for " + cache2Name + " = " + minTimeSizeGet );
221         ratioPut = (minTimeSizeGet * 1.0) / minTimeCountGet;
222         System.out.println( cache2Name.trim() + " puts took " + ratioPut + " times the " + cacheName.trim() + ", the goal is <" + targetGet
223             + "x" );
224 
225     }
226 
227     /**
228      * @param args
229      */
230     public static void main( String args[] )
231     {
232     	LRUMapSizeVsCount test = new LRUMapSizeVsCount( "command" );
233         test.doWork();
234     }
235 
236 }