📄 jdbcbench.java
字号:
if (TabFile != null) { TabFile.print("<auto-commit>;"); } } System.out.println("\n--------------------"); System.out.println("Time to execute " + transaction_count + " transactions: " + completion_time + " seconds."); System.out.println("Max/Min memory usage: " + MemoryWatcher.max + " / " + MemoryWatcher.min + " kb"); 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."); if (TabFile != null) { TabFile.print(MemoryWatcher.max + ";" + MemoryWatcher.min + ";" + failed_transactions + ";" + rate + "\n"); } transaction_count = 0; failed_transactions = 0; MemoryWatcher.reset(); } public synchronized void incrementTransactionCount() { transaction_count++; } public synchronized void incrementFailedTransactionCount() { failed_transactions++; } void createDatabase(String url, String user, String password) throws 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 Etrxn) { transactions = false; } } try { int accountsnb = 0; Statement Stmt = Conn.createStatement(); String Query; Query = "SELECT count(*) "; Query += "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 (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 (Exception E) {} System.out.println("Creates tables"); try { Statement Stmt = Conn.createStatement(); String Query; if (tableExtension.length() > 0) { Query = tableExtension + " branches ("; } else { Query = "CREATE TABLE branches ("; } Query += "Bid INTEGER NOT NULL PRIMARY KEY, "; Query += "Bbalance INTEGER,"; Query += "filler CHAR(88))"; /* pad to 100 bytes */ if (createExtension.length() > 0) { Query += createExtension; } Stmt.execute(Query); Stmt.clearWarnings(); if (tableExtension.length() > 0) { Query = tableExtension + " tellers ("; } else { 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 */ if (createExtension.length() > 0) { Query += createExtension; } Stmt.execute(Query); Stmt.clearWarnings(); if (tableExtension.length() > 0) { Query = tableExtension + " accounts ("; } else { 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 */ if (createExtension.length() > 0) { Query += createExtension; } Stmt.execute(Query); Stmt.clearWarnings(); if (tableExtension.length() > 0) { Query = tableExtension + " history ("; } else { 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 */ if (createExtension.length() > 0) { Query += createExtension; } Stmt.execute(Query); Stmt.clearWarnings(); Stmt.execute("SET TABLE ACCOUNTS SOURCE \"ACCOUNTS.TXT\""); Stmt.execute("SET TABLE BRANCHES SOURCE \"BBRANCHES.TXT\""); Stmt.execute("SET TABLE TELLERS SOURCE \"TELLERS.TXT\""); Stmt.execute("SET TABLE HISTORY SOURCE \"HISTORY.TXT\""); if (transactions) { Conn.commit(); } Stmt.close(); } catch (Exception E) { System.out.println( "Delete elements in table in case Drop didn't work"); } 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 Epstmt) { 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"); // for tests Stmt.execute(ShutdownCommand); Stmt.close(); } catch (Exception E) { System.out.println(E.getMessage()); E.printStackTrace(); } connectClose(Conn); } /* end of CreateDatabase */ public static int getRandomInt(int lo, int hi) { int ret = 0; ret = (int) (Math.random() * (hi - lo + 1)); ret += lo;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -