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