KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > db4ounit > jre11 > tools > QueryStatsTestCase


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.db4ounit.jre11.tools;
22
23 import com.db4o.ObjectSet;
24 import com.db4o.events.Event4;
25 import com.db4o.events.EventListener4;
26 import com.db4o.events.EventRegistryFactory;
27 import com.db4o.query.Query;
28 import com.db4o.tools.QueryStats;
29
30 import db4ounit.Assert;
31 import db4ounit.extensions.*;
32
33 public class QueryStatsTestCase extends AbstractDb4oTestCase {
34     
35     public static class Item {
36     }
37     
38     private static final int ITEM_COUNT = 10;
39     private QueryStats _stats;
40     
41     final EventListener4 _sleepOnQueryStart = new EventListener4() {
42         public void onEvent(com.db4o.events.Event4 e, com.db4o.events.EventArgs args) {
43             try {
44                 Thread.sleep(50);
45             } catch (InterruptedException JavaDoc x) {
46                 x.printStackTrace();
47             }
48         }
49     };
50
51     protected void store() {
52         for (int i=0; i<ITEM_COUNT; ++i) {
53             db().set(new Item());
54         }
55     }
56     
57     protected void db4oSetupAfterStore() throws Exception JavaDoc {
58         _stats = new QueryStats();
59         _stats.connect(db());
60     }
61
62     protected void db4oCustomTearDown() throws Exception JavaDoc {
63         _stats.disconnect();
64     }
65
66     public void testActivationCount() {
67         
68         Query q = db().query();
69         q.constrain(Item.class);
70         
71         ObjectSet result = q.execute();
72         Assert.areEqual(0, _stats.activationCount());
73         result.next();
74         
75         if (isClientServer()) {
76             Assert.areEqual(10, _stats.activationCount());
77         } else {
78             Assert.areEqual(1, _stats.activationCount());
79             result.next();
80             Assert.areEqual(2, _stats.activationCount());
81         }
82     }
83
84     public void testExecutionTime() {
85         
86         sleepOnQueryStart();
87         
88         Query q = db().query();
89         q.constrain(Item.class);
90         
91         long started = System.currentTimeMillis();
92         q.execute();
93         long elapsed = System.currentTimeMillis() - started;
94         Assert.isTrue(_stats.executionTime() > 0);
95         Assert.isTrue(_stats.executionTime() <= elapsed);
96     }
97
98     private void sleepOnQueryStart() {
99         queryStartedEvent().addListener(_sleepOnQueryStart);
100     }
101     
102     private Event4 queryStartedEvent() {
103         return EventRegistryFactory.forObjectContainer(fileSession()).queryStarted();
104     }
105
106     public static void main(String JavaDoc[] args) {
107         new QueryStatsTestCase().runSoloAndClientServer();
108     }
109 }
110
Popular Tags