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