1 8 9 package com.sleepycat.je.util; 10 11 import java.io.File ; 12 import java.io.IOException ; 13 14 import com.sleepycat.je.Cursor; 15 import com.sleepycat.je.Database; 16 import com.sleepycat.je.DatabaseConfig; 17 import com.sleepycat.je.DatabaseException; 18 import com.sleepycat.je.Environment; 19 import com.sleepycat.je.EnvironmentConfig; 20 import com.sleepycat.je.LockMode; 21 import com.sleepycat.je.OperationStatus; 22 23 public class MiniPerf { 24 25 private File envHome; 26 private Environment exampleEnv; 27 private Database exampleDb; 28 private Cursor cursor; 29 30 static int nKeys; 31 32 static public void main(String argv[]) 33 throws DatabaseException, IOException , NumberFormatException { 34 35 boolean create = false; 36 if (argv.length > 0) { 37 nKeys = Integer.parseInt(argv[0]); 38 create = true; 39 } else { 40 create = false; 41 } 42 new MiniPerf().doit(create); 43 } 44 45 void doit(boolean create) 46 throws DatabaseException, IOException { 47 48 envHome = new File (System.getProperty(TestUtils.DEST_DIR)); 49 setUp(create); 50 testIterationPerformance(create); 51 tearDown(); 52 } 53 54 public void setUp(boolean create) 55 throws IOException , DatabaseException { 56 57 if (create) { 58 TestUtils.removeLogFiles("Setup", envHome, false); 59 } 60 61 EnvironmentConfig envConfig = TestUtils.initEnvConfig(); 63 envConfig.setAllowCreate(create); 64 exampleEnv = new Environment(envHome, envConfig); 65 66 String databaseName = "simpleDb"; 68 DatabaseConfig dbConfig = new DatabaseConfig(); 69 dbConfig.setAllowCreate(true); 70 exampleDb = exampleEnv.openDatabase(null, databaseName, dbConfig); 71 72 cursor = exampleDb.openCursor(null, null); 74 } 75 76 public void tearDown() 77 throws IOException , DatabaseException { 78 79 exampleEnv.sync(); 80 81 if (exampleDb != null) { 82 exampleDb.close(); 83 exampleDb = null; 84 } 85 if (exampleEnv != null) { 86 try { 87 exampleEnv.close(); 88 } catch (DatabaseException DE) { 89 95 } 96 exampleEnv = null; 97 } 98 99 cursor = null; 100 } 101 102 public void testIterationPerformance(boolean create) 103 throws IOException , DatabaseException { 104 105 final int N_KEY_BYTES = 10; 106 final int N_DATA_BYTES = 20; 107 108 if (create) { 109 System.out.print("Creating..."); 110 for (int i = 0; i < nKeys; i++) { 111 if (i % 100000 == 0) { 112 System.out.println(i); 113 } 114 byte[] key = new byte[N_KEY_BYTES]; 115 TestUtils.generateRandomAlphaBytes(key); 116 String keyString = new String (key); 117 118 byte[] data = new byte[N_DATA_BYTES]; 119 TestUtils.generateRandomAlphaBytes(data); 120 String dataString = new String (data); 121 cursor.put(new StringDbt(keyString), 122 new StringDbt(dataString)); 123 } 124 System.out.print("done."); 125 } else { 126 String middleKey = null; 127 int middleEntry = -1; 128 int count = 0; 129 for (int i = 0; i < 3; i++) { 130 System.out.print("Iterating..."); 131 StringDbt foundKey = new StringDbt(); 132 StringDbt foundData = new StringDbt(); 133 134 long startTime = System.currentTimeMillis(); 135 OperationStatus status = cursor.getFirst(foundKey, foundData, LockMode.DEFAULT); 136 137 count = 0; 138 while (status == OperationStatus.SUCCESS) { 139 status = 140 cursor.getNext(foundKey, foundData, LockMode.DEFAULT); 141 count++; 142 if (count == middleEntry) { 143 middleKey = foundKey.getString(); 144 } 145 } 146 long endTime = System.currentTimeMillis(); 147 System.out.println("done."); 148 System.out.println(count + " records found."); 149 middleEntry = count >> 1; 150 System.out.println((endTime - startTime) + " millis"); 151 } 152 153 System.out.println("Middle key: " + middleKey); 154 155 StringDbt searchKey = new StringDbt(middleKey); 156 StringDbt searchData = new StringDbt(); 157 for (int j = 0; j < 3; j++) { 158 long startTime = System.currentTimeMillis(); 159 for (int i = 0; i < count; i++) { 160 if (cursor.getSearchKey(searchKey, 161 searchData, 162 LockMode.DEFAULT) != OperationStatus.SUCCESS) { 163 System.out.println("non-0 return"); 164 } 165 } 166 long endTime = System.currentTimeMillis(); 167 System.out.println((endTime - startTime) + " millis"); 168 } 169 } 170 } 171 } 172 | Popular Tags |