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