View Javadoc
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  import org.apache.commons.jcs.auxiliary.disk.DiskTestObject;
26  import org.apache.commons.jcs.utils.timing.ElapsedTimer;
27  
28  import java.text.DecimalFormat;
29  import java.util.Random;
30  
31  /**
32   * This allows you to put thousands of large objects into the disk cache and to force removes to
33   * trigger optimizations along the way.
34   * <p>
35   * @author Aaron Smuts
36   */
37  public class IndexedDiskCacheSteadyLoadTest
38      extends TestCase
39  {
40      /** For display */
41      private static final String LOG_DIVIDER = "---------------------------";
42  
43      /** For getting memory info */
44      private static Runtime rt = Runtime.getRuntime();
45  
46      /** For display */
47      private static DecimalFormat format = new DecimalFormat( "#,###" );
48  
49      /**
50       * Insert 2000 wait 1 second, repeat. Average 1000 / sec.
51       * <p>
52       * @throws Exception
53       */
54      public void testRunSteadyLoadTest()
55          throws Exception
56      {
57          JCS.setConfigFilename( "/TestDiskCacheSteadyLoad.ccf" );
58  
59          System.out.println( "runSteadyLoadTest" );
60  
61          logMemoryUsage();
62  
63          int numPerRun = 200;
64          long pauseBetweenRuns = 1000;
65          int runCount = 0;
66          int runs = 1000;
67          int upperKB = 50;
68  
69          CacheAccess<String, DiskTestObject> jcs = JCS.getInstance( ( numPerRun / 2 ) + "aSecond" );
70  
71          ElapsedTimer timer = new ElapsedTimer();
72          int numToGet = numPerRun * ( runs / 10 );
73          for ( int i = 0; i < numToGet; i++ )
74          {
75              jcs.get( String.valueOf( i ) );
76          }
77          System.out.println( LOG_DIVIDER );
78          System.out.println( "After getting " + numToGet );
79          System.out.println( "Elapsed " + timer.getElapsedTimeString() );
80          logMemoryUsage();
81  
82          jcs.clear();
83          Thread.sleep( 3000 );
84          System.out.println( LOG_DIVIDER );
85          System.out.println( "Start putting" );
86  
87          long totalSize = 0;
88          int totalPut = 0;
89  
90          Random random = new Random( 89 );
91          while ( runCount < runs )
92          {
93              runCount++;
94              for ( int i = 0; i < numPerRun; i++ )
95              {
96                  // 1/2 upper to upperKB-4 KB
97                  int kiloBytes = Math.max( upperKB / 2, random.nextInt( upperKB ) );
98                  int bytes = ( kiloBytes ) * 1024;
99                  totalSize += bytes;
100                 totalPut++;
101                 DiskTestObject object = new DiskTestObject( Integer.valueOf( i ), new byte[bytes] );
102                 jcs.put( String.valueOf( totalPut ), object );
103             }
104 
105             // remove half of those inserted the previous run
106             if ( runCount > 1 )
107             {
108                 for ( int j = ( ( totalPut - numPerRun ) - ( numPerRun / 2 ) ); j < ( totalPut - numPerRun ); j++ )
109                 {
110                     jcs.remove( String.valueOf( j ) );
111                 }
112             }
113 
114             Thread.sleep( pauseBetweenRuns );
115             if ( runCount % 100 == 0 )
116             {
117                 System.out.println( LOG_DIVIDER );
118                 System.out.println( "Elapsed " + timer.getElapsedTimeString() );
119                 System.out.println( "Run count: " + runCount + " Average size: " + ( totalSize / totalPut ) + "\n"
120                     + jcs.getStats() );
121                 logMemoryUsage();
122             }
123         }
124 
125         Thread.sleep( 3000 );
126         System.out.println( jcs.getStats() );
127         logMemoryUsage();
128 
129         Thread.sleep( 10000 );
130         System.out.println( jcs.getStats() );
131         logMemoryUsage();
132 
133         System.gc();
134         Thread.sleep( 3000 );
135         System.gc();
136         System.out.println( jcs.getStats() );
137         logMemoryUsage();
138     }
139 
140     /**
141      * Logs the memory usage.
142      */
143     private static void logMemoryUsage()
144     {
145         long byte2MB = 1024 * 1024;
146         long total = rt.totalMemory() / byte2MB;
147         long free = rt.freeMemory() / byte2MB;
148         long used = total - free;
149         System.out.println( LOG_DIVIDER );
150         System.out.println( "Memory:" + " Used:" + format.format( used ) + "MB" + " Free:" + format.format( free )
151             + "MB" + " Total:" + format.format( total ) + "MB" );
152     }
153 }