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