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