001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.test;
022:
023: import java.io.*;
024:
025: import com.db4o.*;
026: import com.db4o.query.*;
027:
028: /**
029: *
030: */
031: public class MaxSize {
032:
033: public String index;
034:
035: private static final int SIZE = 1000000;
036: private static final int COMMIT_INTERVAL = 10000;
037: private static final int QUERIES = 1000;
038: private static final String FILE = "maxSize.yap";
039:
040: public static void main(String[] args) {
041:
042: // make sure there is an index on the "index" field
043: Db4o.configure().objectClass(MaxSize.class)
044: .objectField("index").indexed(true);
045:
046: store();
047: query();
048: }
049:
050: public MaxSize() {
051:
052: }
053:
054: public MaxSize(String index) {
055: this .index = index;
056: }
057:
058: public static void store() {
059: new File(FILE).delete();
060: ObjectContainer objectContainer = Db4o.openFile(FILE);
061: long start = System.currentTimeMillis();
062: long elapsed;
063: for (int i = 1; i <= SIZE; i++) {
064: objectContainer.set(new MaxSize("" + i));
065: if (((double) i / (double) COMMIT_INTERVAL) == i
066: / COMMIT_INTERVAL) {
067: objectContainer.commit();
068: objectContainer.ext().purge();
069: elapsed = System.currentTimeMillis() - start;
070: System.out.println("Committed " + i + " from " + SIZE
071: + " elapsed " + elapsed + "ms");
072: }
073: }
074: objectContainer.close();
075: elapsed = System.currentTimeMillis() - start;
076: System.out.println("\nTime to store " + SIZE + " objects:\n"
077: + elapsed + "ms");
078: }
079:
080: public static void query() {
081:
082: ObjectContainer objectContainer = Db4o.openFile(FILE);
083: long time = System.currentTimeMillis();
084: for (int i = 1; i <= QUERIES; i++) {
085: Query q = objectContainer.query();
086: q.constrain(MaxSize.class);
087: q.descend("index").constrain("" + i);
088: q.execute();
089: }
090: time = System.currentTimeMillis() - time;
091: objectContainer.close();
092: System.out.println("\nTime for " + QUERIES
093: + " queries against an indexed field in " + SIZE
094: + " objects:\n" + time + "ms");
095: double perQuery = (double) time / (double) 1000;
096: System.out.println("\nTime per query:\n" + perQuery + "ms");
097: int perSecond = (int) ((double) 1000 / perQuery);
098: System.out.println("\nQueries per second:\n" + perSecond);
099: }
100:
101: }
|