1 package org.apache.commons.jcs3;
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 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
33
34
35
36
37 public class JCSvsHashtablePerformanceTest
38 extends TestCase
39 {
40
41 float ratioPut;
42
43
44 float ratioGet;
45
46
47 float target = 3.50f;
48
49
50 int loops = 20;
51
52
53 int tries = 50000;
54
55
56
57
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 }