1 21 package com.db4o.test; 22 23 import java.io.*; 24 25 import com.db4o.*; 26 import com.db4o.query.*; 27 28 31 public class IndexedFieldPerformance implements Serializable { 32 33 public String index; 34 35 public static final int SIZE = 50000; 36 public static final int COMMIT_STEP = 5000; 37 public static final int QUERIES = 1000; 38 public static final String FILE = "ifp.yap"; 39 40 public static void main(String [] args){ 41 new Thread (new Runnable () { 42 public void run() { 43 Db4o.configure().objectClass(IndexedFieldPerformance.class).objectField("index").indexed(true); 44 store(); 45 query(); 46 } 47 }).start(); 48 } 49 50 public IndexedFieldPerformance() { 51 } 52 53 public IndexedFieldPerformance(String index) { 54 this.index = index; 55 } 56 57 public static void store() { 58 new File(FILE).delete(); 59 ObjectContainer objectContainer = Db4o.openFile(FILE); 60 61 long start = System.currentTimeMillis(); 62 long elapsed; 63 for (int i = 1; i <= SIZE; i++) { 64 objectContainer.set(new IndexedFieldPerformance("" + i)); 65 if (((double) i / (double) COMMIT_STEP) == i / COMMIT_STEP) { 66 objectContainer.commit(); 67 objectContainer.ext().purge(); 68 elapsed = System.currentTimeMillis() - start; 69 System.out.println("Committed " + i + " from " + SIZE + " elapsed " + elapsed + "ms"); 70 } 71 } 72 elapsed = System.currentTimeMillis() - start; 73 objectContainer.close(); 74 System.out.println("Time to store " + SIZE + " objects: " + elapsed + "ms"); 75 } 76 77 public static void query() { 78 ObjectContainer objectContainer = Db4o.openFile(FILE); 79 long time = System.currentTimeMillis(); 80 for (int i = 1; i <= QUERIES; i++) { 81 Query q = objectContainer.query(); 82 q.constrain(IndexedFieldPerformance.class); 83 q.descend("index").constrain("" + i); 84 q.execute(); 85 } 86 time = System.currentTimeMillis() - time; 87 objectContainer.close(); 88 System.out.println("\nTime for " + QUERIES + " queries against an indexed field in " + SIZE + " objects:\n" + time + "ms"); 89 double perQuery = (double)time / (double)1000; 90 System.out.println("\nTime per query:\n" + perQuery + "ms"); 91 int perSecond = (int)((double)1000 / perQuery); 92 System.out.println("\nQueries per second:\n" + perSecond); 93 } 94 } | Popular Tags |