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