1 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 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 { 58 _stats = new QueryStats(); 59 _stats.connect(db()); 60 } 61 62 protected void db4oCustomTearDown() throws Exception { 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 [] args) { 107 new QueryStatsTestCase().runSoloAndClientServer(); 108 } 109 } 110 | Popular Tags |