1 package org.apache.commons.jcs;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
34
35
36
37
38 public class JCSvsHashtablePerformanceTest
39 extends TestCase
40 {
41
42 float ratioPut = 0;
43
44
45 float ratioGet = 0;
46
47
48 float target = 3.50f;
49
50
51 int loops = 20;
52
53
54 int tries = 50000;
55
56
57
58
59 public JCSvsHashtablePerformanceTest( String testName )
60 {
61 super( testName );
62 }
63
64
65
66
67
68 public static Test suite()
69 {
70 return new TestSuite( JCSvsHashtablePerformanceTest.class );
71 }
72
73
74
75
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
203
204 public static void main( String args[] )
205 {
206 JCSvsHashtablePerformanceTest test = new JCSvsHashtablePerformanceTest( "command" );
207 test.doWork();
208 }
209
210 }