1 package org.apache.commons.jcs3.engine.memory.mru;
2
3 import org.apache.commons.jcs3.JCS;
4 import org.apache.commons.jcs3.access.CacheAccess;
5 import org.apache.commons.jcs3.engine.memory.lru.LRUMemoryCache;
6 import org.apache.commons.jcs3.log.Log;
7 import org.apache.commons.jcs3.log.LogManager;
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 import junit.framework.TestCase;
29
30
31
32
33 public class LRUvsMRUPerformanceTest
34 extends TestCase
35 {
36
37 float ratioPut;
38
39
40 float ratioGet;
41
42
43 float target = 1.20f;
44
45
46 int loops = 20;
47
48
49 int tries = 10000;
50
51
52
53
54
55 public void testSimpleLoad()
56 throws Exception
57 {
58 final Log log1 = LogManager.getLog( LRUMemoryCache.class );
59 if ( log1.isDebugEnabled() )
60 {
61 System.out.println( "The log level must be at info or above for the a performance test." );
62 return;
63 }
64 final Log log2 = LogManager.getLog( MRUMemoryCache.class );
65 if ( log2.isDebugEnabled() )
66 {
67 System.out.println( "The log level must be at info or above for the a performance test." );
68 return;
69 }
70 doWork();
71
72
73
74
75 }
76
77
78
79
80 public void doWork()
81 {
82
83 long start = 0;
84 long end = 0;
85 long time = 0;
86 float tPer = 0;
87
88 long putTotalLRU = 0;
89 long getTotalLRU = 0;
90 long putTotalMRU = 0;
91 long getTotalMRU = 0;
92
93 try
94 {
95
96 JCS.setConfigFilename( "/TestMRUCache.ccf" );
97 final CacheAccess<String, String> cache = JCS.getInstance( "lruDefined" );
98 final CacheAccess<String, String> mru = JCS.getInstance( "mruDefined" );
99
100 System.out.println( "LRU = " + cache );
101
102 for ( int j = 0; j < loops; j++ )
103 {
104
105 System.out.println( "Beginning loop " + j );
106
107 String name = "LRU ";
108 start = System.currentTimeMillis();
109 for ( int i = 0; i < tries; i++ )
110 {
111 cache.put( "key:" + i, "data" + i );
112 }
113 end = System.currentTimeMillis();
114 time = end - start;
115 putTotalLRU += time;
116 tPer = Float.intBitsToFloat( (int) time ) / Float.intBitsToFloat( tries );
117 System.out.println( name + " put time for " + tries + " = " + time + "; millis per = " + tPer );
118
119 start = System.currentTimeMillis();
120 for ( int i = 0; i < tries; i++ )
121 {
122 cache.get( "key:" + i );
123 }
124 end = System.currentTimeMillis();
125 time = end - start;
126 getTotalLRU += time;
127 tPer = Float.intBitsToFloat( (int) time ) / Float.intBitsToFloat( tries );
128 System.out.println( name + " get time for " + tries + " = " + time + "; millis per = " + tPer );
129
130
131 name = "MRU";
132 start = System.currentTimeMillis();
133 for ( int i = 0; i < tries; i++ )
134 {
135 mru.put( "key:" + i, "data" + i );
136 }
137 end = System.currentTimeMillis();
138 time = end - start;
139 putTotalMRU += time;
140 tPer = Float.intBitsToFloat( (int) time ) / Float.intBitsToFloat( tries );
141 System.out.println( name + " put time for " + tries + " = " + time + "; millis per = " + tPer );
142
143 start = System.currentTimeMillis();
144 for ( int i = 0; i < tries; i++ )
145 {
146 mru.get( "key:" + i );
147 }
148 end = System.currentTimeMillis();
149 time = end - start;
150 getTotalMRU += time;
151 tPer = Float.intBitsToFloat( (int) time ) / Float.intBitsToFloat( tries );
152 System.out.println( name + " get time for " + tries + " = " + time + "; millis per = " + tPer );
153
154 System.out.println( "\n" );
155 }
156
157 }
158 catch ( final Exception e )
159 {
160 e.printStackTrace( System.out );
161 System.out.println( e );
162 }
163
164 final long putAvJCS = putTotalLRU / loops;
165 final long getAvJCS = getTotalLRU / loops;
166 final long putAvHashtable = putTotalMRU / loops;
167 final long getAvHashtable = getTotalMRU / loops;
168
169 System.out.println( "Finished " + loops + " loops of " + tries + " gets and puts" );
170
171 System.out.println( "\n" );
172 System.out.println( "Put average for JCS = " + putAvJCS );
173 System.out.println( "Put average for MRU = " + putAvHashtable );
174 ratioPut = Float.intBitsToFloat( (int) putAvJCS ) / Float.intBitsToFloat( (int) putAvHashtable );
175 System.out.println( "JCS puts took " + ratioPut + " times the Hashtable, the goal is <" + target + "x" );
176
177 System.out.println( "\n" );
178 System.out.println( "Get average for JCS = " + getAvJCS );
179 System.out.println( "Get average for MRU = " + getAvHashtable );
180 ratioGet = Float.intBitsToFloat( (int) getAvJCS ) / Float.intBitsToFloat( (int) getAvHashtable );
181 System.out.println( "JCS gets took " + ratioGet + " times the Hashtable, the goal is <" + target + "x" );
182 }
183
184 }