KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
26 import com.db4o.config.*;
27 import com.db4o.io.*;
28 import com.db4o.query.*;
29
30
31 public class SimplePerformanceBenchmark {
32     
33     private static int COUNT = 1000;
34     
35     private static int DEPTH = 3;
36     
37     private static boolean CLIENT_SERVER = false;
38     
39     private static boolean TCP = true;
40     
41     private static final String JavaDoc FILE = "sip.yap";
42     
43     private static final int PORT = 4477;
44     
45     
46     private ObjectContainer objectContainer;
47     
48     private ObjectServer objectServer;
49     
50     
51     private long startTime;
52     
53     
54     public static void main(String JavaDoc[] arguments) {
55         new SimplePerformanceBenchmark().run();
56     }
57     
58     private void run(){
59         
60         clean();
61         
62         configure();
63         
64         open();
65         store();
66         close();
67         
68         open();
69         delete();
70         close();
71     }
72     
73     private void clean(){
74         new File(FILE).delete();
75     }
76     
77     private void configure(){
78         Configuration config = Db4o.configure();
79         config.lockDatabaseFile(false);
80         config.weakReferences(false);
81         config.io(new MemoryIoAdapter());
82         config.flushFileBuffers(false);
83         config.singleThreadedClient(true);
84     }
85     
86     private void store(){
87         startTimer();
88         for (int i = 0; i < COUNT ;i++) {
89             Item item = new Item("load", null);
90             for (int j = 1; j < DEPTH; j++) {
91                 item = new Item("load", item);
92             }
93             objectContainer.set(item);
94         }
95         objectContainer.commit();
96         stopTimer("Store "+ totalObjects() + " objects");
97     }
98     
99     private void delete(){
100         startTimer();
101         Query q = objectContainer.query();
102         q.constrain(Item.class);
103         ObjectSet objectSet = q.execute();
104         while(objectSet.hasNext()){
105             objectContainer.delete(objectSet.next());
106         }
107         objectContainer.commit();
108         stopTimer("Delete "+ totalObjects() + " objects");
109     }
110     
111     private int totalObjects(){
112         return COUNT * DEPTH;
113     }
114     
115     private void open(){
116         if(CLIENT_SERVER){
117             int port = TCP ? PORT : 0;
118             String JavaDoc user = "db4o";
119             String JavaDoc password = user;
120             objectServer = Db4o.openServer(FILE, port);
121             objectServer.grantAccess(user, password);
122             try {
123                 objectContainer = TCP ?
124                     Db4o.openClient("localhost", port, user, password) :
125                     objectServer.openClient();
126             } catch (IOException e) {
127                 e.printStackTrace();
128             }
129         } else{
130             objectContainer = Db4o.openFile(FILE);
131         }
132     }
133     
134     private void close(){
135         objectContainer.close();
136         if(CLIENT_SERVER){
137             objectServer.close();
138         }
139     }
140     
141     private void startTimer(){
142         startTime = System.currentTimeMillis();
143     }
144     
145     private void stopTimer(String JavaDoc message){
146         long stop = System.currentTimeMillis();
147         long duration = stop - startTime;
148         System.out.println(message + ": " + duration + "ms");
149     }
150     
151     public static class Item {
152         
153         public String JavaDoc _name;
154         
155         public Item _child;
156         
157         public Item(){
158             
159         }
160         
161         public Item(String JavaDoc name, Item child){
162             _name = name;
163             _child = child;
164         }
165      
166     }
167
168 }
169
Popular Tags