1 /*------------------------------------------------------------------------------
2 Name: TestUtils.java
3 Project: xmlBlaster.org
4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
5 ------------------------------------------------------------------------------*/
6
7 package org.xmlBlaster.test.contrib;
8
9 import java.io.ByteArrayInputStream;
10 import java.io.ByteArrayOutputStream;
11 import java.io.IOException;
12 import java.io.InputStream;
13 import java.util.HashMap;
14
15 import org.custommonkey.xmlunit.XMLTestCase;
16 import org.custommonkey.xmlunit.XMLUnit;
17 import org.xmlBlaster.contrib.MomEventEngine;
18
19
20 /**
21 * TestUtils
22 * @author <a href="mailto:michele@laghi.eu">Michele Laghi</a>
23 */
24 public class TestUtils extends XMLTestCase {
25
26 public TestUtils() {
27 super();
28 XMLUnit.setIgnoreWhitespace(true);
29 }
30
31 /**
32 * Configure database access.
33 * @see TestCase#setUp()
34 */
35 protected void setUp() throws Exception {
36 super.setUp();
37 }
38
39 /*
40 * @see TestCase#tearDown()
41 */
42 protected void tearDown() throws Exception {
43 super.tearDown();
44 }
45
46 private String compress(int size, int sweeps, boolean isZip) {
47 isZip = true;
48 // long seed = System.currentTimeMillis();
49 long t0 = 0L;
50 long sumCompress = 0L;
51 long sumExpand = 0L;
52 long sumSize = 0L;
53 // Random random = new Random(seed);
54 HashMap map = new HashMap();
55 byte[] buf = new byte[size];
56 for (int i=0; i < sweeps; i++) {
57 //random.nextBytes(buf);
58 for (int j=0; j < buf.length; j++)
59 buf[i] = (byte)j;
60 t0 = System.currentTimeMillis();
61 byte[] compressed = MomEventEngine.compress(buf, map, 1, null);
62 sumCompress += System.currentTimeMillis() - t0;
63 t0 = System.currentTimeMillis();
64 byte[] copy = getContent(MomEventEngine.decompress(new ByteArrayInputStream(compressed), map));
65 sumExpand += System.currentTimeMillis() - t0;
66 sumSize += compressed.length;
67 assertEquals("Wrong size of copy of message", buf.length, copy.length);
68 for (int j=0; j < buf.length; j++) {
69 if (buf[j] != copy[j])
70 assertTrue("The position '" + j + "' is '" + copy[j] + "' but should be '" + buf[j] + "'", false);
71 }
72 }
73 double ratio = 1.0*sumSize/(1.0*size*sweeps);
74 double comp = 1.0 * sumCompress / sweeps;
75 double expand = 1.0 * sumExpand / sweeps;
76 double limit = 1024.0 * (1.0 - ratio) * size / (comp + expand);
77
78 String txt = "" + ratio + "\t" + comp + "\t" + expand + "\t" + limit + "";
79 return txt;
80 }
81
82 public static byte[] getContent(InputStream is) {
83 int ret = 0;
84 byte[] buf = new byte[1024];
85 ByteArrayOutputStream baos = new ByteArrayOutputStream();
86 try {
87 while ( (ret=is.read(buf)) > -1) {
88 baos.write(buf, 0, ret);
89 }
90 }
91 catch (IOException ex) {
92 ex.printStackTrace();
93 return new byte[0];
94 }
95 return baos.toByteArray();
96 }
97
98 public void testCompress() {
99 System.out.println("\t\tZIP\t\t\t\tGZIP\t\t");
100 System.out.println("SIZE\tratio\tcompress (ms)\texpand (ms)\tlimit (KB/s)\t\tratio\tcompress (ms)\texpand (ms)\tlimit (KB/s)");
101 /*
102 compressBoth(100, 1); // fail anyway
103 compressBoth(1000, 1000);
104 compressBoth(10000, 100);
105 compressBoth(100000, 10);
106 compressBoth(1000000, 1);
107 compressBoth(10000000, 1);
108 */
109 compressBoth(100, 1); // fail anyway
110 compressBoth(1000, 1);
111 compressBoth(10000, 1);
112 compressBoth(100000, 1);
113 compressBoth(1000000, 1);
114 compressBoth(10000000, 1);
115 }
116
117 private void compressBoth(int size, int sweeps) {
118 String txt1 = compress(size, sweeps, true);
119 String txt2 = compress(size, sweeps, false);
120 System.out.println("" + size + "\t" + txt1 + "\t" + txt2);
121 }
122
123 /**
124 * @param args
125 */
126 public static void main(String[] args) {
127 // junit.swingui.TestRunner.run(TestDbBasics.class);
128
129 TestUtils test = new TestUtils();
130 try {
131 test.setUp();
132 test.testCompress();
133 test.tearDown();
134 }
135 catch (Exception ex) {
136 ex.printStackTrace();
137 fail();
138 }
139 }
140
141 }
syntax highlighted by Code2HTML, v. 0.9.1