KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > test > performance > RunIOBench


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.test.performance;
22
23 import java.io.*;
24
25 import com.db4o.io.*;
26
27 /**
28  *
29  * @exclude
30  */

31 public class RunIOBench {
32     public static void main(String JavaDoc[] args) throws IOException {
33
34         RandomAccessFile recordedIn = new RandomAccessFile(Util.BENCHFILE+".1", "rw");
35         new File(Util.DBFILE).delete();
36         IoAdapter testadapt = new RandomAccessFileAdapter().open(Util.DBFILE,
37                 false, 1024);
38
39         // IoAdapter testadapt = new MemoryIoAdapter().open(Util.DBFILE, false,
40
// 1024);
41
// IoAdapter testadapt = new SymbianIoAdapter().open(Util.DBFILE,
42
// false, 1024);
43
long bench = benchmark(recordedIn, testadapt);
44         System.out.println("tested IOAdapter: ["
45                 + testadapt.getClass().getName() + "]\nspeed: " + bench);
46     }
47
48     public static long benchmark(RandomAccessFile recordedIn, IoAdapter adapter)
49             throws IOException {
50         byte[] defaultData = new byte[1000];
51         long start = System.currentTimeMillis();
52         int runs = 0;
53         try {
54             while (true) {
55                 runs++;
56                 char type = recordedIn.readChar();
57                 if (type == 'q') {
58                     break;
59                 }
60                 if (type == 'f') {
61                     adapter.sync();
62                     continue;
63                 }
64                 long pos = recordedIn.readLong();
65                 int length = recordedIn.readInt();
66                 adapter.seek(pos);
67                 byte[] data = (length <= defaultData.length ? defaultData
68                         : new byte[length]);
69                 switch (type) {
70                 case 'r':
71                     adapter.read(data, length);
72                     break;
73                 case 'w':
74                     adapter.write(data, length);
75                     break;
76                 default:
77                     throw new IllegalArgumentException JavaDoc("Unknown access type: "
78                             + type);
79                 }
80             }
81         } finally {
82             recordedIn.close();
83             adapter.close();
84         }
85         // System.err.println(runs);
86
return System.currentTimeMillis() - start;
87     }
88 }
Popular Tags