View Javadoc
1   package org.apache.commons.jcs.utils.struct;
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 junit.framework.Test;
23  import junit.framework.TestCase;
24  import junit.framework.TestSuite;
25  import org.apache.commons.jcs.JCSvsHashtablePerformanceTest;
26  
27  import java.util.Map;
28  
29  /**
30   * This ensures that the jcs version of the LRU map is as fast as the commons
31   * version. It has been testing at .6 to .7 times the commons LRU.
32   * <p>
33   * @author aaronsm
34   *
35   */
36  public class LRUMapPerformanceTest
37      extends TestCase
38  {
39      /** The put put ration after the test */
40      float ratioPut = 0;
41  
42      /** The ratio after the test */
43      float ratioGet = 0;
44  
45      /** put jcs / commons ratio */
46      float targetPut = 1.2f;
47  
48      /** get jcs / commons ratio */
49      float targetGet = .5f;
50  
51      /** Time to loop */
52      int loops = 20;
53  
54      /** items to put and get per loop */
55      int tries = 100000;
56  
57      /**
58       * @param testName
59       */
60      public LRUMapPerformanceTest( String testName )
61      {
62          super( testName );
63      }
64  
65      /**
66       * A unit test suite for JUnit
67       * <p>
68       * @return The test suite
69       */
70      public static Test suite()
71      {
72          return new TestSuite( LRUMapPerformanceTest.class );
73      }
74  
75      /**
76       * A unit test for JUnit
77       *
78       * @throws Exception
79       *                Description of the Exception
80       */
81      public void testSimpleLoad()
82          throws Exception
83      {
84          doWork();
85          assertTrue( this.ratioPut < targetPut );
86          assertTrue( this.ratioGet < targetGet );
87      }
88  
89      /**
90       *
91       */
92      public void doWork()
93      {
94          long start = 0;
95          long end = 0;
96          long time = 0;
97          float tPer = 0;
98  
99          long putTotalJCS = 0;
100         long getTotalJCS = 0;
101         long putTotalHashtable = 0;
102         long getTotalHashtable = 0;
103 
104         String name = "LRUMap";
105         String cache2Name = "";
106 
107         try
108         {
109             Map<String, String> cache = new LRUMap<String, String>( tries );
110 
111             for ( int j = 0; j < loops; j++ )
112             {
113                 name = "JCS      ";
114                 start = System.currentTimeMillis();
115                 for ( int i = 0; i < tries; i++ )
116                 {
117                     cache.put( "key:" + i, "data" + i );
118                 }
119                 end = System.currentTimeMillis();
120                 time = end - start;
121                 putTotalJCS += time;
122                 tPer = Float.intBitsToFloat( (int) time ) / Float.intBitsToFloat( tries );
123                 System.out.println( name + " put time for " + tries + " = " + time + "; millis per = " + tPer );
124 
125                 start = System.currentTimeMillis();
126                 for ( int i = 0; i < tries; i++ )
127                 {
128                     cache.get( "key:" + i );
129                 }
130                 end = System.currentTimeMillis();
131                 time = end - start;
132                 getTotalJCS += time;
133                 tPer = Float.intBitsToFloat( (int) time ) / Float.intBitsToFloat( tries );
134                 System.out.println( name + " get time for " + tries + " = " + time + "; millis per = " + tPer );
135 
136                 ///////////////////////////////////////////////////////////////
137                 cache2Name = "LRUMapJCS (commons)";
138                 //or LRUMapJCS
139                 Map<String, String> cache2 = new org.apache.commons.collections4.map.LRUMap<String, String>( tries );
140                 //cache2Name = "Hashtable";
141                 //Hashtable cache2 = new Hashtable();
142                 start = System.currentTimeMillis();
143                 for ( int i = 0; i < tries; i++ )
144                 {
145                     cache2.put( "key:" + i, "data" + i );
146                 }
147                 end = System.currentTimeMillis();
148                 time = end - start;
149                 putTotalHashtable += time;
150                 tPer = Float.intBitsToFloat( (int) time ) / Float.intBitsToFloat( tries );
151                 System.out.println( cache2Name + " put time for " + tries + " = " + time + "; millis per = " + tPer );
152 
153                 start = System.currentTimeMillis();
154                 for ( int i = 0; i < tries; i++ )
155                 {
156                     cache2.get( "key:" + i );
157                 }
158                 end = System.currentTimeMillis();
159                 time = end - start;
160                 getTotalHashtable += time;
161                 tPer = Float.intBitsToFloat( (int) time ) / Float.intBitsToFloat( tries );
162                 System.out.println( cache2Name + " get time for " + tries + " = " + time + "; millis per = " + tPer );
163 
164                 System.out.println( "\n" );
165             }
166         }
167         catch ( Exception e )
168         {
169             e.printStackTrace( System.out );
170             System.out.println( e );
171         }
172 
173         long putAvJCS = putTotalJCS / loops;
174         long getAvJCS = getTotalJCS / loops;
175         long putAvHashtable = putTotalHashtable / loops;
176         long getAvHashtable = getTotalHashtable / loops;
177 
178         System.out.println( "Finished " + loops + " loops of " + tries + " gets and puts" );
179 
180         System.out.println( "\n" );
181         System.out.println( "Put average for LRUMap       = " + putAvJCS );
182         System.out.println( "Put average for " + cache2Name + " = " + putAvHashtable );
183         ratioPut = Float.intBitsToFloat( (int) putAvJCS ) / Float.intBitsToFloat( (int) putAvHashtable );
184         System.out.println( name + " puts took " + ratioPut + " times the " + cache2Name + ", the goal is <" + targetPut
185             + "x" );
186 
187         System.out.println( "\n" );
188         System.out.println( "Get average for LRUMap       = " + getAvJCS );
189         System.out.println( "Get average for " + cache2Name + " = " + getAvHashtable );
190         ratioGet = Float.intBitsToFloat( (int) getAvJCS ) / Float.intBitsToFloat( (int) getAvHashtable );
191         System.out.println( name + " gets took " + ratioGet + " times the " + cache2Name + ", the goal is <" + targetGet
192             + "x" );
193     }
194 
195     /**
196      * @param args
197      */
198     public static void main( String args[] )
199     {
200         JCSvsHashtablePerformanceTest test = new JCSvsHashtablePerformanceTest( "command" );
201         test.doWork();
202     }
203 
204 }