1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
|
/*
* This is a sample implementation of the
* Transaction Processing Performance Council
* Benchmark B coded in Java/JDBC and ANSI SQL2.
*
* This version is using one connection per
* thread to parallellize server operations.
*/
package SQLite;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Enumeration;
import java.util.Vector;
public abstract class Benchmark {
/* the tps scaling factor: here it is 1 */
public static int tps = 1;
/* number of branches in 1 tps db */
public static int nbranches = 1;
/* number of tellers in 1 tps db */
public static int ntellers = 10;
/* number of accounts in 1 tps db */
public static int naccounts = 50000;
/* number of history recs in 1 tps db */
public static int nhistory = 864000;
public final static int TELLER = 0;
public final static int BRANCH = 1;
public final static int ACCOUNT = 2;
int failed_transactions = 0;
int transaction_count = 0;
static int n_clients = 10;
static int n_txn_per_client = 10;
long start_time = 0;
static boolean transactions = true;
static boolean prepared_stmt = false;
static boolean verbose = false;
/*
* main program
* creates a 1-tps database:
* i.e. 1 branch, 10 tellers,...
* runs one TPC BM B transaction
*/
public void run(String[] args) {
String DriverName = "";
String DBUrl = "";
String DBUser = "";
String DBPassword = "";
boolean initialize_dataset = false;
for (int i = 0; i < args.length; i++) {
if (args[i].equals("-clients")) {
if (i + 1 < args.length) {
i++;
n_clients = Integer.parseInt(args[i]);
}
} else if (args[i].equals("-driver")) {
if (i + 1 < args.length) {
i++;
DriverName = args[i];
}
} else if (args[i].equals("-url")) {
if (i + 1 < args.length) {
i++;
DBUrl = args[i];
}
} else if (args[i].equals("-user")) {
if (i + 1 < args.length) {
i++;
DBUser = args[i];
}
} else if (args[i].equals("-password")) {
if (i + 1 < args.length) {
i++;
DBPassword = args[i];
}
} else if (args[i].equals("-tpc")) {
if (i + 1 < args.length) {
i++;
n_txn_per_client = Integer.parseInt(args[i]);
}
} else if (args[i].equals("-init")) {
initialize_dataset = true;
} else if (args[i].equals("-tps")) {
if (i + 1 < args.length) {
i++;
tps = Integer.parseInt(args[i]);
}
} else if (args[i].equals("-v")) {
verbose = true;
}
}
if (DriverName.length() == 0 || DBUrl.length() == 0) {
System.out.println("JDBC based benchmark program\n\n" +
"JRE usage:\n\njava SQLite.BenchmarkDriver " +
"-url [url_to_db] \\\n " +
"[-user [username]] " +
"[-password [password]] " +
"[-driver [driver_class_name]] \\\n " +
"[-v] [-init] [-tpc N] [-tps N] " +
"[-clients N]\n");
System.out.println("OJEC usage:\n\ncvm SQLite.BenchmarkDataSource " +
"[-user [username]] " +
"[-password [password]] " +
"[-driver [driver_class_name]] \\\n " +
"[-v] [-init] [-tpc N] [-tps N] " +
"[-clients N]\n");
System.out.println();
System.out.println("-v verbose mode");
System.out.println("-init initialize the tables");
System.out.println("-tpc N transactions per client");
System.out.println("-tps N scale factor");
System.out.println("-clients N number of simultaneous clients/threads");
System.out.println();
System.out.println("Default driver class is SQLite.JDBCDriver");
System.out.println("in this case use an -url parameter of the form");
System.out.println(" jdbc:sqlite:/[path]");
System.exit(1);
}
System.out.println("Driver: " + DriverName);
System.out.println("URL:" + DBUrl);
System.out.println();
System.out.println("Scale factor value: " + tps);
System.out.println("Number of clients: " + n_clients);
System.out.println("Number of transactions per client: " +
n_txn_per_client);
System.out.println();
try {
benchmark(DBUrl, DBUser, DBPassword, initialize_dataset);
} catch (java.lang.Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
public void benchmark(String url, String user, String password, boolean init) {
Vector vClient = new Vector();
Thread Client = null;
Enumeration en = null;
try {
if (init) {
System.out.print("Initializing dataset...");
createDatabase(url, user, password);
System.out.println("done.\n");
}
System.out.println("* Starting Benchmark Run *");
transactions = false;
prepared_stmt = false;
start_time = System.currentTimeMillis();
for (int i = 0; i < n_clients; i++) {
Client = new BenchmarkThread(n_txn_per_client, url, user,
password, this);
Client.start();
vClient.addElement(Client);
}
/*
* Barrier to complete this test session
*/
en = vClient.elements();
while (en.hasMoreElements()) {
Client = (Thread) en.nextElement();
Client.join();
}
vClient.removeAllElements();
reportDone();
transactions = true;
prepared_stmt = false;
start_time = System.currentTimeMillis();
for (int i = 0; i < n_clients; i++) {
Client = new BenchmarkThread(n_txn_per_client, url,
user, password, this);
Client.start();
vClient.addElement(Client);
}
/*
* Barrier to complete this test session
*/
en = vClient.elements();
while (en.hasMoreElements()) {
Client = (Thread) en.nextElement();
Client.join();
}
vClient.removeAllElements();
reportDone();
transactions = false;
prepared_stmt = true;
start_time = System.currentTimeMillis();
for (int i = 0; i < n_clients; i++) {
Client = new BenchmarkThread(n_txn_per_client, url,
user, password, this);
Client.start();
vClient.addElement(Client);
}
/*
* Barrier to complete this test session
*/
en = vClient.elements();
while (en.hasMoreElements()) {
Client = (Thread) en.nextElement();
Client.join();
}
vClient.removeAllElements();
reportDone();
transactions = true;
prepared_stmt = true;
start_time = System.currentTimeMillis();
for (int i = 0; i < n_clients; i++) {
Client = new BenchmarkThread(n_txn_per_client, url,
user, password, this);
Client.start();
vClient.addElement(Client);
}
/*
* Barrier to complete this test session
*/
en = vClient.elements();
while (en.hasMoreElements()) {
Client = (Thread) en.nextElement();
Client.join();
}
vClient.removeAllElements();
reportDone();
} catch (java.lang.Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
} finally {
System.exit(0);
}
}
public void reportDone() {
long end_time = System.currentTimeMillis();
double completion_time =
((double)end_time - (double)start_time) / 1000;
System.out.println("\n* Benchmark Report *" );
System.out.print("* Featuring ");
if (prepared_stmt) {
System.out.print("<prepared statements> ");
} else {
System.out.print("<direct queries> ");
}
if (transactions) {
System.out.print("<transactions> ");
} else {
System.out.print("<auto-commit> ");
}
System.out.println("\n--------------------");
System.out.println("Time to execute " +
transaction_count + " transactions: " +
completion_time + " seconds.");
System.out.println(failed_transactions + " / " +
transaction_count + " failed to complete.");
double rate = (transaction_count - failed_transactions)
/ completion_time;
System.out.println("Transaction rate: " + rate + " txn/sec.");
transaction_count = 0;
failed_transactions = 0;
System.gc();
}
public synchronized void incrementTransactionCount() {
transaction_count++;
}
public synchronized void incrementFailedTransactionCount() {
failed_transactions++;
}
void createDatabase(String url, String user, String password)
throws java.lang.Exception {
Connection Conn = connect(url, user, password);
String s = Conn.getMetaData().getDatabaseProductName();
System.out.println("DBMS: "+s);
transactions = true;
if (transactions) {
try {
Conn.setAutoCommit(false);
System.out.println("In transaction mode");
} catch (SQLException etxn) {
transactions = false;
}
}
try {
int accountsnb = 0;
Statement Stmt = Conn.createStatement();
String Query;
Query = "SELECT count(*) FROM accounts";
ResultSet RS = Stmt.executeQuery(Query);
Stmt.clearWarnings();
while (RS.next()) {
accountsnb = RS.getInt(1);
}
if (transactions) {
Conn.commit();
}
Stmt.close();
if (accountsnb == (naccounts*tps)) {
System.out.println("Already initialized");
connectClose(Conn);
return;
}
} catch (java.lang.Exception e) {
}
System.out.println("Drop old tables if they exist");
try {
Statement Stmt = Conn.createStatement();
String Query;
Query = "DROP TABLE history";
Stmt.execute(Query);
Stmt.clearWarnings();
Query = "DROP TABLE accounts";
Stmt.execute(Query);
Stmt.clearWarnings();
Query = "DROP TABLE tellers";
Stmt.execute(Query);
Stmt.clearWarnings();
Query = "DROP TABLE branches";
Stmt.execute(Query);
Stmt.clearWarnings();
if (transactions) {
Conn.commit();
}
Stmt.close();
} catch (java.lang.Exception e) {
}
System.out.println("Creates tables");
try {
Statement Stmt = Conn.createStatement();
String Query;
Query = "CREATE TABLE branches (";
Query += "Bid INTEGER NOT NULL PRIMARY KEY,";
Query += "Bbalance INTEGER,";
Query += "filler CHAR(88))"; /* pad to 100 bytes */
Stmt.execute(Query);
Stmt.clearWarnings();
Query = "CREATE TABLE tellers (";
Query += "Tid INTEGER NOT NULL PRIMARY KEY,";
Query += "Bid INTEGER,";
Query += "Tbalance INTEGER,";
Query += "filler CHAR(84))"; /* pad to 100 bytes */
Stmt.execute(Query);
Stmt.clearWarnings();
Query = "CREATE TABLE accounts (";
Query += "Aid INTEGER NOT NULL PRIMARY KEY,";
Query += "Bid INTEGER,";
Query += "Abalance INTEGER,";
Query += "filler CHAR(84))"; /* pad to 100 bytes */
Stmt.execute(Query);
Stmt.clearWarnings();
Query = "CREATE TABLE history (";
Query += "Tid INTEGER,";
Query += "Bid INTEGER,";
Query += "Aid INTEGER,";
Query += "delta INTEGER,";
Query += "tstime TIMESTAMP,";
Query += "filler CHAR(22))"; /* pad to 50 bytes */
Stmt.execute(Query);
Stmt.clearWarnings();
if (transactions) {
Conn.commit();
}
Stmt.close();
} catch (java.lang.Exception e) {
}
System.out.println("Delete elements in table in case DROP didn't work");
try {
Statement Stmt = Conn.createStatement();
String Query;
Query = "DELETE FROM history";
Stmt.execute(Query);
Stmt.clearWarnings();
Query = "DELETE FROM accounts";
Stmt.execute(Query);
Stmt.clearWarnings();
Query = "DELETE FROM tellers";
Stmt.execute(Query);
Stmt.clearWarnings();
Query = "DELETE FROM branches";
Stmt.execute(Query);
Stmt.clearWarnings();
if (transactions) {
Conn.commit();
}
/*
* prime database using TPC BM B scaling rules.
* Note that for each branch and teller:
* branch_id = teller_id / ntellers
* branch_id = account_id / naccounts
*/
PreparedStatement pstmt = null;
prepared_stmt = true;
if (prepared_stmt) {
try {
Query = "INSERT INTO branches(Bid,Bbalance) VALUES (?,0)";
pstmt = Conn.prepareStatement(Query);
System.out.println("Using prepared statements");
} catch (SQLException estmt) {
pstmt = null;
prepared_stmt = false;
}
}
System.out.println("Insert data in branches table");
for (int i = 0; i < nbranches * tps; i++) {
if (prepared_stmt) {
pstmt.setInt(1,i);
pstmt.executeUpdate();
pstmt.clearWarnings();
} else {
Query = "INSERT INTO branches(Bid,Bbalance) VALUES (" +
i + ",0)";
Stmt.executeUpdate(Query);
}
if ((i%100==0) && (transactions)) {
Conn.commit();
}
}
if (prepared_stmt) {
pstmt.close();
}
if (transactions) {
Conn.commit();
}
if (prepared_stmt) {
Query = "INSERT INTO tellers(Tid,Bid,Tbalance) VALUES (?,?,0)";
pstmt = Conn.prepareStatement(Query);
}
System.out.println("Insert data in tellers table");
for (int i = 0; i < ntellers * tps; i++) {
if (prepared_stmt) {
pstmt.setInt(1,i);
pstmt.setInt(2,i/ntellers);
pstmt.executeUpdate();
pstmt.clearWarnings();
} else {
Query = "INSERT INTO tellers(Tid,Bid,Tbalance) VALUES (" +
i + "," + i / ntellers + ",0)";
Stmt.executeUpdate(Query);
}
if ((i%100==0) && (transactions)) {
Conn.commit();
}
}
if (prepared_stmt) {
pstmt.close();
}
if (transactions) {
Conn.commit();
}
if (prepared_stmt) {
Query = "INSERT INTO accounts(Aid,Bid,Abalance) VALUES (?,?,0)";
pstmt = Conn.prepareStatement(Query);
}
System.out.println("Insert data in accounts table");
for (int i = 0; i < naccounts*tps; i++) {
if (prepared_stmt) {
pstmt.setInt(1,i);
pstmt.setInt(2,i/naccounts);
pstmt.executeUpdate();
pstmt.clearWarnings();
} else {
Query = "INSERT INTO accounts(Aid,Bid,Abalance) VALUES (" +
i + "," + i / naccounts + ",0)";
Stmt.executeUpdate(Query);
}
if ((i%10000==0) && (transactions)) {
Conn.commit();
}
if ((i>0) && ((i%10000)==0)) {
System.out.println("\t" + i + "\t records inserted");
}
}
if (prepared_stmt) {
pstmt.close();
}
if (transactions) {
Conn.commit();
}
System.out.println("\t" + (naccounts*tps) + "\t records inserted");
Stmt.close();
} catch (java.lang.Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
connectClose(Conn);
}
public static int getRandomInt(int lo, int hi) {
int ret = 0;
ret = (int)(Math.random() * (hi - lo + 1));
ret += lo;
return ret;
}
public static int getRandomID(int type) {
int min, max, num;
max = min = 0;
num = naccounts;
switch(type) {
case TELLER:
min += nbranches;
num = ntellers;
/* FALLTHROUGH */
case BRANCH:
if (type == BRANCH) {
num = nbranches;
}
min += naccounts;
/* FALLTHROUGH */
case ACCOUNT:
max = min + num - 1;
}
return (getRandomInt(min, max));
}
public abstract Connection connect(String DBUrl, String DBUser,
String DBPassword);
public static void connectClose(Connection c) {
if (c == null) {
return;
}
try {
c.close();
} catch (java.lang.Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
class BenchmarkThread extends Thread {
int ntrans = 0;
Connection Conn;
Benchmark bench;
PreparedStatement pstmt1 = null;
PreparedStatement pstmt2 = null;
PreparedStatement pstmt3 = null;
PreparedStatement pstmt4 = null;
PreparedStatement pstmt5 = null;
public BenchmarkThread(int number_of_txns,String url,
String user, String password,
Benchmark b) {
bench = b;
ntrans = number_of_txns;
Conn = b.connect(url, user, password);
if (Conn == null) {
return;
}
try {
if (Benchmark.transactions) {
Conn.setAutoCommit(false);
}
if (Benchmark.prepared_stmt) {
String Query;
Query = "UPDATE accounts";
Query += " SET Abalance = Abalance + ?";
Query += " WHERE Aid = ?";
pstmt1 = Conn.prepareStatement(Query);
Query = "SELECT Abalance";
Query += " FROM accounts";
Query += " WHERE Aid = ?";
pstmt2 = Conn.prepareStatement(Query);
Query = "UPDATE tellers";
Query += " SET Tbalance = Tbalance + ?";
Query += " WHERE Tid = ?";
pstmt3 = Conn.prepareStatement(Query);
Query = "UPDATE branches";
Query += " SET Bbalance = Bbalance + ?";
Query += " WHERE Bid = ?";
pstmt4 = Conn.prepareStatement(Query);
Query = "INSERT INTO history(Tid, Bid, Aid, delta)";
Query += " VALUES (?,?,?,?)";
pstmt5 = Conn.prepareStatement(Query);
}
} catch (java.lang.Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
public void run() {
while (ntrans-- > 0) {
int account = Benchmark.getRandomID(Benchmark.ACCOUNT);
int branch = Benchmark.getRandomID(Benchmark.BRANCH);
int teller = Benchmark.getRandomID(Benchmark.TELLER);
int delta = Benchmark.getRandomInt(0, 1000);
doOne(branch, teller, account, delta);
bench.incrementTransactionCount();
}
if (Benchmark.prepared_stmt) {
try {
if (pstmt1 != null) {
pstmt1.close();
}
if (pstmt2 != null) {
pstmt2.close();
}
if (pstmt3 != null) {
pstmt3.close();
}
if (pstmt4 != null) {
pstmt4.close();
}
if (pstmt5 != null) {
pstmt5.close();
}
} catch (java.lang.Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
Benchmark.connectClose(Conn);
Conn = null;
}
/*
* Executes a single TPC BM B transaction.
*/
int doOne(int bid, int tid, int aid, int delta) {
int aBalance = 0;
if (Conn == null) {
bench.incrementFailedTransactionCount();
return 0;
}
try {
if (Benchmark.prepared_stmt) {
pstmt1.setInt(1,delta);
pstmt1.setInt(2,aid);
pstmt1.executeUpdate();
pstmt1.clearWarnings();
pstmt2.setInt(1,aid);
ResultSet RS = pstmt2.executeQuery();
pstmt2.clearWarnings();
while (RS.next()) {
aBalance = RS.getInt(1);
}
pstmt3.setInt(1,delta);
pstmt3.setInt(2,tid);
pstmt3.executeUpdate();
pstmt3.clearWarnings();
pstmt4.setInt(1,delta);
pstmt4.setInt(2,bid);
pstmt4.executeUpdate();
pstmt4.clearWarnings();
pstmt5.setInt(1,tid);
pstmt5.setInt(2,bid);
pstmt5.setInt(3,aid);
pstmt5.setInt(4,delta);
pstmt5.executeUpdate();
pstmt5.clearWarnings();
} else {
Statement Stmt = Conn.createStatement();
String Query = "UPDATE accounts";
Query += " SET Abalance = Abalance + " + delta;
Query += " WHERE Aid = " + aid;
Stmt.executeUpdate(Query);
Stmt.clearWarnings();
Query = "SELECT Abalance";
Query += " FROM accounts";
Query += " WHERE Aid = " + aid;
ResultSet RS = Stmt.executeQuery(Query);
Stmt.clearWarnings();
while (RS.next()) {
aBalance = RS.getInt(1);
}
Query = "UPDATE tellers";
Query += " SET Tbalance = Tbalance + " + delta;
Query += " WHERE Tid = " + tid;
Stmt.executeUpdate(Query);
Stmt.clearWarnings();
Query = "UPDATE branches";
Query += " SET Bbalance = Bbalance + " + delta;
Query += " WHERE Bid = " + bid;
Stmt.executeUpdate(Query);
Stmt.clearWarnings();
Query = "INSERT INTO history(Tid, Bid, Aid, delta)";
Query += " VALUES (";
Query += tid + ",";
Query += bid + ",";
Query += aid + ",";
Query += delta + ")";
Stmt.executeUpdate(Query);
Stmt.clearWarnings();
Stmt.close();
}
if (Benchmark.transactions) {
Conn.commit();
}
return aBalance;
} catch (java.lang.Exception e) {
if (Benchmark.verbose) {
System.out.println("Transaction failed: "
+ e.getMessage());
e.printStackTrace();
}
bench.incrementFailedTransactionCount();
if (Benchmark.transactions) {
try {
Conn.rollback();
} catch (SQLException e1) {
}
}
}
return 0;
}
}
|