1 /*******************************************************************************
2 * Copyright (c) 2010 Association for Decentralized Information Management in
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.databoard.tests;
14 import java.io.BufferedInputStream;
15 import java.io.BufferedOutputStream;
17 import java.io.FileInputStream;
18 import java.io.FileNotFoundException;
19 import java.io.FileOutputStream;
20 import java.io.IOException;
21 import java.io.RandomAccessFile;
23 import junit.framework.TestCase;
25 import org.simantics.databoard.util.binary.BinaryFile;
26 import org.simantics.databoard.util.binary.BinaryReadable;
27 import org.simantics.databoard.util.binary.BinaryWriteable;
28 import org.simantics.databoard.util.binary.InputStreamReadable;
29 import org.simantics.databoard.util.binary.OutputStreamWriteable;
31 public class TestWriteable extends TestCase {
33 public static final int ITERATIONS = 50;
35 public void testWriteRead()
36 throws FileNotFoundException, IOException
38 File tmp1 = File.createTempFile("xyz1", "tmp");
39 File tmp2 = File.createTempFile("xyz2", "tmp");
40 File tmp3 = File.createTempFile("xyz3", "tmp");
41 File tmp4 = File.createTempFile("xyz4", "tmp");
48 BinaryFile write1 = new BinaryFile( new RandomAccessFile(tmp1, "rw") );
49 BinaryWriteable write2 = new OutputStreamWriteable( new FileOutputStream( tmp2, true ) );
50 BinaryWriteable write3 = new OutputStreamWriteable( new BufferedOutputStream( new FileOutputStream( tmp3, true ) ) );
51 BinaryFile write4 = new BinaryFile( tmp4 );
53 System.out.println("Write Times:");
54 long time2 = measureWrite(write2);
55 long time3 = measureWrite(write3);
56 long time1 = measureWrite(write1);
57 long time4 = measureWrite(write4);
60 System.out.println("RandomAccessFile: "+time1);
61 System.out.println("FileOutputStream: "+time2);
62 System.out.println("FileOutputStream (Buffered) : "+time3);
63 System.out.println("BinaryFile : "+time4);
67 BinaryFile read1 = new BinaryFile( new RandomAccessFile(tmp1, "r") );
68 BinaryReadable read2 = new InputStreamReadable( new FileInputStream( tmp2 ), tmp1.length() );
69 BinaryReadable read3 = new InputStreamReadable( new BufferedInputStream( new FileInputStream( tmp3 ) ), tmp1.length() );
70 BinaryFile read4 = new BinaryFile( tmp4 );
72 System.out.println("Read Times:");
73 time2 = measureRead(read2);
74 time3 = measureRead(read3);
75 time1 = measureRead(read1);
76 time4 = measureRead(read4);
78 System.out.println("RandomAccessFile: "+time1);
79 System.out.println("FileOutputStream: "+time2);
80 System.out.println("FileOutputStream (Buffered) : "+time3);
81 System.out.println("BinaryFile : "+time4);
85 time1 = measureWrite(write1);
88 time2 = measureRead(read1);
89 System.out.println("\nRandomAccessFile (again): write="+time1+", read="+time2);
93 static long measureWrite(BinaryWriteable write)
97 byte[] data = new byte[1024];
98 for (int i=0; i<data.length; i++)
101 long startTime = System.currentTimeMillis();
102 for (int iter=0; iter<ITERATIONS; iter++)
104 if (iter % 10==0) System.out.print(".");
105 for (int i=0; i<256; i++)
106 write.write((byte)i);
107 for (int i=0; i<256; i++)
108 write.writeDouble(i);
109 for (int i=0; i<256; i++)
111 for (int i=0; i<256; i++)
113 for (int i=0; i<256; i++)
114 write.writeShort((short)i);
118 long elapsedTime = System.currentTimeMillis() - startTime;
123 static long measureRead(BinaryReadable read)
127 byte[] data = new byte[1024];
129 long startTime = System.currentTimeMillis();
130 for (int iter=0; iter<ITERATIONS; iter++)
132 if (iter % 10==0) System.out.print(".");
133 for (int i=0; i<256; i++)
134 assertEquals((byte) i, read.readByte());
135 for (int i=0; i<256; i++)
136 assertEquals((double) i, read.readDouble());
137 for (int i=0; i<256; i++)
138 assertEquals((float) i, read.readFloat());
139 for (int i=0; i<256; i++)
140 assertEquals((int) i, read.readInt());
141 for (int i=0; i<256; i++)
142 assertEquals((short) i, read.readShort());
143 read.readFully(data);
144 for (int i=0; i<data.length; i++)
145 assertEquals(data[i], (byte) i);
147 long elapsedTime = System.currentTimeMillis() - startTime;