1 /**
2 * Copyright 2003-2006 Greg Luck
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
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 * Note these tests need a live network interface running in multicast mode to work
31 *
32 * @author <a href="mailto:gluck@thoughtworks.com">Greg Luck</a>
33 * @version $Id: PayloadUtilTest.java 28 2006-04-15 05:12:32Z gregluck $
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 * setup test
43 * @throws Exception
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 * Shuts down the cachemanager
52 * @throws Exception
53 */
54 protected void tearDown() throws Exception {
55 manager.shutdown();
56 }
57
58 /**
59 * The maximum Ethernet MTU is 1500 bytes.
60 * <p/>
61 * We want to be able to work with 100 caches
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 * 376 µs per one gzipping each time.
76 * .1 µs if we compare hashCodes on the String and only gzip as necessary.
77 * @throws IOException
78 * @throws InterruptedException
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(10);
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 * 169 µs per one.
102 * @throws IOException
103 * @throws InterruptedException
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(10);
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 }