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