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