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 java.io.Serializable;
25  import java.text.DecimalFormat;
26  import java.util.Random;
27  
28  import org.apache.commons.jcs3.JCS;
29  import org.apache.commons.jcs3.access.CacheAccess;
30  
31  /**
32   * This is for manually testing the defrag process.
33   */
34  public class IndexedDiskCacheDefragPerformanceTest
35      extends TestCase
36  {
37      /** For readability */
38      private static final String LOG_DIVIDER = "---------------------------";
39  
40      /** total to test with */
41      private static final int TOTAL_ELEMENTS = 30000;
42  
43      /** time to wait */
44      private static final long SLEEP_TIME_DISK = 8000;
45  
46      /** how often to log */
47      private static final int LOG_INCREMENT = 5000;
48  
49      /** for getting memory usage */
50      private static final Runtime rt = Runtime.getRuntime();
51  
52      /** for displaying memory usage */
53      private static final DecimalFormat format = new DecimalFormat( "#,###" );
54  
55      /**
56       * @throws Exception
57       */
58      public void testRealTimeOptimization()
59          throws Exception
60      {
61          System.out.println( LOG_DIVIDER );
62          System.out.println( "JCS DEFRAG PERFORMANCE TESTS" );
63          System.out.println( LOG_DIVIDER );
64          logMemoryUsage();
65          IndexedDiskCacheDefragPerformanceTest.runRealTimeOptimizationTest();
66          logMemoryUsage();
67  
68          System.out.println( LOG_DIVIDER );
69      }
70  
71      /**
72       * @throws Exception
73       */
74      private static void runRealTimeOptimizationTest()
75          throws Exception
76      {
77          JCS.setConfigFilename( "/TestDiskCacheDefragPerformance.ccf" );
78          final CacheAccess<Integer, Tile> jcs = JCS.getInstance( "defrag" );
79  
80          Tile tile;
81          System.out.println( "Cache Defrag Test" );
82  
83          final Random random = new Random( 89 );
84          for ( int i = 0; i < TOTAL_ELEMENTS; i++ )
85          {
86              final int bytes = random.nextInt( 20 );
87              // 4-24 KB
88              tile = new Tile( Integer.valueOf( i ), new byte[( bytes + 4 ) * 1024] );
89              // images
90  
91              jcs.put( tile.id, tile );
92  
93              if ( ( i != 0 ) && ( 0 == ( i % 100 ) ) )
94              {
95                  jcs.get( Integer.valueOf( random.nextInt( i ) ) );
96              }
97  
98              if ( 0 == ( i % LOG_INCREMENT ) )
99              {
100                 System.out.print( i + ", " );
101                 Thread.sleep( SLEEP_TIME_DISK );
102             }
103         }
104 
105         System.out.println( LOG_DIVIDER );
106         System.out.println( "Total elements = " + TOTAL_ELEMENTS );
107         System.out.println( "Stats prior to sleeping " + jcs.getStats() );
108 
109         // Allow system to settle down
110         System.out.println( "Sleeping for a minute." );
111         Thread.sleep( 60000 );
112 
113         System.out.println( LOG_DIVIDER );
114         System.out.println( "Stats prior to dispose " + jcs.getStats() );
115 
116         jcs.dispose();
117         System.out.println( LOG_DIVIDER );
118         System.out.println( "Stats after dispose " + jcs.getStats() );
119         System.out.println( "Done testing." );
120     }
121 
122     /**
123      * Logs the memory usage.
124      */
125     private static void logMemoryUsage()
126     {
127         final long byte2MB = 1024 * 1024;
128         final long total = rt.totalMemory() / byte2MB;
129         final long free = rt.freeMemory() / byte2MB;
130         final long used = total - free;
131         System.out.println( LOG_DIVIDER );
132         System.out.println( "Memory:" + " Used:" + format.format( used ) + "MB" + " Free:" + format.format( free )
133             + "MB" + " Total:" + format.format( total ) + "MB" );
134     }
135 
136     /**
137      * Resembles a cached image.
138      */
139     private static class Tile
140         implements Serializable
141     {
142         /** Don't change */
143         private static final long serialVersionUID = 1L;
144 
145         /**
146          * Key
147          */
148         public Integer id;
149 
150         /**Byte size
151          *
152          */
153         public byte[] imageBytes;
154 
155         /**
156          * @param id
157          * @param imageBytes
158          */
159         public Tile( final Integer id, final byte[] imageBytes )
160         {
161             this.id = id;
162             this.imageBytes = imageBytes;
163         }
164     }
165 
166     /**
167      * @param args
168      */
169     public static void main( final String args[] )
170     {
171         try
172         {
173             final IndexedDiskCacheDefragPerformanceTest tester = new IndexedDiskCacheDefragPerformanceTest();
174             tester.testRealTimeOptimization();
175         }
176         catch ( final Exception e )
177         {
178             e.printStackTrace();
179         }
180     }
181 }