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