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