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