1 package org.apache.jcs.auxiliary.disk.block;
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 org.apache.jcs.JCS;
25 import org.apache.jcs.utils.timing.ElapsedTimer;
26 import org.apache.jcs.utils.timing.SleepUtil;
27
28 /***
29 * Put a few hundred thousand entries in the block disk cache.
30 * <p.
31 * @author Aaron Smuts
32 *
33 */
34 public class HugeQuantityBlockDiskCacheLoadTest
35 extends TestCase
36 {
37
38 /***
39 * Test setup
40 */
41 public void setUp()
42 {
43 JCS.setConfigFilename( "/TestBlockDiskCacheHuge.ccf" );
44 }
45
46 /***
47 * Adds items to cache, gets them, and removes them. The item count is more
48 * than the size of the memory cache, so items should spool to disk.
49 *
50 * @param region
51 * Name of the region to access
52 *
53 * @exception Exception
54 * If an error occurs
55 */
56 public void testLargeNumberOfItems()
57 throws Exception
58 {
59 int items = 300000;
60 String region = "testCache1";
61
62 System.out.println( "--------------------------" );
63 long initialMemory = measureMemoryUse();
64 System.out.println( "Before getting JCS: " + initialMemory );
65
66 JCS jcs = JCS.getInstance( region );
67 jcs.clear();
68
69 try
70 {
71 ElapsedTimer timer = new ElapsedTimer();
72 System.out.println( "Start: " + measureMemoryUse() );
73
74
75 for ( int i = 0; i <= items; i++ )
76 {
77 jcs.put( i + ":key", region + " data " + i );
78 }
79
80 System.out.println( jcs.getStats() );
81 System.out.println( "--------------------------" );
82 System.out.println( "After put: " + measureMemoryUse() );
83
84 Thread.sleep( 5000 );
85
86 System.out.println( jcs.getStats() );
87 System.out.println( "--------------------------" );
88 System.out.println( "After wait: " + measureMemoryUse() );
89
90 for ( int i = 0; i < 10; i++ )
91 {
92 SleepUtil.sleepAtLeast( 3000 );
93 System.out.println( "--------------------------" );
94 System.out.println( "After sleep. " + timer.getElapsedTimeString() + " memory used = " + measureMemoryUse() );
95 System.out.println( jcs.getStats() );
96 }
97
98
99 System.out.println( "--------------------------" );
100 System.out.println( "Retrieving all." );
101 for ( int i = 0; i <= items; i++ )
102 {
103
104 String value = (String) jcs.get( i + ":key" );
105 if( i % 1000 == 0 )
106 {
107
108 System.out.println( i + " ");
109 }
110 assertEquals( "Wrong value returned.", region + " data " + i, value );
111 }
112 long aftetGet = measureMemoryUse();
113 System.out.println( "After get: " + aftetGet + " diff = " + (aftetGet - initialMemory));
114
115 }
116 finally
117 {
118
119 System.out.println( jcs.getStats() );
120 System.out.println( "--------------------------" );
121 long endMemory = measureMemoryUse();
122 System.out.println( "End: " + endMemory + " diff = " + (endMemory - initialMemory) );
123 }
124 }
125
126 /***
127 * Measure memory used by the VM.
128 *
129 * @return
130 * @throws InterruptedException
131 */
132 protected long measureMemoryUse()
133 throws InterruptedException
134 {
135 System.gc();
136 Thread.sleep( 3000 );
137 System.gc();
138 return Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
139 }
140 }