Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.io.*;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.concurrent.LinkedBlockingQueue;
- public class Main extends java.lang.Thread {
- private static class Config {
- public String username = "root";
- public String password = "";
- public String host = "127.0.0.1";
- public String port = "3306";
- public String infile = "";
- public int thread = 1;
- public int iteration = 1;
- public int maxattempt = 1;
- }
- private static class Master implements Runnable {
- private LinkedBlockingQueue<String> queue = new LinkedBlockingQueue<String>(100000);
- private Config conf = null;
- public Master(Config config) {
- this.conf = config;
- }
- public void run() {
- Thread t[] = new Thread[conf.thread];
- for (int i = 0; i < conf.thread; i++) {
- t[i] = new Thread(new Worker(i, queue, conf));
- }
- for (int i = 0; i < conf.thread; i++) {
- t[i].start();
- }
- for (int i = 0; i < conf.iteration; i++) {
- try { // Create an input stream, reading from the specified file
- BufferedReader reader = new BufferedReader(
- new InputStreamReader(
- new FileInputStream(conf.infile)));
- String in;
- while ((in = reader.readLine()) != null) {
- try {
- queue.put(in);
- Thread.sleep(5);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- reader.close();
- } catch (IOException ioe) {
- System.out.println("I/O error - " + ioe);
- }
- }
- for (int i = 0; i < conf.thread; i++) {
- try {
- queue.put("STOP");
- } catch (InterruptedException ie) {
- ie.printStackTrace();
- }
- }
- }
- }
- private static class Worker implements Runnable {
- private LinkedBlockingQueue<String> queue = null;
- private Config conf = null;
- private int thread_id = -1;
- private QueryRunner qr;
- public Worker(int thread_id, LinkedBlockingQueue<String> queue, Config config) { // Assign to member variable
- this.queue = queue;
- this.conf = config;
- this.thread_id = thread_id;
- }
- private class QueryRunner {
- String hostname, port, username, password;
- int retries;
- Connection conn = null;
- Statement s = null;
- public QueryRunner(String hostname, String port, String username, String password, int retries) {
- this.hostname = hostname;
- this.port = port;
- this.username = username;
- this.password = password;
- this.retries = retries;
- connect();
- }
- private void connect() {
- try {
- String url = "jdbc:mysql://" + hostname + ":" + port + "/";
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- conn = DriverManager.getConnection(url, username, password);
- s = conn.createStatement();
- } catch (SQLException sqle) {
- sqle.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- private boolean refresh() {
- int tries = 0;
- boolean buf = false;
- try {
- while (!(buf = !conn.isClosed()) && tries < retries) {
- tries++;
- connect();
- }
- } catch (SQLException sqle) {
- sqle.printStackTrace();
- }
- return buf;
- }
- public boolean query(String q) {
- boolean buf = false;
- try {
- if (refresh()) {
- ResultSet rs = s.executeQuery(q);
- buf = true;
- }
- } catch (SQLException sqle) {
- sqle.printStackTrace();
- }
- return buf;
- }
- public void end() {
- try {
- s.close();
- conn.close();
- } catch (SQLException sqle) {
- sqle.printStackTrace();
- }
- }
- }
- public void run() {
- qr = new QueryRunner(conf.host, conf.port, conf.username, conf.password, conf.maxattempt);
- long startThreadTime = System.currentTimeMillis();
- java.util.Date dt = new java.util.Date();
- dt.setTime(startThreadTime);
- System.out.println("Thread " + thread_id + " start. " + dt);
- String receivedString = "";
- String logStr = "";
- Boolean stop = false;
- String[] queries = null;
- while (!stop) {
- try {
- receivedString = queue.take();
- } catch (InterruptedException ie) {
- ie.printStackTrace();
- }
- if (receivedString != null) {
- if (receivedString.equals("STOP")) {
- stop = true;
- } else {
- long startQueryTime = System.currentTimeMillis();
- //lakukan berulang ulang selama belum berhasil dan paling banyak maxattempt
- queries = receivedString.split(";");
- for (int i = 0; i < queries.length; i++) {
- queries[i] = queries[i].trim();
- }
- String test = "";
- logStr = "Execution " + thread_id + "\t" + dt;
- for (int i = 0; i < queries.length; i++) {
- test = queries[i].toLowerCase();
- if (test.startsWith("use") || test.startsWith("select")) {
- dt.setTime(startQueryTime);
- qr.query(queries[i]);
- }
- }
- long endQueryTime = System.currentTimeMillis();
- dt.setTime(endQueryTime);
- logStr = logStr + "\t" + dt;
- logStr = logStr + "\t" + (endQueryTime - startQueryTime) + "\t" + queries[1];
- System.out.println(logStr);
- }
- }
- }
- long endThreadTime = System.currentTimeMillis();
- System.out.println("Thread " + thread_id + " stop. " + (endThreadTime - startThreadTime) + " milliseconds");
- qr.end();
- }
- }
- public static void main(String args[]) throws IOException {
- Config config = new Config();
- for (int i = 0; i < args.length; i++) {
- if (args[i].startsWith("-u")) {
- config.username = args[i].substring(2);
- } else if (args[i].startsWith("-p")) {
- config.password = args[i].substring(2);
- } else if (args[i].startsWith("--batchfile=")) {
- config.infile = args[i].substring(12);
- } else if (args[i].startsWith("--thread=")) {
- config.thread = Integer.parseInt(args[i].substring(9));
- } else if (args[i].startsWith("--iteration=")) {
- config.iteration = Integer.parseInt(args[i].substring(12));
- } else if (args[i].startsWith("--host=")) {
- config.host = args[i].substring(7);
- } else if (args[i].startsWith("--port=")) {
- config.port = args[i].substring(7);
- } else if (args[i].startsWith("--maxattempt=")) {
- config.maxattempt = Integer.parseInt(args[i].substring(13));
- }
- }
- Thread master_t = new Thread(new Master(config));
- master_t.start();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement