1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sf.ehcache.distribution;
18
19 import junit.framework.TestCase;
20 import net.sf.ehcache.AbstractCacheTest;
21 import net.sf.ehcache.CacheManager;
22 import net.sf.ehcache.StopWatch;
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25
26 import java.io.IOException;
27
28
29
30
31
32
33
34
35
36 public class PayloadUtilTest extends TestCase {
37
38 private static final Log LOG = LogFactory.getLog(PayloadUtilTest.class.getName());
39 private CacheManager manager;
40
41
42
43
44
45 protected void setUp() throws Exception {
46 String fileName = AbstractCacheTest.TEST_CONFIG_DIR + "ehcache-big.xml";
47 manager = new CacheManager(fileName);
48 }
49
50
51
52
53
54 protected void tearDown() throws Exception {
55 manager.shutdown();
56 }
57
58
59
60
61
62
63 public void testMaximumDatagram() throws IOException {
64 String payload = createReferenceString();
65
66 final byte[] compressed = PayloadUtil.gzip(payload.getBytes());
67
68 int length = compressed.length;
69 LOG.info("gzipped size: " + length);
70 assertTrue("Heartbeat too big for one Datagram " + length, length <= 1500);
71
72 }
73
74
75
76
77
78
79
80 public void testGzipSanityAndPerformance() throws IOException, InterruptedException {
81 String payload = createReferenceString();
82
83 for (int i = 0; i < 10; i++) {
84 byte[] compressed = PayloadUtil.gzip(payload.getBytes());
85
86 assertTrue(compressed.length > 300);
87 Thread.sleep(20);
88 }
89 int hashCode = payload.hashCode();
90 StopWatch stopWatch = new StopWatch();
91 for (int i = 0; i < 10000; i++) {
92 if (hashCode != payload.hashCode()) {
93 PayloadUtil.gzip(payload.getBytes());
94 }
95 }
96 long elapsed = stopWatch.getElapsedTime();
97 LOG.info("Gzip took " + elapsed / 10F + " µs");
98 }
99
100
101
102
103
104
105 public void testUngzipPerformance() throws IOException, InterruptedException {
106 String payload = createReferenceString();
107 int length = payload.toCharArray().length;
108 byte[] original = payload.getBytes();
109 int byteLength = original.length;
110 assertEquals(length, byteLength);
111 byte[] compressed = PayloadUtil.gzip(original);
112
113 for (int i = 0; i < 10; i++) {
114 byte[] uncompressed = PayloadUtil.ungzip(compressed);
115 uncompressed.hashCode();
116 assertEquals(original.length, uncompressed.length);
117 Thread.sleep(20);
118 }
119 StopWatch stopWatch = new StopWatch();
120 for (int i = 0; i < 10000; i++) {
121 PayloadUtil.ungzip(compressed);
122 }
123 long elapsed = stopWatch.getElapsedTime();
124 LOG.info("Ungzip took " + elapsed / 10000F + " µs");
125 }
126
127
128
129 private String createReferenceString() {
130
131 String[] names = manager.getCacheNames();
132 String urlBase = "//localhost.localdomain:12000/";
133 StringBuffer buffer = new StringBuffer();
134 for (int i = 0; i < names.length; i++) {
135 String name = names[i];
136 buffer.append(urlBase);
137 buffer.append(name);
138 buffer.append("|");
139 }
140 String payload = buffer.toString();
141 return payload;
142 }
143
144
145
146 }