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