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