View Javadoc
1   package org.apache.commons.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.commons.jcs.JCS;
28  import org.apache.commons.jcs.access.CacheAccess;
29  import org.apache.commons.jcs.auxiliary.disk.DiskTestObject;
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          logMemoryUsage();
60  
61          int numPerRun = 250;
62          long pauseBetweenRuns = 1000;
63          int runCount = 0;
64          int runs = 1000;
65          int upperKB = 50;
66  
67          CacheAccess<String, DiskTestObject> jcs = JCS.getInstance( ( numPerRun / 2 ) + "aSecond" );
68  
69  //        ElapsedTimer timer = new ElapsedTimer();
70          int numToGet = numPerRun * ( runs / 10 );
71          for ( int i = 0; i < numToGet; i++ )
72          {
73              jcs.get( String.valueOf( i ) );
74          }
75  //        System.out.println( LOG_DIVIDER );
76  //        System.out.println( "After getting " + numToGet );
77  //        System.out.println( "Elapsed " + timer.getElapsedTimeString() );
78          logMemoryUsage();
79  
80          jcs.clear();
81          Thread.sleep( 3000 );
82  //        System.out.println( LOG_DIVIDER );
83  //        System.out.println( "Start putting" );
84  
85  //        long totalSize = 0;
86          int totalPut = 0;
87  
88          Random random = new Random( 89 );
89          while ( runCount < runs )
90          {
91              runCount++;
92              for ( int i = 0; i < numPerRun; i++ )
93              {
94                  // 1/2 upper to upperKB-4 KB
95                  int kiloBytes = Math.max( upperKB / 2, random.nextInt( upperKB ) );
96                  int bytes = ( kiloBytes ) * 1024;
97  //                totalSize += bytes;
98                  totalPut++;
99                  DiskTestObject object = new DiskTestObject( Integer.valueOf( i ), new byte[bytes] );
100                 jcs.put( String.valueOf( totalPut ), object );
101             }
102 
103             // get half of those inserted the previous run
104             if ( runCount > 1 )
105             {
106                 for ( int j = ( ( totalPut - numPerRun ) - ( numPerRun / 2 ) ); j < ( totalPut - numPerRun ); j++ )
107                 {
108                     jcs.get( String.valueOf( j ) );
109                 }
110             }
111 
112             // remove half of those inserted the previous run
113             if ( runCount > 1 )
114             {
115                 for ( int j = ( ( totalPut - numPerRun ) - ( numPerRun / 2 ) ); j < ( totalPut - numPerRun ); j++ )
116                 {
117                     jcs.remove( String.valueOf( j ) );
118                 }
119             }
120 
121 
122             Thread.sleep( pauseBetweenRuns );
123             if ( runCount % 100 == 0 )
124             {
125 //                System.out.println( LOG_DIVIDER );
126 //                System.out.println( "Elapsed " + timer.getElapsedTimeString() );
127 //                System.out.println( "Run count: " + runCount + " Average size: " + ( totalSize / totalPut ) + "\n"
128 //                    + jcs.getStats() );
129                 logMemoryUsage();
130             }
131         }
132 
133         Thread.sleep( 3000 );
134 //        System.out.println( jcs.getStats() );
135         logMemoryUsage();
136 
137         Thread.sleep( 10000 );
138 //        System.out.println( jcs.getStats() );
139         logMemoryUsage();
140 
141         System.gc();
142         Thread.sleep( 3000 );
143         System.gc();
144 //        System.out.println( jcs.getStats() );
145         logMemoryUsage();
146     }
147 
148     /**
149      * Logs the memory usage.
150      */
151     private static void logMemoryUsage()
152     {
153         long byte2MB = 1024 * 1024;
154         long total = rt.totalMemory() / byte2MB;
155         long free = rt.freeMemory() / byte2MB;
156         long used = total - free;
157         System.out.println( LOG_DIVIDER );
158         System.out.println( "Memory:" + " Used:" + format.format( used ) + "MB" + " Free:" + format.format( free )
159             + "MB" + " Total:" + format.format( total ) + "MB" );
160     }
161 }