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.tools;
022:
023: import com.db4o.ObjectContainer;
024: import com.db4o.events.Event4;
025: import com.db4o.events.EventArgs;
026: import com.db4o.events.EventListener4;
027: import com.db4o.events.EventRegistry;
028: import com.db4o.events.EventRegistryFactory;
029: import com.db4o.foundation.StopWatch;
030:
031: /**
032: * Keeps track of query statistics.
033: */
034: public class QueryStats {
035:
036: private EventRegistry _registry = null;
037:
038: protected int _activationCount;
039:
040: protected final StopWatch _watch = new StopWatch();
041:
042: private final EventListener4 _queryStarted = new EventListener4() {
043: public void onEvent(Event4 e, EventArgs args) {
044: _activationCount = 0;
045: _watch.start();
046: }
047: };
048:
049: private final EventListener4 _queryFinished = new EventListener4() {
050: public void onEvent(Event4 e, EventArgs args) {
051: _watch.stop();
052: }
053: };
054:
055: private final EventListener4 _activated = new EventListener4() {
056: public void onEvent(Event4 e, EventArgs args) {
057: ++_activationCount;
058: }
059: };
060:
061: /**
062: * How long the last query took to execute.
063: *
064: * @return time in miliseconds
065: */
066: public long executionTime() {
067: return _watch.elapsed();
068: }
069:
070: /**
071: * How many objects were activated so far.
072: */
073: public int activationCount() {
074: return _activationCount;
075: }
076:
077: /**
078: * Starts gathering query statistics for the specified container.
079: */
080: public void connect(ObjectContainer container) {
081: if (_registry != null) {
082: throw new IllegalArgumentException(
083: "Already connected to an ObjectContainer");
084: }
085: _registry = EventRegistryFactory.forObjectContainer(container);
086: _registry.queryStarted().addListener(_queryStarted);
087: _registry.queryFinished().addListener(_queryFinished);
088: _registry.activated().addListener(_activated);
089: }
090:
091: /**
092: * Disconnects from the current container.
093: */
094: public void disconnect() {
095: if (null != _registry) {
096: _registry.queryStarted().removeListener(_queryStarted);
097: _registry.queryFinished().removeListener(_queryFinished);
098: _registry.activated().removeListener(_activated);
099: _registry = null;
100: }
101: }
102: }
|