001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2002,2008 Oracle. All rights reserved.
005: *
006: * $Id: MiniPerf.java,v 1.20.2.3 2008/01/07 15:14:34 cwl Exp $
007: */
008:
009: package com.sleepycat.je.util;
010:
011: import java.io.File;
012: import java.io.IOException;
013:
014: import com.sleepycat.je.Cursor;
015: import com.sleepycat.je.Database;
016: import com.sleepycat.je.DatabaseConfig;
017: import com.sleepycat.je.DatabaseException;
018: import com.sleepycat.je.Environment;
019: import com.sleepycat.je.EnvironmentConfig;
020: import com.sleepycat.je.LockMode;
021: import com.sleepycat.je.OperationStatus;
022:
023: public class MiniPerf {
024:
025: private File envHome;
026: private Environment exampleEnv;
027: private Database exampleDb;
028: private Cursor cursor;
029:
030: static int nKeys;
031:
032: static public void main(String argv[]) throws DatabaseException,
033: IOException, NumberFormatException {
034:
035: boolean create = false;
036: if (argv.length > 0) {
037: nKeys = Integer.parseInt(argv[0]);
038: create = true;
039: } else {
040: create = false;
041: }
042: new MiniPerf().doit(create);
043: }
044:
045: void doit(boolean create) throws DatabaseException, IOException {
046:
047: envHome = new File(System.getProperty(TestUtils.DEST_DIR));
048: setUp(create);
049: testIterationPerformance(create);
050: tearDown();
051: }
052:
053: public void setUp(boolean create) throws IOException,
054: DatabaseException {
055:
056: if (create) {
057: TestUtils.removeLogFiles("Setup", envHome, false);
058: }
059:
060: // Set up an environment
061: EnvironmentConfig envConfig = TestUtils.initEnvConfig();
062: envConfig.setAllowCreate(create);
063: exampleEnv = new Environment(envHome, envConfig);
064:
065: // Set up a database
066: String databaseName = "simpleDb";
067: DatabaseConfig dbConfig = new DatabaseConfig();
068: dbConfig.setAllowCreate(true);
069: exampleDb = exampleEnv.openDatabase(null, databaseName,
070: dbConfig);
071:
072: // Set up cursors
073: cursor = exampleDb.openCursor(null, null);
074: }
075:
076: public void tearDown() throws IOException, DatabaseException {
077:
078: exampleEnv.sync();
079:
080: if (exampleDb != null) {
081: exampleDb.close();
082: exampleDb = null;
083: }
084: if (exampleEnv != null) {
085: try {
086: exampleEnv.close();
087: } catch (DatabaseException DE) {
088: /*
089: * Ignore this exception. It's caused by us calling
090: * tearDown() within the test. Each tearDown() call
091: * forces the database closed. So when the call from
092: * junit comes along, it's already closed.
093: */
094: }
095: exampleEnv = null;
096: }
097:
098: cursor = null;
099: }
100:
101: public void testIterationPerformance(boolean create)
102: throws IOException, DatabaseException {
103:
104: final int N_KEY_BYTES = 10;
105: final int N_DATA_BYTES = 20;
106:
107: if (create) {
108: System.out.print("Creating...");
109: for (int i = 0; i < nKeys; i++) {
110: if (i % 100000 == 0) {
111: System.out.println(i);
112: }
113: byte[] key = new byte[N_KEY_BYTES];
114: TestUtils.generateRandomAlphaBytes(key);
115: String keyString = new String(key);
116:
117: byte[] data = new byte[N_DATA_BYTES];
118: TestUtils.generateRandomAlphaBytes(data);
119: String dataString = new String(data);
120: cursor.put(new StringDbt(keyString), new StringDbt(
121: dataString));
122: }
123: System.out.print("done.");
124: } else {
125: String middleKey = null;
126: int middleEntry = -1;
127: int count = 0;
128: for (int i = 0; i < 3; i++) {
129: System.out.print("Iterating...");
130: StringDbt foundKey = new StringDbt();
131: StringDbt foundData = new StringDbt();
132:
133: long startTime = System.currentTimeMillis();
134: OperationStatus status = cursor.getFirst(foundKey,
135: foundData, LockMode.DEFAULT);
136:
137: count = 0;
138: while (status == OperationStatus.SUCCESS) {
139: status = cursor.getNext(foundKey, foundData,
140: 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, searchData,
161: LockMode.DEFAULT) != OperationStatus.SUCCESS) {
162: System.out.println("non-0 return");
163: }
164: }
165: long endTime = System.currentTimeMillis();
166: System.out.println((endTime - startTime) + " millis");
167: }
168: }
169: }
170: }
|