📄 jdbcbench.java
字号:
package org.hsqldb.test;// nbazin@users - enhancements to the original code// fredt@users - 20050202 - corrected getRandomID(int) to return a randomly distributed value/* * This is a sample implementation of the Transaction Processing Performance * Council Benchmark B coded in Java and ANSI SQL2. * * This version is using one connection per thread to parallellize * server operations. * @author Mark Matthews (mark@mysql.com) */import java.io.FileOutputStream;import java.io.PrintStream;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.Enumeration;import java.util.Vector;class JDBCBench { /* tpc bm b scaling rules */ public static int tps = 1; /* the tps scaling factor: here it is 1 */ public static int nbranches = 1; /* number of branches in 1 tps db */ public static int ntellers = 10; /* number of tellers in 1 tps db */ public static int naccounts = 100000; /* number of accounts in 1 tps db */ public static int nhistory = 864000; /* number of history recs in 1 tps db */ public static final int TELLER = 0; public static final int BRANCH = 1; public static final 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 String tableExtension = ""; static String createExtension = ""; static String ShutdownCommand = ""; static String startupCommand = ""; static PrintStream TabFile = null; static boolean verbose = false; MemoryWatcherThread MemoryWatcher; /* main program, creates a 1-tps database: i.e. 1 branch, 10 tellers,... * runs one TPC BM B transaction * example command line: * -driver org.hsqldb.jdbcDriver -url jdbc:hsqldb:/hsql/jdbcbench/test -user sa -clients 20 */ public static void main(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]; if (DriverName.equals( "org.enhydra.instantdb.jdbc.idbDriver")) { ShutdownCommand = "SHUTDOWN"; } if (DriverName.equals( "com.borland.datastore.jdbc.DataStoreDriver")) {} if (DriverName.equals("com.mckoi.JDBCDriver")) { ShutdownCommand = "SHUTDOWN"; } if (DriverName.equals("org.hsqldb.jdbcDriver")) { tableExtension = "CREATE CACHED TABLE "; ShutdownCommand = "SHUTDOWN"; startupCommand = ""; } } } 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("-tabfile")) { if (i + 1 < Args.length) { i++; try { FileOutputStream File = new FileOutputStream(Args[i]); TabFile = new PrintStream(File); } catch (Exception e) { TabFile = null; } } } 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( "usage: java JDBCBench -driver [driver_class_name] -url [url_to_db] -user [username] -password [password] [-v] [-init] [-tpc n] [-clients n]"); System.out.println(); System.out.println("-v verbose error messages"); System.out.println("-init initialize the tables"); System.out.println("-tpc transactions per client"); System.out.println("-clients number of simultaneous clients"); System.exit(-1); } System.out.println( "*********************************************************"); System.out.println( "* JDBCBench v1.1 *"); System.out.println( "*********************************************************"); System.out.println(); 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 { Class.forName(DriverName); JDBCBench Me = new JDBCBench(DBUrl, DBUser, DBPassword, initialize_dataset); } catch (Exception E) { System.out.println(E.getMessage()); E.printStackTrace(); } } public JDBCBench(String url, String user, String password, boolean init) { Vector vClient = new Vector(); Thread Client = null; Enumeration e = null; Connection guardian = null; try { if (init) { System.out.println("Start: " + (new java.util.Date()).toString()); System.out.print("Initializing dataset..."); createDatabase(url, user, password); System.out.println("done.\n"); System.out.println("Complete: " + (new java.util.Date()).toString()); } guardian = connect(url, user, password); if (startupCommand.length() != 0) { Statement statement = guardian.createStatement(); statement.execute(startupCommand); statement.close(); } System.out.println("* Starting Benchmark Run *"); MemoryWatcher = new MemoryWatcherThread(); MemoryWatcher.start(); transactions = true; prepared_stmt = false; start_time = System.currentTimeMillis(); for (int i = 0; i < n_clients; i++) { Client = new ClientThread(n_txn_per_client, url, user, password); Client.start(); vClient.addElement(Client); } /* ** Barrier to complete this test session */ e = vClient.elements(); while (e.hasMoreElements()) { Client = (Thread) e.nextElement(); Client.join(); } vClient.removeAllElements(); reportDone(); checkSums(guardian); // debug - allows stopping the test if (!transactions) { throw new Exception("end after one round"); } transactions = true; prepared_stmt = false; start_time = System.currentTimeMillis(); for (int i = 0; i < n_clients; i++) { Client = new ClientThread(n_txn_per_client, url, user, password); Client.start(); vClient.addElement(Client); } /* ** Barrier to complete this test session */ e = vClient.elements(); while (e.hasMoreElements()) { Client = (Thread) e.nextElement(); Client.join(); } vClient.removeAllElements(); reportDone(); checkSums(guardian); transactions = true; prepared_stmt = true; start_time = System.currentTimeMillis(); for (int i = 0; i < n_clients; i++) { Client = new ClientThread(n_txn_per_client, url, user, password); Client.start(); vClient.addElement(Client); } /* ** Barrier to complete this test session */ e = vClient.elements(); while (e.hasMoreElements()) { Client = (Thread) e.nextElement(); Client.join(); } vClient.removeAllElements(); reportDone(); checkSums(guardian); transactions = true; prepared_stmt = true; start_time = System.currentTimeMillis(); for (int i = 0; i < n_clients; i++) { Client = new ClientThread(n_txn_per_client, url, user, password); Client.start(); vClient.addElement(Client); } /* ** Barrier to complete this test session */ e = vClient.elements(); while (e.hasMoreElements()) { Client = (Thread) e.nextElement(); Client.join(); } vClient.removeAllElements(); reportDone(); checkSums(guardian); } catch (Exception E) { System.out.println(E.getMessage()); E.printStackTrace(); } finally { MemoryWatcher.end(); try { MemoryWatcher.join(); if (ShutdownCommand.length() > 0) { Statement Stmt = guardian.createStatement(); Stmt.execute(ShutdownCommand); Stmt.close(); connectClose(guardian); } if (TabFile != null) { TabFile.close(); } } catch (Exception E1) {}// System.exit(0); } } public void reportDone() { long end_time = System.currentTimeMillis(); double completion_time = ((double) end_time - (double) start_time) / 1000; if (TabFile != null) { TabFile.print(tps + ";" + n_clients + ";" + n_txn_per_client + ";"); } System.out.println("\n* Benchmark Report *"); System.out.print("* Featuring "); if (prepared_stmt) { System.out.print("<prepared statements> "); if (TabFile != null) { TabFile.print("<prepared statements>;"); } } else { System.out.print("<direct queries> "); if (TabFile != null) { TabFile.print("<direct queries>;"); } } if (transactions) { System.out.print("<transactions> "); if (TabFile != null) { TabFile.print("<transactions>;"); } } else { System.out.print("<auto-commit> ");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -