1 21 package com.db4o.test.performance; 22 23 import java.io.*; 24 25 import com.db4o.*; 26 import com.db4o.config.*; 27 import com.db4o.io.*; 28 import com.db4o.query.*; 29 30 31 public class SimplePerformanceBenchmark { 32 33 private static int COUNT = 1000; 34 35 private static int DEPTH = 3; 36 37 private static boolean CLIENT_SERVER = false; 38 39 private static boolean TCP = true; 40 41 private static final String FILE = "sip.yap"; 42 43 private static final int PORT = 4477; 44 45 46 private ObjectContainer objectContainer; 47 48 private ObjectServer objectServer; 49 50 51 private long startTime; 52 53 54 public static void main(String [] arguments) { 55 new SimplePerformanceBenchmark().run(); 56 } 57 58 private void run(){ 59 60 clean(); 61 62 configure(); 63 64 open(); 65 store(); 66 close(); 67 68 open(); 69 delete(); 70 close(); 71 } 72 73 private void clean(){ 74 new File(FILE).delete(); 75 } 76 77 private void configure(){ 78 Configuration config = Db4o.configure(); 79 config.lockDatabaseFile(false); 80 config.weakReferences(false); 81 config.io(new MemoryIoAdapter()); 82 config.flushFileBuffers(false); 83 config.singleThreadedClient(true); 84 } 85 86 private void store(){ 87 startTimer(); 88 for (int i = 0; i < COUNT ;i++) { 89 Item item = new Item("load", null); 90 for (int j = 1; j < DEPTH; j++) { 91 item = new Item("load", item); 92 } 93 objectContainer.set(item); 94 } 95 objectContainer.commit(); 96 stopTimer("Store "+ totalObjects() + " objects"); 97 } 98 99 private void delete(){ 100 startTimer(); 101 Query q = objectContainer.query(); 102 q.constrain(Item.class); 103 ObjectSet objectSet = q.execute(); 104 while(objectSet.hasNext()){ 105 objectContainer.delete(objectSet.next()); 106 } 107 objectContainer.commit(); 108 stopTimer("Delete "+ totalObjects() + " objects"); 109 } 110 111 private int totalObjects(){ 112 return COUNT * DEPTH; 113 } 114 115 private void open(){ 116 if(CLIENT_SERVER){ 117 int port = TCP ? PORT : 0; 118 String user = "db4o"; 119 String password = user; 120 objectServer = Db4o.openServer(FILE, port); 121 objectServer.grantAccess(user, password); 122 try { 123 objectContainer = TCP ? 124 Db4o.openClient("localhost", port, user, password) : 125 objectServer.openClient(); 126 } catch (IOException e) { 127 e.printStackTrace(); 128 } 129 } else{ 130 objectContainer = Db4o.openFile(FILE); 131 } 132 } 133 134 private void close(){ 135 objectContainer.close(); 136 if(CLIENT_SERVER){ 137 objectServer.close(); 138 } 139 } 140 141 private void startTimer(){ 142 startTime = System.currentTimeMillis(); 143 } 144 145 private void stopTimer(String message){ 146 long stop = System.currentTimeMillis(); 147 long duration = stop - startTime; 148 System.out.println(message + ": " + duration + "ms"); 149 } 150 151 public static class Item { 152 153 public String _name; 154 155 public Item _child; 156 157 public Item(){ 158 159 } 160 161 public Item(String name, Item child){ 162 _name = name; 163 _child = child; 164 } 165 166 } 167 168 } 169 | Popular Tags |