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