1 package org.apache.commons.jcs3.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.commons.jcs3.auxiliary.disk.DiskTestObject;
26 import org.apache.commons.jcs3.engine.CacheElement;
27 import org.apache.commons.jcs3.engine.behavior.ICacheElement;
28 import org.apache.commons.jcs3.utils.serialization.StandardSerializer;
29
30
31
32
33 public class DiskTestObjectUtil
34 {
35
36
37
38
39
40
41
42
43 public static long totalSize( final DiskTestObject[] testObjects, final int endPosition )
44 throws IOException
45 {
46 final StandardSerializer serializer = new StandardSerializer();
47 long total = 0;
48 for ( int i = 0; i < endPosition; i++ )
49 {
50 final int tileSize = serializer.serialize( testObjects[i] ).length + IndexedDisk.HEADER_SIZE_BYTES;
51 total += tileSize;
52 }
53 return total;
54 }
55
56
57
58
59
60
61
62
63
64 public static <K, V> long totalSize( final ICacheElement<K, V>[] elements, final int endPosition )
65 throws IOException
66 {
67 return totalSize( elements, 0, endPosition );
68 }
69
70
71
72
73
74
75
76
77
78
79 public static <K, V> long totalSize( final ICacheElement<K, V>[] elements, final int startPosition, final int endPosition )
80 throws IOException
81 {
82 final StandardSerializer serializer = new StandardSerializer();
83 long total = 0;
84 for ( int i = startPosition; i < endPosition; i++ )
85 {
86 final int tileSize = serializer.serialize( elements[i] ).length + IndexedDisk.HEADER_SIZE_BYTES;
87 total += tileSize;
88 }
89 return total;
90 }
91
92
93
94
95
96
97
98
99
100 public static ICacheElement<Integer, DiskTestObject>[] createCacheElementsWithTestObjects( final int numToCreate, final int bytes, final String cacheName )
101 {
102 @SuppressWarnings("unchecked")
103 final
104 ICacheElement<Integer, DiskTestObject>[] elements = new ICacheElement[numToCreate];
105 for ( int i = 0; i < numToCreate; i++ )
106 {
107
108 final int size = bytes * 1024;
109 final DiskTestObject tile = new DiskTestObject( Integer.valueOf( i ), new byte[size] );
110
111 final ICacheElement<Integer, DiskTestObject> element = new CacheElement<>( cacheName, tile.id, tile );
112 elements[i] = element;
113 }
114 return elements;
115 }
116
117
118
119
120
121
122
123
124 public static ICacheElement<Integer, DiskTestObject>[] createCacheElementsWithTestObjectsOfVariableSizes( final int numToCreate, final String cacheName )
125 {
126 @SuppressWarnings("unchecked")
127 final
128 ICacheElement<Integer, DiskTestObject>[] elements = new ICacheElement[numToCreate];
129 final Random random = new Random( 89 );
130 for ( int i = 0; i < numToCreate; i++ )
131 {
132 final int bytes = random.nextInt( 20 );
133
134 final int size = ( bytes + 4 ) * 1024;
135 final DiskTestObject tile = new DiskTestObject( Integer.valueOf( i ), new byte[size] );
136
137 final ICacheElement<Integer, DiskTestObject> element = new CacheElement<>( cacheName, tile.id, tile );
138 elements[i] = element;
139 }
140 return elements;
141 }
142
143 }