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