KickJava   Java API By Example, From Geeks To Geeks.

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


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 MaxSize {
32
33     public String JavaDoc index;
34
35     private static final int SIZE = 1000000;
36     private static final int COMMIT_INTERVAL = 10000;
37     private static final int QUERIES = 1000;
38     private static final String JavaDoc FILE = "maxSize.yap";
39     
40     public static void main(String JavaDoc[] args) {
41         
42         // make sure there is an index on the "index" field
43
Db4o.configure().objectClass(MaxSize.class).objectField("index").indexed(true);
44         
45         store();
46         query();
47     }
48
49     public MaxSize() {
50
51     }
52
53     public MaxSize(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         long start = System.currentTimeMillis();
61         long elapsed;
62         for (int i = 1; i <= SIZE; i++) {
63             objectContainer.set(new MaxSize("" + i));
64             if (((double) i / (double) COMMIT_INTERVAL) == i / COMMIT_INTERVAL) {
65                 objectContainer.commit();
66                 objectContainer.ext().purge();
67                 elapsed = System.currentTimeMillis() - start;
68                 System.out.println("Committed " + i + " from " + SIZE + " elapsed " + elapsed + "ms");
69             }
70         }
71         objectContainer.close();
72         elapsed = System.currentTimeMillis() - start;
73         System.out.println("\nTime to store " + SIZE + " objects:\n" + elapsed + "ms");
74     }
75     
76     public static void query() {
77         
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(MaxSize.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     
95     
96 }
Popular Tags