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.performance;
022:
023: import java.io.*;
024:
025: import com.db4o.*;
026: import com.db4o.config.*;
027: import com.db4o.io.*;
028: import com.db4o.query.*;
029:
030: public class SimplePerformanceBenchmark {
031:
032: private static int COUNT = 1000;
033:
034: private static int DEPTH = 3;
035:
036: private static boolean CLIENT_SERVER = false;
037:
038: private static boolean TCP = true;
039:
040: private static final String FILE = "sip.yap";
041:
042: private static final int PORT = 4477;
043:
044: private ObjectContainer objectContainer;
045:
046: private ObjectServer objectServer;
047:
048: private long startTime;
049:
050: public static void main(String[] arguments) {
051: new SimplePerformanceBenchmark().run();
052: }
053:
054: private void run() {
055:
056: clean();
057:
058: configure();
059:
060: open();
061: store();
062: close();
063:
064: open();
065: delete();
066: close();
067: }
068:
069: private void clean() {
070: new File(FILE).delete();
071: }
072:
073: private void configure() {
074: Configuration config = Db4o.configure();
075: config.lockDatabaseFile(false);
076: config.weakReferences(false);
077: config.io(new MemoryIoAdapter());
078: config.flushFileBuffers(false);
079: config.clientServer().singleThreadedClient(true);
080: }
081:
082: private void store() {
083: startTimer();
084: for (int i = 0; i < COUNT; i++) {
085: Item item = new Item("load", null);
086: for (int j = 1; j < DEPTH; j++) {
087: item = new Item("load", item);
088: }
089: objectContainer.set(item);
090: }
091: objectContainer.commit();
092: stopTimer("Store " + totalObjects() + " objects");
093: }
094:
095: private void delete() {
096: startTimer();
097: Query q = objectContainer.query();
098: q.constrain(Item.class);
099: ObjectSet objectSet = q.execute();
100: while (objectSet.hasNext()) {
101: objectContainer.delete(objectSet.next());
102: }
103: objectContainer.commit();
104: stopTimer("Delete " + totalObjects() + " objects");
105: }
106:
107: private int totalObjects() {
108: return COUNT * DEPTH;
109: }
110:
111: private void open() {
112: if (CLIENT_SERVER) {
113: int port = TCP ? PORT : 0;
114: String user = "db4o";
115: String password = user;
116: objectServer = Db4o.openServer(FILE, port);
117: objectServer.grantAccess(user, password);
118: objectContainer = TCP ? Db4o.openClient("localhost", port,
119: user, password) : objectServer.openClient();
120: } else {
121: objectContainer = Db4o.openFile(FILE);
122: }
123: }
124:
125: private void close() {
126: objectContainer.close();
127: if (CLIENT_SERVER) {
128: objectServer.close();
129: }
130: }
131:
132: private void startTimer() {
133: startTime = System.currentTimeMillis();
134: }
135:
136: private void stopTimer(String message) {
137: long stop = System.currentTimeMillis();
138: long duration = stop - startTime;
139: System.out.println(message + ": " + duration + "ms");
140: }
141:
142: public static class Item {
143:
144: public String _name;
145:
146: public Item _child;
147:
148: public Item() {
149:
150: }
151:
152: public Item(String name, Item child) {
153: _name = name;
154: _child = child;
155: }
156:
157: }
158:
159: }
|