KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > util > io > spikes > SimpleIoBenchmark


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.util.io.spikes;
22
23 import java.io.File JavaDoc;
24
25 import com.db4o.Db4o;
26 import com.db4o.ObjectContainer;
27 import com.db4o.ObjectSet;
28 import com.db4o.io.IoAdapter;
29 import com.db4o.util.io.NIOFileAdapter;
30 import com.db4o.util.io.win32.Win32IoAdapter;
31
32 /**
33  * @exclude
34  */

35 public class SimpleIoBenchmark {
36     
37     private static final String JavaDoc DBFILENAME = "SimpleIoBenchmark.yap";
38     private static final int ITERATIONS = 10000;
39
40     public static void main(String JavaDoc[] args) {
41         
42         for (int i=0; i<3; ++i) {
43             System.out.println("*******************");
44             test("Default IO adapter", null);
45             test("NIOFileadapter", new NIOFileAdapter(1024*32, 16));
46             test("Win32IoAdapter", new Win32IoAdapter());
47         }
48         
49     }
50     
51     private static void test(String JavaDoc name, IoAdapter adapter) {
52         if (null != adapter) {
53             Db4o.configure().io(adapter);
54         }
55         
56         long start = System.currentTimeMillis();
57         store();
58         query();
59         long elapsed = System.currentTimeMillis() - start;
60         
61         // System.gc is necessary to circumvent
62
// http://bugs.sun.com/bugdatabase/view_bug.do;:YfiG?bug_id=4724038
63
System.gc();
64
65         
66         System.out.println(name);
67         System.out.println("\t" + elapsed + "ms");
68         File JavaDoc file = new File JavaDoc(DBFILENAME);
69         System.out.println("\tFile size is " + file.length() + " bytes.");
70         System.out.println("\t" + file.length()/elapsed + " bytes/ms");
71         if (!file.delete()) {
72             System.err.println("Unable to delete '" + DBFILENAME + "'");
73         }
74     }
75     
76
77     private static void query() {
78         ObjectContainer db = Db4o.openFile(DBFILENAME);
79         try {
80             ObjectSet set = db.get(TestDummy.class);
81             if (ITERATIONS != set.size()) {
82                 System.err.println("Expected: " + ITERATIONS + ", actual: " + set.size());
83             }
84         } finally {
85             db.close();
86         }
87     }
88
89     private static void store() {
90         
91         ObjectContainer db = Db4o.openFile(DBFILENAME);
92         try {
93             for (int i=0; i<ITERATIONS; ++i) {
94                 db.set(new TestDummy("Dummy " + i));
95                 if (0 == i % 10) {
96                     db.commit();
97                 }
98             }
99         } finally {
100             db.close();
101         }
102     }
103 }
Popular Tags