]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/testcases/org/simantics/databoard/tests/TestWriteable.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / testcases / org / simantics / databoard / tests / TestWriteable.java
1 /*******************************************************************************
2  *  Copyright (c) 2010 Association for Decentralized Information Management in
3  *  Industry THTH ry.
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
8  *
9  *  Contributors:
10  *      VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.databoard.tests;
13
14 import java.io.BufferedInputStream;
15 import java.io.BufferedOutputStream;
16 import java.io.File;
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;
22
23 import junit.framework.TestCase;
24
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;
30
31 public class TestWriteable extends TestCase {
32
33         public static final int ITERATIONS = 50;
34         
35         public void testWriteRead() 
36         throws FileNotFoundException, IOException 
37         {               
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");
42                 
43                 tmp1.deleteOnExit();
44                 tmp2.deleteOnExit();
45                 tmp3.deleteOnExit();
46                 tmp4.deleteOnExit();
47                 
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 );
52                 
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);
58                 
59                 System.out.println();
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);
64
65                 write4.close();
66                 
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 );
71                 
72                 System.out.println("Read Times:");
73                 time2 = measureRead(read2);
74                 time3 = measureRead(read3);                             
75                 time1 = measureRead(read1);
76                 time4 = measureRead(read4);                             
77                 System.out.println();
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);
82                 
83                 // re-write
84                 write1.position(0);
85                 time1 = measureWrite(write1);           
86                 // Re-read
87                 read1.position(0);
88                 time2 = measureRead(read1);
89                 System.out.println("\nRandomAccessFile (again): write="+time1+", read="+time2);
90                 
91         }
92         
93         static long measureWrite(BinaryWriteable write) 
94         throws IOException
95         {               
96                 System.gc();
97                 byte[] data = new byte[1024];
98                 for (int i=0; i<data.length; i++)
99                         data[i] = (byte) i;
100                 
101                 long startTime = System.currentTimeMillis();            
102                 for (int iter=0; iter<ITERATIONS; iter++)
103                 {
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++)
110                                 write.writeFloat(i);
111                         for (int i=0; i<256; i++)
112                                 write.writeInt(i);
113                         for (int i=0; i<256; i++)
114                                 write.writeShort((short)i);
115                         write.write(data);                      
116                 }               
117                 write.flush();
118                 long elapsedTime = System.currentTimeMillis() - startTime;              
119                 
120                 return elapsedTime;
121         }
122         
123         static long measureRead(BinaryReadable read) 
124         throws IOException
125         {               
126                 System.gc();
127                 byte[] data = new byte[1024];
128                 
129                 long startTime = System.currentTimeMillis();            
130                 for (int iter=0; iter<ITERATIONS; iter++)
131                 {
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);
146                 }               
147                 long elapsedTime = System.currentTimeMillis() - startTime;
148                 
149                 return elapsedTime;
150         }
151         
152         
153 }
154