1 package org.apache.jcs.auxiliary.disk.indexed;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.IOException;
23 import java.util.Random;
24
25 import org.apache.jcs.auxiliary.disk.DiskTestObject;
26 import org.apache.jcs.engine.CacheElement;
27 import org.apache.jcs.engine.behavior.ICacheElement;
28 import org.apache.jcs.utils.serialization.StandardSerializer;
29
30 /***
31 * Utility for dealing with test objects.
32 * <p>
33 * @author Aaron Smuts
34 */
35 public class DiskTestObjectUtil
36 {
37 /***
38 * Total from the start to the endPostion.
39 * <p>
40 * @param testObjects
41 * @param endPosition
42 * @return size
43 * @throws IOException
44 */
45 public static long totalSize( DiskTestObject[] testObjects, int endPosition )
46 throws IOException
47 {
48 StandardSerializer serializer = new StandardSerializer();
49 long total = 0;
50 for ( int i = 0; i < endPosition; i++ )
51 {
52 int tileSize = serializer.serialize( testObjects[i] ).length + IndexedDisk.RECORD_HEADER;
53 total += tileSize;
54 }
55 return total;
56 }
57
58 /***
59 * Total from the start to the endPostion.
60 * <p>
61 * @param elements
62 * @param endPosition
63 * @return size
64 * @throws IOException
65 */
66 public static long totalSize( ICacheElement[] elements, int endPosition )
67 throws IOException
68 {
69 return totalSize( elements, 0, endPosition );
70 }
71
72 /***
73 * Total from the start to the endPostion.
74 * <p>
75 * @param elements
76 * @param startPosition
77 * @param endPosition
78 * @return size
79 * @throws IOException
80 */
81 public static long totalSize( ICacheElement[] elements, int startPosition, int endPosition )
82 throws IOException
83 {
84 StandardSerializer serializer = new StandardSerializer();
85 long total = 0;
86 for ( int i = startPosition; i < endPosition; i++ )
87 {
88 int tileSize = serializer.serialize( elements[i] ).length + IndexedDisk.RECORD_HEADER;
89 total += tileSize;
90 }
91 return total;
92 }
93
94 /***
95 * Creates an array of ICacheElements with DiskTestObjects with payloads the byte size.
96 * <p>
97 * @param numToCreate
98 * @param bytes
99 * @param cacheName
100 * @return ICacheElement[]
101 */
102 public static ICacheElement[] createCacheElementsWithTestObjects( int numToCreate, int bytes, String cacheName )
103 {
104 ICacheElement[] elements = new ICacheElement[numToCreate];
105 for ( int i = 0; i < numToCreate; i++ )
106 {
107
108 int size = bytes * 1024;
109 DiskTestObject tile = new DiskTestObject( new Integer( i ), new byte[size] );
110
111 ICacheElement element = new CacheElement( cacheName, tile.id, tile );
112 elements[i] = element;
113 }
114 return elements;
115 }
116
117 /***
118 * Creates an array of ICacheElements with DiskTestObjects with payloads the byte size.
119 * <p>
120 * @param numToCreate
121 * @param cacheName
122 * @return ICacheElement[]
123 */
124 public static ICacheElement[] createCacheElementsWithTestObjectsOfVariableSizes( int numToCreate, String cacheName )
125 {
126 ICacheElement[] elements = new ICacheElement[numToCreate];
127 Random random = new Random( 89 );
128 for ( int i = 0; i < numToCreate; i++ )
129 {
130 int bytes = random.nextInt( 20 );
131
132 int size = ( bytes + 4 ) * 1024;
133 DiskTestObject tile = new DiskTestObject( new Integer( i ), new byte[size] );
134
135 ICacheElement element = new CacheElement( cacheName, tile.id, tile );
136 elements[i] = element;
137 }
138 return elements;
139 }
140
141 }