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