1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sf.ehcache;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21
22 import java.io.ByteArrayInputStream;
23 import java.io.ByteArrayOutputStream;
24 import java.io.IOException;
25 import java.io.ObjectInputStream;
26 import java.io.ObjectOutputStream;
27 import java.io.Serializable;
28 import java.util.HashMap;
29
30
31
32
33
34
35
36 public class ElementTest extends AbstractCacheTest {
37 private static final Log LOG = LogFactory.getLog(ElementTest.class.getName());
38
39
40
41
42
43
44
45
46
47
48 public void testSerializationPerformanceByteArray() throws CacheException {
49 Serializable key = "key";
50
51 ByteArrayOutputStream bout = new ByteArrayOutputStream();
52 for (int j = 0; j < 10000; j++) {
53 try {
54 bout.write("abcdefghijklmnopqrstv1234567890".getBytes());
55 } catch (IOException e) {
56 LOG.error("This should not happen");
57 }
58 }
59 byte[] value = bout.toByteArray();
60
61 Element element = new Element(key, value);
62 StopWatch stopWatch = new StopWatch();
63 for (int i = 0; i < 100; i++) {
64 element.getSerializedSize();
65 }
66 long elapsed = stopWatch.getElapsedTime() / 100;
67 LOG.info("In-memory size in bytes: " + element.getSerializedSize()
68 + " time to serialize in ms: " + elapsed);
69 assertTrue("Large object clone takes more than than 100ms", elapsed < 100);
70 }
71
72
73
74
75
76
77 public void testSerializationPerformanceJavaObjects() throws Exception {
78
79 HashMap map = new HashMap(10000);
80 for (int j = 0; j < 10000; j++) {
81 map.put("key" + j, new String[]{"adfdafs", "asdfdsafa", "sdfasdf"});
82 }
83 Element element = new Element("key1", map);
84 StopWatch stopWatch = new StopWatch();
85 for (int i = 0; i < 100; i++) {
86 element.getSerializedSize();
87 }
88 long elapsed = stopWatch.getElapsedTime() / 100;
89 LOG.info("In-memory size in bytes: " + element.getSerializedSize()
90 + " time to serialize in ms: " + elapsed);
91 assertTrue("Large object clone took more than 500ms", elapsed < 500);
92 }
93
94
95
96
97
98
99 public void testCalculateClonePerformanceJavaObjects() throws Exception {
100
101 HashMap map = new HashMap(10000);
102 for (int j = 0; j < 10000; j++) {
103 map.put("key" + j, new String[]{"adfdafs", "asdfdsafa", "sdfasdf"});
104 }
105 Element element = new Element("key1", map);
106 StopWatch stopWatch = new StopWatch();
107 for (int i = 0; i < 100; i++) {
108 element.clone();
109 }
110 long elapsed = stopWatch.getElapsedTime() / 100;
111 LOG.info("Time to clone object in ms: " + elapsed);
112 LOG.info("In-memory size in bytes: " + element.getSerializedSize()
113 + " time to clone in ms: " + elapsed);
114 assertTrue("Large object clone takes less than 1 second", elapsed < 1000);
115 }
116
117
118
119
120
121
122
123
124
125
126
127 public void testClonePerformanceByteArray() throws CacheException, CloneNotSupportedException {
128 Serializable key = "key";
129
130 byte[] value = getTestByteArray();
131
132 Element element = new Element(key, value);
133 StopWatch stopWatch = new StopWatch();
134 for (int i = 0; i < 100; i++) {
135 element.clone();
136 }
137 long elapsed = stopWatch.getElapsedTime() / 100;
138 LOG.info("In-memory size in bytes: " + element.getSerializedSize()
139 + " time to serialize in ms: " + elapsed);
140 assertTrue("Large object clone takes less than 130 milliseconds", elapsed < 180);
141 }
142
143
144 private byte[] getTestByteArray() {
145 ByteArrayOutputStream bout = new ByteArrayOutputStream();
146 for (int j = 0; j < 10000; j++) {
147 try {
148 bout.write("abcdefghijklmnopqrstv1234567890".getBytes());
149 } catch (IOException e) {
150 LOG.error("This should not happen");
151 }
152 }
153 return bout.toByteArray();
154
155 }
156
157
158
159
160
161
162
163 public void testDeserializationPerformance() throws IOException, ClassNotFoundException {
164
165 byte[] value = getTestByteArray();
166 Element element = new Element("test", value);
167
168 ByteArrayOutputStream bout = new ByteArrayOutputStream();
169 ObjectOutputStream oos = new ObjectOutputStream(bout);
170 oos.writeObject(element);
171 byte[] serializedValue = bout.toByteArray();
172 oos.close();
173 StopWatch stopWatch = new StopWatch();
174 for (int i = 0; i < 100; i++) {
175 ByteArrayInputStream bin = new ByteArrayInputStream(serializedValue);
176 ObjectInputStream ois = new ObjectInputStream(bin);
177 ois.readObject();
178 ois.close();
179 }
180 long elapsed = stopWatch.getElapsedTime() / 100;
181 LOG.info("In-memory size in bytes: " + serializedValue.length
182 + " time to deserialize in ms: " + elapsed);
183 assertTrue(elapsed < 30);
184 }
185
186
187
188
189
190 public void testObjectAccess() {
191 Object key = new Object();
192 Object value = new Object();
193 Element element = new Element(key, value);
194
195 assertEquals(key, element.getObjectKey());
196 assertEquals(value, element.getObjectValue());
197
198
199 try {
200 element.getKey();
201 } catch (CacheException e) {
202
203 }
204 assertEquals(value, element.getObjectValue());
205
206 }
207
208
209
210
211 public void testSerializableAccess() {
212 Serializable key = "";
213 Serializable value = "";
214 Element element = new Element(key, value);
215
216
217 assertEquals(key, element.getObjectKey());
218 assertEquals(value, element.getObjectValue());
219
220
221 assertEquals(key, element.getObjectKey());
222 assertEquals(value, element.getObjectValue());
223
224
225 }
226
227
228
229
230
231 public void testIsSerializable() {
232
233 Element element = new Element(null, null);
234 assertFalse(element.isKeySerializable());
235 assertFalse(element.isSerializable());
236
237
238 Element elementWithNullValue = new Element("1", null);
239 assertFalse(elementWithNullValue.isSerializable());
240
241
242 Object object = null;
243 assertFalse(object instanceof Serializable);
244
245 }
246
247
248
249
250 public void testEquals() {
251
252 Element element = new Element("key", "value");
253 assertFalse(element.equals("dog"));
254 assertTrue(element.equals(element));
255 assertFalse(element.equals(null));
256 assertFalse(element.equals(new Element("cat", "hat")));
257 }
258
259
260
261
262 public void testFullConstructor() {
263
264 Element element = new Element("key", "value", 1L, 123L, 1234L, 12345L, 123456L, 1234567L);
265 assertEquals("key", element.getKey());
266 assertEquals("value", element.getValue());
267 assertEquals(1L, element.getVersion());
268 assertEquals(123L, element.getCreationTime());
269 assertEquals(1234L, element.getLastAccessTime());
270 assertEquals(12345L, element.getNextToLastAccessTime());
271 assertEquals(123456L, element.getLastUpdateTime());
272 assertEquals(1234567L, element.getHitCount());
273
274 }
275
276
277 }