KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > util > MiniPerf


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: MiniPerf.java,v 1.20 2006/10/30 21:14:54 bostic Exp $
7  */

8
9 package com.sleepycat.je.util;
10
11 import java.io.File JavaDoc;
12 import java.io.IOException JavaDoc;
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 JavaDoc 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 JavaDoc argv[])
33     throws DatabaseException, IOException JavaDoc, NumberFormatException JavaDoc {
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 JavaDoc {
47
48         envHome = new File JavaDoc(System.getProperty(TestUtils.DEST_DIR));
49         setUp(create);
50     testIterationPerformance(create);
51     tearDown();
52     }
53
54     public void setUp(boolean create)
55     throws IOException JavaDoc, 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 JavaDoc 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
76     public void tearDown()
77     throws IOException JavaDoc, 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
102     public void testIterationPerformance(boolean create)
103         throws IOException JavaDoc, 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 JavaDoc keyString = new String JavaDoc(key);
117
118         byte[] data = new byte[N_DATA_BYTES];
119         TestUtils.generateRandomAlphaBytes(data);
120         String JavaDoc dataString = new String JavaDoc(data);
121         cursor.put(new StringDbt(keyString),
122                            new StringDbt(dataString));
123         }
124         System.out.print("done.");
125     } else {
126         String JavaDoc 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