001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (license2)
004: * Initial Developer: H2 Group
005: */
006: package org.h2.test.bench;
007:
008: import java.sql.PreparedStatement;
009: import java.util.Random;
010:
011: /**
012: * This is a very simple benchmark application. One table is created
013: * where rows are inserted, updated, selected (in sequential and random order),
014: * and then deleted.
015: */
016: public class BenchSimple implements Bench {
017:
018: Database db;
019: int records;
020:
021: public void init(Database db, int size) throws Exception {
022: this .db = db;
023: this .records = size * 60;
024:
025: db.start(this , "Init");
026: db.openConnection();
027: db.dropTable("TEST");
028: db.setAutoCommit(false);
029: int commitEvery = 1000;
030: db
031: .update("CREATE TABLE TEST(ID INT NOT NULL PRIMARY KEY, NAME VARCHAR(255))");
032: db.commit();
033: PreparedStatement prep = db
034: .prepare("INSERT INTO TEST VALUES(?, ?)");
035: for (int i = 0; i < records; i++) {
036: prep.setInt(1, i);
037: prep.setString(2, "Hello World " + i);
038: db.update(prep, "insertTest");
039: if (i % commitEvery == 0) {
040: db.commit();
041: }
042: }
043: db.commit();
044: db.closeConnection();
045: db.end();
046:
047: // db.start(this, "Open/Close");
048: // db.openConnection();
049: // db.closeConnection();
050: // db.end();
051:
052: }
053:
054: public void runTest() throws Exception {
055: PreparedStatement prep;
056: Random random = db.getRandom();
057:
058: db.openConnection();
059:
060: db.start(this , "Query (random)");
061: prep = db.prepare("SELECT * FROM TEST WHERE ID=?");
062: for (int i = 0; i < records; i++) {
063: prep.setInt(1, random.nextInt(records));
064: db.queryReadResult(prep);
065: }
066: db.end();
067:
068: db.start(this , "Query (sequential)");
069: prep = db.prepare("SELECT * FROM TEST WHERE ID=?");
070: for (int i = 0; i < records; i++) {
071: prep.setInt(1, i);
072: db.queryReadResult(prep);
073: }
074: db.end();
075:
076: db.start(this , "Update (random)");
077: prep = db.prepare("UPDATE TEST SET NAME=? WHERE ID=?");
078: for (int i = 0; i < records; i++) {
079: prep.setString(1, "Hallo Welt");
080: prep.setInt(2, i);
081: db.update(prep, "updateTest");
082: }
083: db.end();
084:
085: db.start(this , "Delete (sequential)");
086: prep = db.prepare("DELETE FROM TEST WHERE ID=?");
087: // delete only 50%
088: for (int i = 0; i < records; i += 2) {
089: prep.setInt(1, i);
090: db.update(prep, "deleteTest");
091: }
092: db.end();
093:
094: db.closeConnection();
095:
096: db.openConnection();
097: prep = db.prepare("SELECT * FROM TEST WHERE ID=?");
098: for (int i = 0; i < records; i++) {
099: prep.setInt(1, random.nextInt(records));
100: db.queryReadResult(prep);
101: }
102: db.logMemory(this , "Memory Usage");
103: db.closeConnection();
104:
105: }
106:
107: public String getName() {
108: return "Simple";
109: }
110:
111: }
|