1 package org.apache.commons.jcs.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 junit.framework.TestCase;
23 import org.apache.commons.jcs.JCS;
24 import org.apache.commons.jcs.access.CacheAccess;
25
26 import java.io.Serializable;
27 import java.text.DecimalFormat;
28 import java.util.Random;
29
30
31
32
33 public class IndexedDiskCacheDefragPerformanceTest
34 extends TestCase
35 {
36
37 private static final String LOG_DIVIDER = "---------------------------";
38
39
40 private static final int TOTAL_ELEMENTS = 30000;
41
42
43 private static final long SLEEP_TIME_DISK = 8000;
44
45
46 private static final int LOG_INCREMENT = 5000;
47
48
49 private static Runtime rt = Runtime.getRuntime();
50
51
52 private static DecimalFormat format = new DecimalFormat( "#,###" );
53
54
55
56
57 public void testRealTimeOptimization()
58 throws Exception
59 {
60 System.out.println( LOG_DIVIDER );
61 System.out.println( "JCS DEFRAG PERFORMANCE TESTS" );
62 System.out.println( LOG_DIVIDER );
63 logMemoryUsage();
64 IndexedDiskCacheDefragPerformanceTest.runRealTimeOptimizationTest();
65 logMemoryUsage();
66
67 System.out.println( LOG_DIVIDER );
68 }
69
70
71
72
73 private static void runRealTimeOptimizationTest()
74 throws Exception
75 {
76 JCS.setConfigFilename( "/TestDiskCacheDefragPerformance.ccf" );
77 CacheAccess<Integer, Tile> jcs = JCS.getInstance( "defrag" );
78
79 Tile tile;
80 System.out.println( "Cache Defrag Test" );
81
82 Random random = new Random( 89 );
83 for ( int i = 0; i < TOTAL_ELEMENTS; i++ )
84 {
85 int bytes = random.nextInt( 20 );
86
87 tile = new Tile( Integer.valueOf( i ), new byte[( bytes + 4 ) * 1024] );
88
89
90 jcs.put( tile.id, tile );
91
92 if ( ( i != 0 ) && ( 0 == ( i % 100 ) ) )
93 {
94 jcs.get( Integer.valueOf( random.nextInt( i ) ) );
95 }
96
97 if ( 0 == ( i % LOG_INCREMENT ) )
98 {
99 System.out.print( i + ", " );
100 Thread.sleep( SLEEP_TIME_DISK );
101 }
102 }
103
104 System.out.println( LOG_DIVIDER );
105 System.out.println( "Total elements = " + TOTAL_ELEMENTS );
106 System.out.println( "Stats prior to sleeping " + jcs.getStats() );
107
108
109 System.out.println( "Sleeping for a a minute." );
110 Thread.sleep( 60000 );
111
112 System.out.println( LOG_DIVIDER );
113 System.out.println( "Stats prior to dispose " + jcs.getStats() );
114
115 jcs.dispose();
116 System.out.println( LOG_DIVIDER );
117 System.out.println( "Stats after dispose " + jcs.getStats() );
118 System.out.println( "Done testing." );
119 }
120
121
122
123
124 private static void logMemoryUsage()
125 {
126 long byte2MB = 1024 * 1024;
127 long total = rt.totalMemory() / byte2MB;
128 long free = rt.freeMemory() / byte2MB;
129 long used = total - free;
130 System.out.println( LOG_DIVIDER );
131 System.out.println( "Memory:" + " Used:" + format.format( used ) + "MB" + " Free:" + format.format( free )
132 + "MB" + " Total:" + format.format( total ) + "MB" );
133 }
134
135
136
137
138 private static class Tile
139 implements Serializable
140 {
141
142 private static final long serialVersionUID = 1L;
143
144
145
146
147 public Integer id;
148
149
150
151
152 public byte[] imageBytes;
153
154
155
156
157
158 public Tile( Integer id, byte[] imageBytes )
159 {
160 this.id = id;
161 this.imageBytes = imageBytes;
162 }
163 }
164
165
166
167
168 public static void main( String args[] )
169 {
170 try
171 {
172 IndexedDiskCacheDefragPerformanceTest tester = new IndexedDiskCacheDefragPerformanceTest();
173 tester.testRealTimeOptimization();
174 }
175 catch ( Exception e )
176 {
177 e.printStackTrace();
178 }
179 }
180 }