1 package org.apache.commons.jcs.auxiliary.disk.indexed;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.Map;
23
24 import junit.framework.Test;
25 import junit.framework.TestCase;
26 import junit.framework.TestSuite;
27
28
29
30
31
32
33
34
35 public class LRUMapSizeVsCount
36 extends TestCase
37 {
38
39 double ratioPut = 0;
40
41
42 double ratioGet = 0;
43
44
45 float targetPut = 1.2f;
46
47
48 float targetGet = 1.2f;
49
50
51 int loops = 20;
52
53
54 int tries = 100000;
55
56
57
58
59 public LRUMapSizeVsCount( String testName )
60 {
61 super( testName );
62 }
63
64
65
66
67
68
69 public static Test suite()
70 {
71 return new TestSuite( LRUMapSizeVsCount.class );
72 }
73
74
75
76
77
78
79
80 public void testSimpleLoad()
81 throws Exception
82 {
83 doWork();
84 assertTrue( this.ratioPut < targetPut );
85 assertTrue( this.ratioGet < targetGet );
86 }
87
88
89
90
91 public void doWork()
92 {
93 long start = 0;
94 long end = 0;
95 long time = 0;
96 float tPer = 0;
97
98 long putTotalCount = 0;
99 long getTotalCount = 0;
100 long putTotalSize = 0;
101 long getTotalSize = 0;
102
103 long minTimeSizePut = Long.MAX_VALUE;
104 long minTimeSizeGet = Long.MAX_VALUE;
105 long minTimeCountPut = Long.MAX_VALUE;
106 long minTimeCountGet = Long.MAX_VALUE;
107
108 String cacheName = "LRUMap";
109 String cache2Name = "";
110
111 try
112 {
113 IndexedDiskCacheAttributes cattr = new IndexedDiskCacheAttributes();
114 cattr.setName("junit");
115 cattr.setCacheName("junit");
116 cattr.setDiskPath(".");
117 IndexedDiskCache<String, String> idc = new IndexedDiskCache<String, String>(cattr);
118
119 Map<String, IndexedDiskElementDescriptor> cacheCount = idc.new LRUMapCountLimited( tries );
120 Map<String, IndexedDiskElementDescriptor> cacheSize = idc.new LRUMapSizeLimited( tries/1024/2 );
121
122 for ( int j = 0; j < loops; j++ )
123 {
124 cacheName = "LRU Count ";
125 start = System.currentTimeMillis();
126 for ( int i = 0; i < tries; i++ )
127 {
128 cacheCount.put( "key:" + i, new IndexedDiskElementDescriptor(i, i) );
129 }
130 end = System.currentTimeMillis();
131 time = end - start;
132 putTotalCount += time;
133 minTimeCountPut = Math.min(time, minTimeCountPut);
134 tPer = Float.intBitsToFloat( (int) time ) / Float.intBitsToFloat( tries );
135 System.out.println( cacheName + " put time for " + tries + " = " + time + "; millis per = " + tPer );
136
137 start = System.currentTimeMillis();
138 for ( int i = 0; i < tries; i++ )
139 {
140 cacheCount.get( "key:" + i );
141 }
142 end = System.currentTimeMillis();
143 time = end - start;
144 getTotalCount += time;
145 minTimeCountGet = Math.min(minTimeCountGet, time);
146 tPer = Float.intBitsToFloat( (int) time ) / Float.intBitsToFloat( tries );
147 System.out.println( cacheName + " get time for " + tries + " = " + time + "; millis per = " + tPer );
148
149
150 cache2Name = "LRU Size ";
151
152
153
154 start = System.currentTimeMillis();
155 for ( int i = 0; i < tries; i++ )
156 {
157 cacheSize.put( "key:" + i, new IndexedDiskElementDescriptor(i, i) );
158 }
159 end = System.currentTimeMillis();
160 time = end - start;
161 putTotalSize += time;
162 minTimeSizePut = Math.min(minTimeSizePut, time);
163
164 tPer = Float.intBitsToFloat( (int) time ) / Float.intBitsToFloat( tries );
165 System.out.println( cache2Name + " put time for " + tries + " = " + time + "; millis per = " + tPer );
166
167 start = System.currentTimeMillis();
168 for ( int i = 0; i < tries; i++ )
169 {
170 cacheSize.get( "key:" + i );
171 }
172 end = System.currentTimeMillis();
173 time = end - start;
174 getTotalSize += time;
175 minTimeSizeGet = Math.min(minTimeSizeGet, time);
176
177 tPer = Float.intBitsToFloat( (int) time ) / Float.intBitsToFloat( tries );
178 System.out.println( cache2Name + " get time for " + tries + " = " + time + "; millis per = " + tPer );
179
180 System.out.println( "\n" );
181 }
182 }
183 catch ( Exception e )
184 {
185 e.printStackTrace( System.out );
186 System.out.println( e );
187 }
188
189 long putAvCount = putTotalCount / loops;
190 long getAvCount = getTotalCount / loops;
191 long putAvSize = putTotalSize / loops;
192 long getAvSize = getTotalSize / loops;
193
194 System.out.println( "Finished " + loops + " loops of " + tries + " gets and puts" );
195
196 System.out.println( "\n" );
197 System.out.println( "Put average for " + cacheName + " = " + putAvCount );
198 System.out.println( "Put average for " + cache2Name + " = " + putAvSize );
199 ratioPut = (putAvSize *1.0) / putAvCount;
200 System.out.println( cache2Name.trim() + " puts took " + ratioPut + " times the " + cacheName.trim() + ", the goal is <" + targetPut
201 + "x" );
202
203 System.out.println( "\n" );
204 System.out.println( "Put minimum for " + cacheName + " = " + minTimeCountPut );
205 System.out.println( "Put minimum for " + cache2Name + " = " + minTimeSizePut );
206 ratioPut = (minTimeSizePut * 1.0) / minTimeCountPut;
207 System.out.println( cache2Name.trim() + " puts took " + ratioPut + " times the " + cacheName.trim() + ", the goal is <" + targetPut
208 + "x" );
209
210 System.out.println( "\n" );
211 System.out.println( "Get average for " + cacheName + " = " + getAvCount );
212 System.out.println( "Get average for " + cache2Name + " = " + getAvSize );
213 ratioGet = Float.intBitsToFloat( (int) getAvCount ) / Float.intBitsToFloat( (int) getAvSize );
214 ratioGet = (getAvSize * 1.0) / getAvCount;
215 System.out.println( cache2Name.trim() + " gets took " + ratioGet + " times the " + cacheName.trim() + ", the goal is <" + targetGet
216 + "x" );
217
218 System.out.println( "\n" );
219 System.out.println( "Get minimum for " + cacheName + " = " + minTimeCountGet );
220 System.out.println( "Get minimum for " + cache2Name + " = " + minTimeSizeGet );
221 ratioPut = (minTimeSizeGet * 1.0) / minTimeCountGet;
222 System.out.println( cache2Name.trim() + " puts took " + ratioPut + " times the " + cacheName.trim() + ", the goal is <" + targetGet
223 + "x" );
224
225 }
226
227
228
229
230 public static void main( String args[] )
231 {
232 LRUMapSizeVsCount test = new LRUMapSizeVsCount( "command" );
233 test.doWork();
234 }
235
236 }