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 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 BlockDiskCacheSteadyLoadTest
38      extends TestCase
39  {
40      /*** String for separating log entries. */
41      private static final String LOG_DIVIDER = "---------------------------";
42  
43      /*** the runtime. */
44      private static Runtime rt = Runtime.getRuntime();
45  
46      /*** The decimal format to use int he logs. */
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( "/TestBlockDiskCacheSteadyLoad.ccf" );
58  
59          System.out.println( "runSteadyLoadTest" );
60  
61          logMemoryUsage();
62  
63          int numPerRun = 250;
64          long pauseBetweenRuns = 1000;
65          int runCount = 0;
66          int runs = 1000;
67          int upperKB = 50;
68  
69          JCS 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( new Integer( i ), new byte[bytes] );
102                 jcs.put( String.valueOf( totalPut ), object );
103             }
104 
105             // get 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.get( String.valueOf( j ) );
111                 }
112             }
113 
114             // remove half of those inserted the previous run
115             if ( runCount > 1 )
116             {
117                 for ( int j = ( ( totalPut - numPerRun ) - ( numPerRun / 2 ) ); j < ( totalPut - numPerRun ); j++ )
118                 {
119                     jcs.remove( String.valueOf( j ) );
120                 }
121             }
122 
123 
124             Thread.sleep( pauseBetweenRuns );
125             if ( runCount % 1 == 0 )
126             {
127                 System.out.println( LOG_DIVIDER );
128                 System.out.println( "Elapsed " + timer.getElapsedTimeString() );
129                 System.out.println( "Run count: " + runCount + " Average size: " + ( totalSize / totalPut ) + "\n"
130                     + jcs.getStats() );
131                 logMemoryUsage();
132             }
133         }
134 
135         Thread.sleep( 3000 );
136         System.out.println( jcs.getStats() );
137         logMemoryUsage();
138 
139         Thread.sleep( 10000 );
140         System.out.println( jcs.getStats() );
141         logMemoryUsage();
142 
143         System.gc();
144         Thread.sleep( 3000 );
145         System.gc();
146         System.out.println( jcs.getStats() );
147         logMemoryUsage();
148     }
149 
150     /***
151      * Logs the memory usage.
152      */
153     private static void logMemoryUsage()
154     {
155         long byte2MB = 1024 * 1024;
156         long total = rt.totalMemory() / byte2MB;
157         long free = rt.freeMemory() / byte2MB;
158         long used = total - free;
159         System.out.println( LOG_DIVIDER );
160         System.out.println( "Memory:" + " Used:" + format.format( used ) + "MB" + " Free:" + format.format( free )
161             + "MB" + " Total:" + format.format( total ) + "MB" );
162     }
163 }