1   package org.apache.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  
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              // Add items to cache
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              // Test that all items are in cache
99              System.out.println( "--------------------------" );
100             System.out.println( "Retrieving all." );
101             for ( int i = 0; i <= items; i++ )
102             {
103                 //System.out.print(  "\033[s" );
104                 String value = (String) jcs.get( i + ":key" );
105                 if( i % 1000 == 0 )
106                 {
107                     //System.out.print(  "\033[r" );
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             // dump the stats to the report
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 }