]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.fastlz/testcases/org/simantics/fastlz/FastLZBasicTests.java
Merge commit 'd1a82fe'
[simantics/platform.git] / bundles / org.simantics.fastlz / testcases / org / simantics / fastlz / FastLZBasicTests.java
1 /*******************************************************************************\r
2 t * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.fastlz;
13
14 import static org.junit.Assert.fail;\r
15 \r
16 import java.io.BufferedInputStream;\r
17 import java.io.BufferedOutputStream;\r
18 import java.io.File;\r
19 import java.io.FileInputStream;\r
20 import java.io.FileOutputStream;\r
21 import java.io.IOException;\r
22 import java.io.InputStream;\r
23 import java.io.OutputStream;\r
24 import java.util.Arrays;\r
25 \r
26 import junit.framework.Assert;\r
27 \r
28 import org.junit.BeforeClass;\r
29 import org.junit.Test;\r
30
31 /**\r
32  * @author Tuukka Lehtonen\r
33  */\r
34 @SuppressWarnings("deprecation")\r
35 public class FastLZBasicTests {\r
36 \r
37     static File testData1;\r
38     static File testData2;\r
39     static File testDataPrime;\r
40 \r
41     @BeforeClass\r
42     public static void initialize() throws IOException {\r
43         FastLZ.initialize(null);\r
44 \r
45         testData1 = new File("uncompressed.data");\r
46         writeTestData(testData1, 10000000);\r
47         testData2 = new File("uncompressed-small.data");\r
48         writeTestData(testData2, 100000);\r
49         testDataPrime = new File("uncompressed-prime.data");\r
50         writeTestData(testDataPrime, 1000);\r
51 \r
52         System.out.println("test data directory: " + testData1.getAbsolutePath());\r
53 \r
54 //        File javaFlz = new File("compressed-prime.data.native.flz");\r
55 //        for (int i = 0; i < 100; ++i) {\r
56 //            compressFlzJava(testDataPrime, javaFlz);\r
57 //        }\r
58     }\r
59 \r
60     @Test\r
61     public void validateCompress() throws IOException {\r
62         validateCompress(testData1);\r
63     }\r
64 \r
65     private void validateCompress(File testData) throws IOException {\r
66         System.out.println("==== validateCompress(" + testData.getName() + ") ====");\r
67 \r
68         File nativeFlz = new File("compressed.data.native.flz");\r
69         long nativeCompressedSize = compressFlzNative(testData, nativeFlz);\r
70         System.out.println("native compressed size: " + nativeCompressedSize);\r
71 \r
72         // Need to prime JVM JIT by performing multiple passes\r
73         File javaFlz = new File("compressed.data.java.flz");\r
74         long javaCompressedSize = compressFlzJava(testData, javaFlz);\r
75         System.out.println("java compressed size: " + javaCompressedSize);\r
76 \r
77         Assert.assertEquals(nativeCompressedSize, javaCompressedSize);\r
78 \r
79         System.out.println("Comparing compressed outputs...");\r
80         compareFiles(nativeFlz, javaFlz);\r
81         System.out.println("Compressed outputs match.");\r
82 \r
83         File decompressedNativeFlz = new File("decompressed.data.native.flz");\r
84         File decompressedJavaFlz = new File("decompressed.data.java.flz");\r
85         decompressFlzNative(nativeFlz, decompressedNativeFlz);\r
86         decompressFlzNative(javaFlz, decompressedJavaFlz);\r
87         compareFiles(decompressedJavaFlz, decompressedNativeFlz);\r
88         compareFiles(decompressedNativeFlz, testData);\r
89     }\r
90 \r
91 //    @Test\r
92 //    public void validateCompressSmall() throws IOException {\r
93 //        File nativeFlz = new File("compressed-small.data.native.flz");\r
94 //        long nativeCompressedSize = compressFlzNative(testData2, nativeFlz);\r
95 //        System.out.println("native compressed size: " + nativeCompressedSize);\r
96 //\r
97 //        // Need to prime JVM JIT by performing multiple passes\r
98 //        File javaFlz = new File("compressed-small.data.java.flz");\r
99 //        long javaCompressedSize = compressFlzJava(testData2, javaFlz);\r
100 //        System.out.println("java compressed size: " + javaCompressedSize);\r
101 //\r
102 //        Assert.assertEquals(nativeCompressedSize, javaCompressedSize);\r
103 //\r
104 //        System.out.println("Comparing compressed outputs...");\r
105 //        compareFiles(nativeFlz, javaFlz);\r
106 //        System.out.println("Compressed outputs match.");\r
107 //    }\r
108 \r
109 //    @Test\r
110 //    public void testNativeCompressPerformance() throws IOException {\r
111 //        File javaFlz = new File("compressed.data.native.flz");\r
112 //        for (int i = 0; i < 5; ++i) {\r
113 //            compressFlzNative(testData1, javaFlz);\r
114 //        }\r
115 //    }\r
116 //\r
117 //    @Test\r
118 //    public void testJavaCompressPerformance() throws IOException {\r
119 //        // Need to prime JVM JIT by performing multiple passes\r
120 //        File javaFlz = new File("compressed.data.java.flz");\r
121 //        for (int i = 0; i < 5; ++i) {\r
122 //            compressFlzJava(testData1, javaFlz);\r
123 //        }\r
124 //    }\r
125 \r
126 //    @Test\r
127 //    public void testCompressNative() throws IOException {\r
128 //        File flz = new File("compressed.data.native.flz");\r
129 //        for (int i = 0; i < 5; ++i) {\r
130 //            long compressedSize = compressFlzNative(testData1, flz);\r
131 //            System.out.println("native compressed size: " + compressedSize);\r
132 //        }\r
133 //    }\r
134 //\r
135 //    @Test\r
136 //    public void testCompressionJava() throws IOException {\r
137 //        // Need to prime JVM JIT by performing multiple passes\r
138 //        File flz = new File("compressed.data.java.flz");\r
139 //        for (int i = 0; i < 5; ++i) {\r
140 //            long compressedSize = compressFlzJava(testData1, flz);\r
141 //            System.out.println("java compressed size: " + compressedSize);\r
142 //        }\r
143 //    }\r
144 \r
145     @SuppressWarnings("unused")\r
146     @Test\r
147     public void testDecompress() throws IOException {\r
148         File nativeFlz = new File("compressed.data.native.flz");\r
149         long nativeCompressedSize = compressFlzNative(testData1, nativeFlz);\r
150         System.out.println("native compressed size: " + nativeCompressedSize);\r
151 \r
152         // Need to prime JVM JIT by performing multiple passes\r
153         File javaFlz = new File("compressed.data.java.flz");\r
154         long javaCompressedSize = compressFlzJava(testData1, javaFlz);\r
155         System.out.println("java compressed size: " + javaCompressedSize);\r
156 \r
157         Assert.assertEquals(nativeCompressedSize, javaCompressedSize);\r
158 \r
159         System.out.println("Comparing compressed outputs...");\r
160         compareFiles(nativeFlz, javaFlz);\r
161         System.out.println("Compressed outputs match.");\r
162 \r
163         File java1 = new File("java-compressed.data.decompressed-with-native");\r
164         long nativeDecompressedSize = decompressFlzNative(javaFlz, java1);\r
165         System.out.println("Comparing native-decompressed output...");\r
166         compareFiles(testData1, java1);\r
167         System.out.println("Native-decompressed output matches original.");\r
168 \r
169         File java2 = new File("java-compressed.data.decompressed-with-java");\r
170         long javaDecompressedSize = decompressFlzJava(javaFlz, java2);\r
171         System.out.println("Comparing java-decompressed output...");\r
172         compareFiles(testData1, java2);\r
173         System.out.println("Java-decompressed output matches original.");\r
174 \r
175         for (int i = 0; i < 5; ++i)\r
176             decompressFlz(javaFlz, FastLZ.read(javaFlz), java1, NullOutputStream.INSTANCE);\r
177         for (int i = 0; i < 5; ++i)\r
178             decompressFlz(javaFlz, FastLZJava.read(javaFlz), java2, NullOutputStream.INSTANCE);\r
179     }\r
180 \r
181     @Test
182     public void testDecompressCluster() {
183         fail("Not yet implemented");
184     }
185 \r
186     static void compareFiles(File file1, File file2) throws IOException {\r
187         InputStream in1 = new BufferedInputStream(new FileInputStream(file1));\r
188         InputStream in2 = new BufferedInputStream(new FileInputStream(file2));\r
189         try {\r
190             int offset = 0;\r
191             while (true) {\r
192                 int b1 = in1.read();\r
193                 int b2 = in2.read();\r
194                 if (b1 == -1 && b2 == -1)\r
195                     return;\r
196                 if (b1 == -1)\r
197                     fail("EOF reached in file1 " + file1.getName() + " but not in file2 " + file2.getName());\r
198                 if (b2 == -1)\r
199                     fail("EOF reached in file1 " + file1.getName() + " but not in file2 " + file2.getName());\r
200                 if (b1 != b2)\r
201                     fail("bytes at offset " + offset + " do not match: " + b1 + " vs. " + b2);\r
202                 ++offset;\r
203             }\r
204         } finally {\r
205             in1.close();\r
206             in2.close();\r
207         }\r
208     }\r
209 \r
210     public static void writeTestData(File file, int rows) throws IOException {\r
211         if (file.exists())\r
212             return;\r
213 \r
214         System.out.println("writing test data...");\r
215         OutputStream stream = new FileOutputStream(file);\r
216         for (int i = 0; i < rows; ++i)\r
217             stream.write((Integer.toString(i) + "\n").getBytes());\r
218         stream.close();\r
219         System.out.println("wrote " + file.length() + " bytes of test data.");\r
220     }\r
221 \r
222     /**\r
223      * @param source\r
224      * @param flz\r
225      * @return compressed size in bytes\r
226      * @throws IOException\r
227      */\r
228     static long compressFlzJava(File source, File flz) throws IOException {\r
229         return compressFlz(source, flz, FastLZJava.write(flz));\r
230     }\r
231 \r
232     /**\r
233      * @param source\r
234      * @param flz\r
235      * @return compressed size in bytes\r
236      * @throws IOException\r
237      */\r
238     static long compressFlzNative(File source, File flz) throws IOException {\r
239         return compressFlz(source, flz, FastLZ.write(flz));\r
240     }\r
241 \r
242     /**\r
243      * @param source\r
244      * @param flz\r
245      * @return compressed size in bytes\r
246      * @throws IOException\r
247      */\r
248     static long compressFlz(File source, File flz, OutputStream flzOutput) throws IOException {\r
249         System.out.println("compressFlz(" + source + ", " + flz + ")");\r
250         InputStream input = new BufferedInputStream(new FileInputStream(source));\r
251         copy(input, flzOutput);\r
252         input.close();\r
253         flzOutput.close();\r
254         long compressed = flz.length();\r
255         System.out.println("Wrote " + compressed + " compressed bytes");\r
256         return compressed;\r
257     }\r
258 \r
259     /**\r
260      * @param source\r
261      * @param flz\r
262      * @return compressed size in bytes\r
263      * @throws IOException\r
264      */\r
265     static long decompressFlzJava(File flz, File dest) throws IOException {\r
266         return decompressFlz(flz, FastLZJava.read(flz), dest);\r
267     }\r
268 \r
269     /**\r
270      * @param source\r
271      * @param flz\r
272      * @return compressed size in bytes\r
273      * @throws IOException\r
274      */\r
275     static long decompressFlzNative(File flz, File dest) throws IOException {\r
276         return decompressFlz(flz, FastLZ.read(flz), dest);\r
277     }\r
278 \r
279     /**\r
280      * @param source\r
281      * @param flz\r
282      * @return compressed size in bytes\r
283      * @throws IOException\r
284      */\r
285     static long decompressFlz(File source, InputStream flzInput, File dest) throws IOException {\r
286         System.out.println("decompressFlz(" + source + ", " + dest + ")");\r
287         OutputStream output = new BufferedOutputStream(new FileOutputStream(dest));\r
288         copy(flzInput, output);\r
289         flzInput.close();\r
290         output.close();\r
291         long decompressed = dest.length();\r
292         System.out.println("Wrote " + decompressed + " decompressed bytes");\r
293         return decompressed;\r
294     }\r
295 \r
296     /**\r
297      * @param source\r
298      * @param flz\r
299      * @return compressed size in bytes\r
300      * @throws IOException\r
301      */\r
302     static long decompressFlz(File source, InputStream flzInput, File dest, OutputStream destStream) throws IOException {\r
303         System.out.println("decompressFlz(" + source + ", " + dest + ")");\r
304         copy(flzInput, destStream);\r
305         flzInput.close();\r
306         destStream.close();\r
307         long decompressed = dest.length();\r
308         System.out.println("Wrote " + decompressed + " decompressed bytes");\r
309         return decompressed;\r
310     }\r
311 \r
312     /**\r
313      * Copy the content of the input stream into the output stream, using a temporary\r
314      * byte array buffer whose size is defined by {@link #IO_BUFFER_SIZE}.\r
315      *\r
316      * @param in The input stream to copy from.\r
317      * @param out The output stream to copy to.\r
318      *\r
319      * @throws IOException If any error occurs during the copy.\r
320      */\r
321     private static final int IO_BUFFER_SIZE = 128 * 1024;\r
322 \r
323     public static long copy(InputStream in, OutputStream out) throws IOException {\r
324         byte[] b = new byte[IO_BUFFER_SIZE];\r
325         int read;\r
326         long total = 0;\r
327         long start = System.nanoTime();\r
328         while (true) {\r
329             read = in.read(b);\r
330             if (read < 0)\r
331                 break;\r
332             total += read;\r
333             //System.out.println("read " + read + " bytes, " + total + " bytes in total");\r
334             out.write(b, 0, read);\r
335         }\r
336 \r
337         long end = System.nanoTime();\r
338         double totalmb = total/(1024.0*1024.0);\r
339         double time = (end-start)*1e-9;\r
340         double rate = totalmb / time;\r
341         System.out.format("Transferred %d bytes (%.3f Mbytes) in %f seconds (%.3f MB/s)\n", total, totalmb, time, rate);\r
342 \r
343         return total;\r
344     }\r
345 \r
346     @SuppressWarnings("unused")\r
347     private boolean checksumsEqual(File f1, File f2) throws IOException {\r
348         byte[] s1 = ChecksumUtil.computeSum(f1);\r
349         byte[] s2 = ChecksumUtil.computeSum(f2);\r
350         return Arrays.equals(s1, s2);\r
351     }\r
352 \r
353 }