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