Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.51 KB | None | 0 0
  1.  
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.ResultSet;
  5. import java.io.*;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.util.concurrent.LinkedBlockingQueue;
  9.  
  10. public class Main extends java.lang.Thread {
  11.  
  12.     private static class Config {
  13.  
  14.         public String username = "root";
  15.         public String password = "";
  16.         public String host = "127.0.0.1";
  17.         public String port = "3306";
  18.         public String infile = "";
  19.         public int thread = 1;
  20.         public int iteration = 1;
  21.         public int maxattempt = 1;
  22.     }
  23.  
  24.     private static class Master implements Runnable {
  25.  
  26.         private LinkedBlockingQueue<String> queue = new LinkedBlockingQueue<String>(100000);
  27.         private Config conf = null;
  28.  
  29.         public Master(Config config) {
  30.             this.conf = config;
  31.         }
  32.  
  33.         public void run() {
  34.             Thread t[] = new Thread[conf.thread];
  35.             for (int i = 0; i < conf.thread; i++) {
  36.                 t[i] = new Thread(new Worker(i, queue, conf));
  37.             }
  38.             for (int i = 0; i < conf.thread; i++) {
  39.                 t[i].start();
  40.             }
  41.             for (int i = 0; i < conf.iteration; i++) {
  42.                 try {    // Create an input stream, reading from the specified file
  43.                     BufferedReader reader = new BufferedReader(
  44.                             new InputStreamReader(
  45.                             new FileInputStream(conf.infile)));
  46.                     String in;
  47.                     while ((in = reader.readLine()) != null) {
  48.                         try {
  49.                             queue.put(in);
  50.                             Thread.sleep(5);
  51.                         } catch (Exception e) {
  52.                             e.printStackTrace();
  53.                         }
  54.                     }
  55.                     reader.close();
  56.                 } catch (IOException ioe) {
  57.                     System.out.println("I/O error - " + ioe);
  58.                 }
  59.             }
  60.             for (int i = 0; i < conf.thread; i++) {
  61.                 try {
  62.                     queue.put("STOP");
  63.                 } catch (InterruptedException ie) {
  64.                     ie.printStackTrace();
  65.                 }
  66.             }
  67.         }
  68.     }
  69.  
  70.     private static class Worker implements Runnable {
  71.  
  72.         private LinkedBlockingQueue<String> queue = null;
  73.         private Config conf = null;
  74.         private int thread_id = -1;
  75.         private QueryRunner qr;
  76.  
  77.         public Worker(int thread_id, LinkedBlockingQueue<String> queue, Config config) {    // Assign to member variable
  78.             this.queue = queue;
  79.             this.conf = config;
  80.             this.thread_id = thread_id;
  81.         }
  82.  
  83.         private class QueryRunner {
  84.  
  85.             String hostname, port, username, password;
  86.             int retries;
  87.             Connection conn = null;
  88.             Statement s = null;
  89.  
  90.             public QueryRunner(String hostname, String port, String username, String password, int retries) {
  91.                 this.hostname = hostname;
  92.                 this.port = port;
  93.                 this.username = username;
  94.                 this.password = password;
  95.                 this.retries = retries;
  96.                 connect();
  97.             }
  98.  
  99.             private void connect() {
  100.                 try {
  101.                     String url = "jdbc:mysql://" + hostname + ":" + port + "/";
  102.                     Class.forName("com.mysql.jdbc.Driver").newInstance();
  103.                     conn = DriverManager.getConnection(url, username, password);
  104.                     s = conn.createStatement();
  105.                 } catch (SQLException sqle) {
  106.                     sqle.printStackTrace();
  107.                 } catch (Exception e) {
  108.                     e.printStackTrace();
  109.                 }
  110.             }
  111.  
  112.             private boolean refresh() {
  113.                 int tries = 0;
  114.                 boolean buf = false;
  115.                 try {
  116.                     while (!(buf = !conn.isClosed()) && tries < retries) {
  117.                         tries++;
  118.                         connect();
  119.                     }
  120.                 } catch (SQLException sqle) {
  121.                     sqle.printStackTrace();
  122.                 }
  123.                 return buf;
  124.             }
  125.  
  126.             public boolean query(String q) {
  127.                 boolean buf = false;
  128.                 try {
  129.                     if (refresh()) {
  130.                         ResultSet rs = s.executeQuery(q);
  131.                         buf = true;
  132.                     }
  133.                 } catch (SQLException sqle) {
  134.                     sqle.printStackTrace();
  135.                 }
  136.                 return buf;
  137.             }
  138.  
  139.             public void end() {
  140.                 try {
  141.                     s.close();
  142.                     conn.close();
  143.                 } catch (SQLException sqle) {
  144.                     sqle.printStackTrace();
  145.                 }
  146.             }
  147.         }
  148.  
  149.         public void run() {
  150.             qr = new QueryRunner(conf.host, conf.port, conf.username, conf.password, conf.maxattempt);
  151.             long startThreadTime = System.currentTimeMillis();
  152.             java.util.Date dt = new java.util.Date();
  153.             dt.setTime(startThreadTime);
  154.             System.out.println("Thread " + thread_id + " start. " + dt);
  155.             String receivedString = "";
  156.             String logStr = "";
  157.             Boolean stop = false;
  158.             String[] queries = null;
  159.             while (!stop) {
  160.                 try {
  161.                     receivedString = queue.take();
  162.                 } catch (InterruptedException ie) {
  163.                     ie.printStackTrace();
  164.                 }
  165.                 if (receivedString != null) {
  166.                     if (receivedString.equals("STOP")) {
  167.                         stop = true;
  168.                     } else {
  169.                         long startQueryTime = System.currentTimeMillis();
  170.                         //lakukan berulang ulang selama belum berhasil dan paling banyak maxattempt
  171.                         queries = receivedString.split(";");
  172.                         for (int i = 0; i < queries.length; i++) {
  173.                             queries[i] = queries[i].trim();
  174.                         }
  175.                         String test = "";
  176.                         logStr = "Execution " + thread_id + "\t" + dt;
  177.                         for (int i = 0; i < queries.length; i++) {
  178.                             test = queries[i].toLowerCase();
  179.                             if (test.startsWith("use") || test.startsWith("select")) {
  180.                                 dt.setTime(startQueryTime);
  181.                                 qr.query(queries[i]);
  182.                             }
  183.                         }
  184.                         long endQueryTime = System.currentTimeMillis();
  185.                         dt.setTime(endQueryTime);
  186.                         logStr = logStr + "\t" + dt;
  187.                         logStr = logStr + "\t" + (endQueryTime - startQueryTime) + "\t" + queries[1];
  188.                         System.out.println(logStr);
  189.                     }
  190.                 }
  191.             }
  192.             long endThreadTime = System.currentTimeMillis();
  193.             System.out.println("Thread " + thread_id + " stop. " + (endThreadTime - startThreadTime) + " milliseconds");
  194.             qr.end();
  195.         }
  196.     }
  197.  
  198.     public static void main(String args[]) throws IOException {
  199.         Config config = new Config();
  200.         for (int i = 0; i < args.length; i++) {
  201.             if (args[i].startsWith("-u")) {
  202.                 config.username = args[i].substring(2);
  203.             } else if (args[i].startsWith("-p")) {
  204.                 config.password = args[i].substring(2);
  205.             } else if (args[i].startsWith("--batchfile=")) {
  206.                 config.infile = args[i].substring(12);
  207.             } else if (args[i].startsWith("--thread=")) {
  208.                 config.thread = Integer.parseInt(args[i].substring(9));
  209.             } else if (args[i].startsWith("--iteration=")) {
  210.                 config.iteration = Integer.parseInt(args[i].substring(12));
  211.             } else if (args[i].startsWith("--host=")) {
  212.                 config.host = args[i].substring(7);
  213.             } else if (args[i].startsWith("--port=")) {
  214.                 config.port = args[i].substring(7);
  215.             } else if (args[i].startsWith("--maxattempt=")) {
  216.                 config.maxattempt = Integer.parseInt(args[i].substring(13));
  217.             }
  218.         }
  219.         Thread master_t = new Thread(new Master(config));
  220.         master_t.start();
  221.     }
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement