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.db4ounit.jre11.tools;
022:
023: import com.db4o.ObjectSet;
024: import com.db4o.events.Event4;
025: import com.db4o.events.EventListener4;
026: import com.db4o.events.EventRegistryFactory;
027: import com.db4o.query.Query;
028: import com.db4o.tools.QueryStats;
029:
030: import db4ounit.Assert;
031: import db4ounit.extensions.*;
032:
033: public class QueryStatsTestCase extends AbstractDb4oTestCase {
034:
035: public static class Item {
036: }
037:
038: private static final int ITEM_COUNT = 10;
039: private QueryStats _stats;
040:
041: final EventListener4 _sleepOnQueryStart = new EventListener4() {
042: public void onEvent(com.db4o.events.Event4 e,
043: com.db4o.events.EventArgs args) {
044: try {
045: Thread.sleep(50);
046: } catch (InterruptedException x) {
047: x.printStackTrace();
048: }
049: }
050: };
051:
052: protected void store() {
053: for (int i = 0; i < ITEM_COUNT; ++i) {
054: db().set(new Item());
055: }
056: }
057:
058: protected void db4oSetupAfterStore() throws Exception {
059: _stats = new QueryStats();
060: _stats.connect(db());
061: }
062:
063: protected void db4oTearDownBeforeClean() throws Exception {
064: _stats.disconnect();
065: }
066:
067: public void testActivationCount() {
068:
069: Query q = db().query();
070: q.constrain(Item.class);
071:
072: ObjectSet result = q.execute();
073: Assert.areEqual(0, _stats.activationCount());
074: result.next();
075:
076: if (isClientServer() && !isMTOC()) {
077: Assert.areEqual(10, _stats.activationCount());
078: } else {
079: Assert.areEqual(1, _stats.activationCount());
080: result.next();
081: Assert.areEqual(2, _stats.activationCount());
082: }
083: }
084:
085: public void testExecutionTime() {
086:
087: sleepOnQueryStart();
088:
089: Query q = db().query();
090: q.constrain(Item.class);
091:
092: long started = System.currentTimeMillis();
093: q.execute();
094: long elapsed = System.currentTimeMillis() - started;
095: Assert.isTrue(_stats.executionTime() >= 0);
096: Assert.isTrue(_stats.executionTime() <= elapsed);
097: }
098:
099: private void sleepOnQueryStart() {
100: queryStartedEvent().addListener(_sleepOnQueryStart);
101: }
102:
103: private Event4 queryStartedEvent() {
104: return EventRegistryFactory.forObjectContainer(fileSession())
105: .queryStarted();
106: }
107:
108: public static void main(String[] args) {
109: new QueryStatsTestCase().runSoloAndClientServer();
110: }
111: }
|