KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > test > bench > BenchA


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.test.bench;
6
7 import java.math.BigDecimal JavaDoc;
8 import java.sql.*;
9 import java.util.Random JavaDoc;
10
11 /**
12  * See also: http://www.tpc.org/tpca/spec/tpca_current.pdf
13  */

14 public class BenchA implements Bench {
15
16     private Database db;
17
18     private int branches;
19     private int tellers;
20     private int accounts;
21     private int size;
22     
23     private static final String JavaDoc FILLER = "abcdefghijklmnopqrstuvwxyz";
24     private static final int DELTA = 10000;
25
26     public void init(Database db, int size) throws Exception JavaDoc {
27         this.db = db;
28         this.size = size;
29         
30         int scale = 1;
31         accounts = size * 50;
32         tellers = Math.max(accounts / 10, 1);
33         branches = Math.max(tellers / 10, 1);
34
35         db.start(this, "Init");
36
37         db.openConnection();
38
39         db.dropTable("BRANCHES");
40         db.dropTable("BRANCHES");
41         db.dropTable("TELLERS");
42         db.dropTable("ACCOUNTS");
43         db.dropTable("HISTORY");
44
45         String JavaDoc[] create = { "CREATE TABLE BRANCHES(BID INT NOT NULL PRIMARY KEY, BBALANCE DECIMAL(15,2), FILLER VARCHAR(88))",
46                 "CREATE TABLE TELLERS(TID INT NOT NULL PRIMARY KEY, BID INT, TBALANCE DECIMAL(15,2), FILLER VARCHAR(84))",
47                 "CREATE TABLE ACCOUNTS(AID INT NOT NULL PRIMARY KEY, BID INT, ABALANCE DECIMAL(15,2), FILLER VARCHAR(84))",
48                 "CREATE TABLE HISTORY(TID INT, BID INT, AID INT, DELTA DECIMAL(15,2), HTIME DATETIME, FILLER VARCHAR(40))" };
49
50         for (int i = 0; i < create.length; i++) {
51             db.update(create[i]);
52         }
53         
54         PreparedStatement prep;
55         db.setAutoCommit(false);
56         int commitEvery = 1000;
57         prep = db.prepare("INSERT INTO BRANCHES(BID,BBALANCE,FILLER) VALUES(?,10000.00,'" + FILLER + "')");
58         for (int i = 0; i < branches * scale; i++) {
59             prep.setInt(1, i);
60             db.update(prep);
61             if(i%commitEvery==0) {
62                 db.commit();
63             }
64         }
65         db.commit();
66         prep = db.prepare("INSERT INTO TELLERS(TID,BID,TBALANCE,FILLER) VALUES(?,?,10000.00,'" + FILLER + "')");
67         for (int i = 0; i < tellers * scale; i++) {
68             prep.setInt(1, i);
69             prep.setInt(2, i / tellers);
70             db.update(prep);
71             if(i%commitEvery==0) {
72                 db.commit();
73             }
74         }
75         db.commit();
76         int len = accounts * scale;
77         prep = db.prepare("INSERT INTO ACCOUNTS(AID,BID,ABALANCE,FILLER) VALUES(?,?,10000.00,'" + FILLER + "')");
78         for (int i = 0; i < len; i++) {
79             prep.setInt(1, i);
80             prep.setInt(2, i / accounts);
81             db.update(prep);
82             if(i%commitEvery==0) {
83                 db.commit();
84             }
85         }
86         db.commit();
87         db.closeConnection();
88         db.end();
89         
90         db.start(this, "Open/Close");
91         db.openConnection();
92         db.closeConnection();
93         db.end();
94     }
95
96     public void run() throws Exception JavaDoc {
97
98         db.start(this, "Transactions");
99         db.openConnection();
100         processTransactions();
101         db.closeConnection();
102         db.end();
103         
104         db.openConnection();
105         processTransactions();
106         db.logMemory(this, "Memory Usage");
107         db.closeConnection();
108                 
109     }
110     
111     private void processTransactions() throws Exception JavaDoc {
112         Random JavaDoc random = db.getRandom();
113         int branch = random.nextInt(branches);
114         int teller = random.nextInt(tellers);
115         int transactions = size * 30;
116         
117         PreparedStatement updateAccount = db.prepare("UPDATE ACCOUNTS SET ABALANCE=ABALANCE+? WHERE AID=?");
118         PreparedStatement selectBalance = db.prepare("SELECT ABALANCE FROM ACCOUNTS WHERE AID=?");
119         PreparedStatement updateTeller = db.prepare("UPDATE TELLERS SET TBALANCE=TBALANCE+? WHERE TID=?");
120         PreparedStatement updateBranch = db.prepare("UPDATE BRANCHES SET BBALANCE=BBALANCE+? WHERE BID=?");
121         PreparedStatement insertHistory = db.prepare("INSERT INTO HISTORY(AID,TID,BID,DELTA,HTIME,FILLER) VALUES(?,?,?,?,?,?)");
122         int accountsPerBranch = accounts / branches;
123         db.setAutoCommit(false);
124
125         for (int i = 0; i < transactions; i++) {
126             int account;
127             if (random.nextInt(100) < 85) {
128                 account = random.nextInt(accountsPerBranch) + branch * accountsPerBranch;
129             } else {
130                 account = random.nextInt(accounts);
131             }
132             int max = BenchA.DELTA;
133             // delta: -max .. +max
134

135             BigDecimal JavaDoc delta = new BigDecimal JavaDoc("" + (random.nextInt(max * 2) - max));
136             long current = System.currentTimeMillis();
137
138             updateAccount.setBigDecimal(1, delta);
139             updateAccount.setInt(2, account);
140             db.update(updateAccount);
141
142             updateTeller.setBigDecimal(1, delta);
143             updateTeller.setInt(2, teller);
144             db.update(updateTeller);
145
146             updateBranch.setBigDecimal(1, delta);
147             updateBranch.setInt(2, branch);
148             db.update(updateBranch);
149             
150             selectBalance.setInt(1, account);
151             db.queryReadResult(selectBalance);
152
153             insertHistory.setInt(1, account);
154             insertHistory.setInt(2, teller);
155             insertHistory.setInt(3, branch);
156             insertHistory.setBigDecimal(4, delta);
157             // TODO convert: should be able to convert date to timestamp (by using 0 for remaining fields)
158
// insertHistory.setDate(5, new java.sql.Date(current));
159
insertHistory.setTimestamp(5, new java.sql.Timestamp JavaDoc(current));
160             insertHistory.setString(6, BenchA.FILLER);
161             db.update(insertHistory);
162
163             db.commit();
164         }
165         updateAccount.close();
166         selectBalance.close();
167         updateTeller.close();
168         updateBranch.close();
169         insertHistory.close();
170     }
171
172     public String JavaDoc getName() {
173         return "BenchA";
174     }
175
176 }
177
Popular Tags