001: /*
002:
003: * $Id: TestPerformance.java,v 1.4 2003/09/21 15:49:02 boisvert Exp $
004:
005: *
006:
007: * Package performance test
008:
009: *
010:
011: * Simple db toolkit
012:
013: * Copyright (C) 1999, 2000 Cees de Groot <cg@cdegroot.com>
014:
015: *
016:
017: * This library is free software; you can redistribute it and/or
018:
019: * modify it under the terms of the GNU Library General Public License
020:
021: * as published by the Free Software Foundation; either version 2
022:
023: * of the License, or (at your option) any later version.
024:
025: *
026:
027: * This library is distributed in the hope that it will be useful,
028:
029: * but WITHOUT ANY WARRANTY; without even the implied warranty of
030:
031: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
032:
033: * Library General Public License for more details.
034:
035: *
036:
037: * You should have received a copy of the GNU Library General Public License
038:
039: * along with this library; if not, write to the Free Software Foundation,
040:
041: * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
042:
043: */
044:
045: package jdbm.recman;
046:
047: import jdbm.RecordManager;
048:
049: import jdbm.RecordManagerFactory;
050:
051: import jdbm.RecordManagerOptions;
052:
053: import junit.framework.*;
054:
055: import java.util.Properties;
056:
057: import java.util.Random;
058:
059: /**
060:
061: * This class contains performance tests for this package.
062:
063: */
064:
065: public class TestPerformance extends TestCase {
066:
067: public TestPerformance(String name) {
068:
069: super (name);
070:
071: }
072:
073: public void setUp() {
074:
075: TestRecordFile.deleteTestFile();
076:
077: }
078:
079: public void tearDown() {
080:
081: //TestRecordFile.deleteTestFile();
082:
083: }
084:
085: // test parameter: maximum record size
086:
087: final int MAXSIZE = 500; // is this a reasonable size for real-world apps?
088:
089: // test parameter: number of records for fetch/update tests
090:
091: final int RECORDS = 10000;
092:
093: Random rnd = new Random(42);
094:
095: /**
096:
097: * Test insert performance
098:
099: */
100:
101: public void testInserts() throws Exception {
102:
103: RecordManager recman;
104:
105: recman = RecordManagerFactory
106: .createRecordManager(TestRecordFile.testFileName);
107:
108: int inserts = 0;
109:
110: long start = System.currentTimeMillis();
111:
112: try {
113:
114: long stop = 0;
115:
116: while (true) {
117:
118: recman.insert(TestUtil.makeRecord(rnd.nextInt(MAXSIZE),
119:
120: (byte) rnd.nextInt()));
121:
122: inserts++;
123:
124: if ((inserts % 25) == 0) {
125:
126: stop = System.currentTimeMillis();
127:
128: if (stop - start >= 60000)
129:
130: break;
131:
132: }
133:
134: }
135:
136: recman.close();
137:
138: System.out.println("Inserts: " + inserts + " in "
139:
140: + (stop - start) + " millisecs");
141:
142: } catch (Throwable e) {
143:
144: fail("unexpected exception after " + inserts + " inserts, "
145:
146: + (System.currentTimeMillis() - start) + "ms: " + e);
147:
148: }
149:
150: }
151:
152: /**
153:
154: * Create a database, return array of rowids.
155:
156: */
157:
158: private long[] makeRows() throws Exception {
159:
160: RecordManager recman;
161:
162: Properties options;
163:
164: options = new Properties();
165:
166: options.setProperty(RecordManagerOptions.DISABLE_TRANSACTIONS,
167: "true");
168:
169: recman = RecordManagerFactory.createRecordManager(
170: TestRecordFile.testFileName,
171:
172: options);
173:
174: long[] retval = new long[RECORDS];
175:
176: System.out.print("Creating test database");
177:
178: long start = System.currentTimeMillis();
179:
180: try {
181:
182: for (int i = 0; i < RECORDS; i++) {
183:
184: retval[i] = recman.insert(TestUtil
185:
186: .makeRecord(rnd.nextInt(MAXSIZE),
187:
188: (byte) rnd.nextInt()));
189:
190: if ((i % 100) == 0)
191:
192: System.out.print(".");
193:
194: }
195:
196: recman.close();
197:
198: } catch (Throwable e) {
199:
200: e.printStackTrace();
201:
202: fail("unexpected exception during db creation: " + e);
203:
204: }
205:
206: System.out.println("done (" + RECORDS + " inserts in "
207:
208: + (System.currentTimeMillis() - start) + "ms).");
209:
210: return retval;
211:
212: }
213:
214: /**
215:
216: * Test fetches
217:
218: */
219:
220: public void testFetches() throws Exception {
221:
222: RecordManager recman;
223:
224: long[] rowids = makeRows();
225:
226: recman = RecordManagerFactory
227: .createRecordManager(TestRecordFile.testFileName);
228:
229: int fetches = 0;
230:
231: long start = System.currentTimeMillis();
232:
233: try {
234:
235: long stop = 0;
236:
237: while (true) {
238:
239: recman.fetch(rowids[rnd.nextInt(RECORDS)]);
240:
241: fetches++;
242:
243: if ((fetches % 25) == 0) {
244:
245: stop = System.currentTimeMillis();
246:
247: if (stop - start >= 60000)
248:
249: break;
250:
251: }
252:
253: }
254:
255: recman.close();
256:
257: System.out.println("Fetches: " + fetches + " in "
258:
259: + (stop - start) + " millisecs");
260:
261: } catch (Throwable e) {
262:
263: fail("unexpected exception after " + fetches + " fetches, "
264:
265: + (System.currentTimeMillis() - start) + "ms: " + e);
266:
267: }
268:
269: }
270:
271: /**
272:
273: * Test updates.
274:
275: */
276:
277: public void testUpdates() throws Exception {
278:
279: RecordManager recman;
280:
281: long[] rowids = makeRows();
282:
283: recman = RecordManagerFactory
284: .createRecordManager(TestRecordFile.testFileName);
285:
286: int updates = 0;
287:
288: long start = System.currentTimeMillis();
289:
290: try {
291:
292: long stop = 0;
293:
294: while (true) {
295:
296: recman.update(rowids[rnd.nextInt(RECORDS)],
297:
298: TestUtil.makeRecord(rnd.nextInt(MAXSIZE),
299:
300: (byte) rnd.nextInt()));
301:
302: updates++;
303:
304: if ((updates % 25) == 0) {
305:
306: stop = System.currentTimeMillis();
307:
308: if (stop - start >= 60000)
309:
310: break;
311:
312: }
313:
314: }
315:
316: recman.close();
317:
318: System.out.println("Updates: " + updates + " in "
319:
320: + (stop - start) + " millisecs");
321:
322: } catch (Throwable e) {
323:
324: fail("unexpected exception after " + updates + " updates, "
325:
326: + (System.currentTimeMillis() - start) + "ms: " + e);
327:
328: }
329:
330: }
331:
332: /**
333:
334: * Test deletes.
335:
336: */
337:
338: public void testDeletes() throws Exception {
339:
340: RecordManager recman;
341:
342: long[] rowids = makeRows();
343:
344: recman = RecordManagerFactory
345: .createRecordManager(TestRecordFile.testFileName);
346:
347: int deletes = 0;
348:
349: long start = System.currentTimeMillis();
350:
351: try {
352:
353: long stop = 0;
354:
355: // This can be done better...
356:
357: for (int i = 0; i < RECORDS; i++) {
358:
359: recman.delete(rowids[i]);
360:
361: deletes++;
362:
363: if ((deletes % 25) == 0) {
364:
365: stop = System.currentTimeMillis();
366:
367: if (stop - start >= 60000)
368:
369: break;
370:
371: }
372:
373: }
374:
375: recman.close();
376:
377: System.out.println("Deletes: " + deletes + " in "
378:
379: + (stop - start) + " millisecs");
380:
381: } catch (Throwable e) {
382:
383: e.printStackTrace();
384:
385: fail("unexpected exception after " + deletes + " deletes, "
386:
387: + (System.currentTimeMillis() - start) + "ms: " + e);
388:
389: }
390:
391: }
392:
393: /**
394:
395: * Runs all tests in this class
396:
397: */
398:
399: public static void main(String[] args) {
400:
401: junit.textui.TestRunner
402: .run(new TestSuite(TestPerformance.class));
403:
404: // if you just want one test:
405:
406: // TestSuite suite = new TestSuite();
407:
408: // suite.addTest(new TestPerformance("testDeletes"));
409:
410: // junit.textui.TestRunner.run(suite);
411:
412: }
413:
414: }
|