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