1 package org.apache.jcs;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.Hashtable;
23
24 import junit.framework.Test;
25 import junit.framework.TestCase;
26 import junit.framework.TestSuite;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.jcs.engine.memory.lru.LRUMemoryCache;
31
32 /***
33 * This test ensures that basic memory operations are with a speficified order
34 * of magnitude of the java.util.Hashtable.
35 * <p>
36 * Currenlty JCS is un 2x a hashtable for gets, and under 1.2x for puts.
37 *
38 */
39 public class JCSvsHashtablePerformanceTest
40 extends TestCase
41 {
42
43 float ratioPut = 0;
44
45 float ratioGet = 0;
46
47 float target = 3.50f;
48
49 int loops = 20;
50
51 int tries = 50000;
52
53 /***
54 * @param testName
55 */
56 public JCSvsHashtablePerformanceTest( String testName )
57 {
58 super( testName );
59 }
60
61 /***
62 * A unit test suite for JUnit
63 *
64 * @return The test suite
65 */
66 public static Test suite()
67 {
68 return new TestSuite( JCSvsHashtablePerformanceTest.class );
69 }
70
71 /***
72 * A unit test for JUnit
73 *
74 * @exception Exception
75 * 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 JCS 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 cache2 = new Hashtable();
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 }