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