1 /*- 2 * See the file LICENSE for redistribution information. 3 * 4 * Copyright (c) 2002, 2014 Oracle and/or its affiliates. All rights reserved. 5 * 6 */ 7 8 package com.sleepycat.je.util; 9 10 import java.io.File; 11 12 import com.sleepycat.je.Cursor; 13 import com.sleepycat.je.Database; 14 import com.sleepycat.je.DatabaseConfig; 15 import com.sleepycat.je.DatabaseException; 16 import com.sleepycat.je.Environment; 17 import com.sleepycat.je.EnvironmentConfig; 18 import com.sleepycat.je.LockMode; 19 import com.sleepycat.je.OperationStatus; 20 import com.sleepycat.util.test.SharedTestUtils; 21 import com.sleepycat.utilint.StringUtils; 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 main(String argv[])32 static public void main(String argv[]) 33 throws DatabaseException, 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 doit(boolean create)45 void doit(boolean create) 46 throws DatabaseException { 47 48 envHome = SharedTestUtils.getTestDir(); 49 setUp(create); 50 testIterationPerformance(create); 51 tearDown(); 52 } 53 setUp(boolean create)54 public void setUp(boolean create) 55 throws DatabaseException { 56 57 if (create) { 58 TestUtils.removeLogFiles("Setup", envHome, false); 59 } 60 61 // Set up an environment 62 EnvironmentConfig envConfig = TestUtils.initEnvConfig(); 63 envConfig.setAllowCreate(create); 64 exampleEnv = new Environment(envHome, envConfig); 65 66 // Set up a database 67 String databaseName = "simpleDb"; 68 DatabaseConfig dbConfig = new DatabaseConfig(); 69 dbConfig.setAllowCreate(true); 70 exampleDb = exampleEnv.openDatabase(null, databaseName, dbConfig); 71 72 // Set up cursors 73 cursor = exampleDb.openCursor(null, null); 74 } 75 tearDown()76 public void tearDown() 77 throws 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 /* 90 * Ignore this exception. It's caused by us calling 91 * tearDown() within the test. Each tearDown() call 92 * forces the database closed. So when the call from 93 * junit comes along, it's already closed. 94 */ 95 } 96 exampleEnv = null; 97 } 98 99 cursor = null; 100 } 101 testIterationPerformance(boolean create)102 public void testIterationPerformance(boolean create) 103 throws 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 = StringUtils.fromUTF8(key); 117 118 byte[] data = new byte[N_DATA_BYTES]; 119 TestUtils.generateRandomAlphaBytes(data); 120 String dataString = StringUtils.fromUTF8(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