1 package org.apache.commons.jcs3.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.jcs3.auxiliary.disk.DiskTestObject;
28 import org.apache.commons.jcs3.JCS;
29 import org.apache.commons.jcs3.access.CacheAccess;
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 */
35 public class BlockDiskCacheSteadyLoadTest
36 extends TestCase
37 {
38 /** String for separating log entries. */
39 private static final String LOG_DIVIDER = "---------------------------";
40
41 /** the runtime. */
42 private static final Runtime rt = Runtime.getRuntime();
43
44 /** The decimal format to use int he logs. */
45 private static final DecimalFormat format = new DecimalFormat( "#,###" );
46
47 /**
48 * Insert 2000 wait 1 second, repeat. Average 1000 / sec.
49 *
50 * @throws Exception
51 */
52 public void testRunSteadyLoadTest()
53 throws Exception
54 {
55 JCS.setConfigFilename( "/TestBlockDiskCacheSteadyLoad.ccf" );
56
57 logMemoryUsage();
58
59 final int numPerRun = 250;
60 final long pauseBetweenRuns = 1000;
61 int runCount = 0;
62 final int runs = 1000;
63 final int upperKB = 50;
64
65 final CacheAccess<String, DiskTestObject> jcs = JCS.getInstance( ( numPerRun / 2 ) + "aSecond" );
66
67 // ElapsedTimer timer = new ElapsedTimer();
68 final int numToGet = numPerRun * ( runs / 10 );
69 for ( int i = 0; i < numToGet; i++ )
70 {
71 jcs.get( String.valueOf( i ) );
72 }
73 // System.out.println( LOG_DIVIDER );
74 // System.out.println( "After getting " + numToGet );
75 // System.out.println( "Elapsed " + timer.getElapsedTimeString() );
76 logMemoryUsage();
77
78 jcs.clear();
79 Thread.sleep( 3000 );
80 // System.out.println( LOG_DIVIDER );
81 // System.out.println( "Start putting" );
82
83 // long totalSize = 0;
84 int totalPut = 0;
85
86 final Random random = new Random( 89 );
87 while ( runCount < runs )
88 {
89 runCount++;
90 for ( int i = 0; i < numPerRun; i++ )
91 {
92 // 1/2 upper to upperKB-4 KB
93 final int kiloBytes = Math.max( upperKB / 2, random.nextInt( upperKB ) );
94 final int bytes = ( kiloBytes ) * 1024;
95 // totalSize += bytes;
96 totalPut++;
97 final DiskTestObject object = new DiskTestObject( Integer.valueOf( i ), new byte[bytes] );
98 jcs.put( String.valueOf( totalPut ), object );
99 }
100
101 // get half of those inserted the previous run
102 if ( runCount > 1 )
103 {
104 for ( int j = ( ( totalPut - numPerRun ) - ( numPerRun / 2 ) ); j < ( totalPut - numPerRun ); j++ )
105 {
106 jcs.get( String.valueOf( j ) );
107 }
108 }
109
110 // remove half of those inserted the previous run
111 if ( runCount > 1 )
112 {
113 for ( int j = ( ( totalPut - numPerRun ) - ( numPerRun / 2 ) ); j < ( totalPut - numPerRun ); j++ )
114 {
115 jcs.remove( String.valueOf( j ) );
116 }
117 }
118
119
120 Thread.sleep( pauseBetweenRuns );
121 if ( runCount % 100 == 0 )
122 {
123 // System.out.println( LOG_DIVIDER );
124 // System.out.println( "Elapsed " + timer.getElapsedTimeString() );
125 // System.out.println( "Run count: " + runCount + " Average size: " + ( totalSize / totalPut ) + "\n"
126 // + jcs.getStats() );
127 logMemoryUsage();
128 }
129 }
130
131 Thread.sleep( 3000 );
132 // System.out.println( jcs.getStats() );
133 logMemoryUsage();
134
135 Thread.sleep( 10000 );
136 // System.out.println( jcs.getStats() );
137 logMemoryUsage();
138
139 System.gc();
140 Thread.sleep( 3000 );
141 System.gc();
142 // System.out.println( jcs.getStats() );
143 logMemoryUsage();
144 }
145
146 /**
147 * Logs the memory usage.
148 */
149 private static void logMemoryUsage()
150 {
151 final long byte2MB = 1024 * 1024;
152 final long total = rt.totalMemory() / byte2MB;
153 final long free = rt.freeMemory() / byte2MB;
154 final long used = total - free;
155 System.out.println( LOG_DIVIDER );
156 System.out.println( "Memory:" + " Used:" + format.format( used ) + "MB" + " Free:" + format.format( free )
157 + "MB" + " Total:" + format.format( total ) + "MB" );
158 }
159 }