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 IndexedFieldPerformance implements Serializable {
032:
033: public String index;
034:
035: public static final int SIZE = 50000;
036: public static final int COMMIT_STEP = 5000;
037: public static final int QUERIES = 1000;
038: public static final String FILE = "ifp.yap";
039:
040: public static void main(String[] args) {
041: new Thread(new Runnable() {
042: public void run() {
043: Db4o.configure().objectClass(
044: IndexedFieldPerformance.class).objectField(
045: "index").indexed(true);
046: store();
047: query();
048: }
049: }).start();
050: }
051:
052: public IndexedFieldPerformance() {
053: }
054:
055: public IndexedFieldPerformance(String index) {
056: this .index = index;
057: }
058:
059: public static void store() {
060: new File(FILE).delete();
061: ObjectContainer objectContainer = Db4o.openFile(FILE);
062:
063: long start = System.currentTimeMillis();
064: long elapsed;
065: for (int i = 1; i <= SIZE; i++) {
066: objectContainer.set(new IndexedFieldPerformance("" + i));
067: if (((double) i / (double) COMMIT_STEP) == i / COMMIT_STEP) {
068: objectContainer.commit();
069: objectContainer.ext().purge();
070: elapsed = System.currentTimeMillis() - start;
071: System.out.println("Committed " + i + " from " + SIZE
072: + " elapsed " + elapsed + "ms");
073: }
074: }
075: elapsed = System.currentTimeMillis() - start;
076: objectContainer.close();
077: System.out.println("Time to store " + SIZE + " objects: "
078: + elapsed + "ms");
079: }
080:
081: public static void query() {
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(IndexedFieldPerformance.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: }
|