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