1 package org.apache.commons.jcs.auxiliary.disk.indexed;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import junit.framework.TestCase;
23 import org.apache.commons.jcs.JCS;
24 import org.apache.commons.jcs.access.CacheAccess;
25
26 /**
27 * Put a few hundred thousand entries in the disk cache.
28 * <p>
29 * @author Aaron Smuts
30 */
31 public class HugeQuantityIndDiskCacheLoadTest
32 extends TestCase
33 {
34 /** Test setup. */
35 @Override
36 public void setUp()
37 {
38 JCS.setConfigFilename( "/TestDiskCacheHuge.ccf" );
39 }
40
41 /**
42 * Adds items to cache, gets them, and removes them. The item count is more than the size of the
43 * memory cache, so items should spool to disk.
44 * <p>
45 * @throws Exception If an error occurs
46 */
47 public void testLargeNumberOfItems()
48 throws Exception
49 {
50 int items = 300000;
51 String region = "testCache1";
52
53 CacheAccess<String, String> jcs = JCS.getInstance( region );
54
55 try
56 {
57 System.out.println( "Start: " + measureMemoryUse() );
58
59 // Add items to cache
60
61 for ( int i = 0; i <= items; i++ )
62 {
63 jcs.put( i + ":key", region + " data " + i );
64 }
65
66 System.out.println( jcs.getStats() );
67 System.out.println( "--------------------------" );
68 System.out.println( "After put: " + measureMemoryUse() );
69
70 Thread.sleep( 5000 );
71
72 System.out.println( jcs.getStats() );
73 System.out.println( "--------------------------" );
74 System.out.println( "After wait: " + measureMemoryUse() );
75
76 // Test that all items are in cache
77
78 for ( int i = 0; i <= items; i++ )
79 {
80 String value = jcs.get( i + ":key" );
81
82 assertEquals( region + " data " + i, value );
83 }
84
85 System.out.println( "After get: " + measureMemoryUse() );
86
87 // // Remove all the items
88 // for ( int i = 0; i <= items; i++ )
89 // {
90 // jcs.remove( i + ":key" );
91 // }
92 //
93 // // Verify removal
94 // for ( int i = 0; i <= items; i++ )
95 // {
96 // assertNull( "Removed key should be null: " + i + ":key" + "\n
97 // stats " + jcs.getStats(), jcs.get( i + ":key" ) );
98 // }
99
100 }
101 finally
102 {
103 // dump the stats to the report
104 System.out.println( jcs.getStats() );
105 System.out.println( "--------------------------" );
106 System.out.println( "End: " + measureMemoryUse() );
107 }
108 }
109
110 /**
111 * Measure memory used by the VM.
112 * <p>
113 * @return memory used
114 * @throws InterruptedException
115 */
116 protected long measureMemoryUse()
117 throws InterruptedException
118 {
119 System.gc();
120 Thread.sleep( 3000 );
121 System.gc();
122 return Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
123 }
124 }