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