KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > test > IndexedFieldPerformance


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;
22
23 import java.io.*;
24
25 import com.db4o.*;
26 import com.db4o.query.*;
27
28 /**
29  *
30  */

31 public class IndexedFieldPerformance implements Serializable {
32
33     public String JavaDoc index;
34
35     public static final int SIZE = 50000;
36     public static final int COMMIT_STEP = 5000;
37     public static final int QUERIES = 1000;
38     public static final String JavaDoc FILE = "ifp.yap";
39     
40     public static void main(String JavaDoc[] args){
41         new Thread JavaDoc(new Runnable JavaDoc() {
42             public void run() {
43                 Db4o.configure().objectClass(IndexedFieldPerformance.class).objectField("index").indexed(true);
44                 store();
45                 query();
46             }
47         }).start();
48     }
49
50     public IndexedFieldPerformance() {
51     }
52
53     public IndexedFieldPerformance(String JavaDoc index) {
54         this.index = index;
55     }
56
57     public static void store() {
58         new File(FILE).delete();
59         ObjectContainer objectContainer = Db4o.openFile(FILE);
60         
61         long start = System.currentTimeMillis();
62         long elapsed;
63         for (int i = 1; i <= SIZE; i++) {
64             objectContainer.set(new IndexedFieldPerformance("" + i));
65             if (((double) i / (double) COMMIT_STEP) == i / COMMIT_STEP) {
66                 objectContainer.commit();
67                 objectContainer.ext().purge();
68                 elapsed = System.currentTimeMillis() - start;
69                 System.out.println("Committed " + i + " from " + SIZE + " elapsed " + elapsed + "ms");
70             }
71         }
72         elapsed = System.currentTimeMillis() - start;
73         objectContainer.close();
74         System.out.println("Time to store " + SIZE + " objects: " + elapsed + "ms");
75     }
76     
77     public static void query() {
78         ObjectContainer objectContainer = Db4o.openFile(FILE);
79         long time = System.currentTimeMillis();
80         for (int i = 1; i <= QUERIES; i++) {
81             Query q = objectContainer.query();
82             q.constrain(IndexedFieldPerformance.class);
83             q.descend("index").constrain("" + i);
84             q.execute();
85         }
86         time = System.currentTimeMillis() - time;
87         objectContainer.close();
88         System.out.println("\nTime for " + QUERIES + " queries against an indexed field in " + SIZE + " objects:\n" + time + "ms");
89         double perQuery = (double)time / (double)1000;
90         System.out.println("\nTime per query:\n" + perQuery + "ms");
91         int perSecond = (int)((double)1000 / perQuery);
92         System.out.println("\nQueries per second:\n" + perSecond);
93     }
94 }
Popular Tags