jdbc压力测试单个sql的执行速度,jdbc压力测试sql,大家知道一个sql在DB
分享于 点击 25843 次 点评:4
jdbc压力测试单个sql的执行速度,jdbc压力测试sql,大家知道一个sql在DB
大家知道一个sql在DBMS里面执行很快,但是并发可能执行很慢,就写了个小Java程序来并发测试下SQL的并发执行速度。
依赖:你的数据的jdbc驱动、commons-io
package test;import java.io.File;import java.io.FileInputStream;import java.io.InputStream;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.SQLException;import java.util.ArrayList;import java.util.List;import java.util.Scanner;import org.apache.commons.io.IOUtils;public class SqlPreess{ //并发执行sql数 private static final int PARALLEL_COUNT = 100; //多少个PreparedStatement共用一个数据库连接 private static final int PER_CONNECT_STAT = 2; //驱动类 private static final String JDBC_DRIVER_CLASS = "org.postgresql.Driver";//JDBC连接 private static final String JDBC_URL = "jdbc:postgresql://192.168.102.165:5432/aischool"; static { try { Class.forName(JDBC_DRIVER_CLASS); } catch (ClassNotFoundException e) { e.printStackTrace(); } } public static void main(String[] args) throws Exception { List<PressThread> list = new ArrayList<PressThread>(); List<Connection> cons = new ArrayList<Connection>(); System.out.println("Prepare Press,Waiting...."); //sql.txt是你的sql文件放的地方 InputStream is = new FileInputStream(new File("e:/sql.txt")); String sql = IOUtils.toString(is); is.close(); Connection c = getConnection(); cons.add(c); int conFlag = 0; for (int i = 0; i < PARALLEL_COUNT; i++) { if (conFlag >= PER_CONNECT_STAT) { c = getConnection(); cons.add(c); conFlag = 0; } PressThread t = new PressThread(Integer.MAX_VALUE, c.prepareStatement(sql)); t.setDaemon(true); list.add(t); conFlag++; } MonitorThread mt = new MonitorThread(list); System.out.println("Prepare Complete,Executing...."); mt.start(); Scanner s = new Scanner(System.in); while (!"stop".equals(s.next())) { } mt.interrupt(); mt.join(); for (PressThread pressThread : list) { try { pressThread.close(); pressThread.interrupt(); } catch (Exception e) { } } for (Connection connection : cons) { connection.close(); } } private static Connection getConnection() throws SQLException { return DriverManager.getConnection(JDBC_URL, "postgres", "123456"); } private static class PressThread extends Thread { private volatile long cost = 0; private volatile int times = 0; private volatile boolean keepRun = true; private int totalTimes; private PreparedStatement s; public PressThread(int totalTimes, PreparedStatement s) { super(); this.totalTimes = totalTimes; this.s = s; } public void close() { keepRun = false; if (s != null) { try { s.close(); } catch (SQLException e) { } } } @Override public void run() { try { boolean passFirst = false; for (int i = 0; i < totalTimes; i++) { long start = System.currentTimeMillis(); s.execute(); if (!passFirst) { passFirst = true; continue; } cost += (System.currentTimeMillis() - start); times++; // System.out.println(this.getName() + ":" + (cost / times)); if (!keepRun) { return; } } } catch (SQLException e) { e.printStackTrace(); } } } private static class MonitorThread extends Thread { private List<PressThread> threadList; public MonitorThread(List<PressThread> threadList) { super(); this.threadList = threadList; } @Override public void run() { for (PressThread pressThread : threadList) { pressThread.start(); } for (int i = 0; i < 60; i++) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); return; } long totaCost = 0; int totalTimes = 0; for (PressThread pressThread : threadList) { totaCost += pressThread.cost; totalTimes += pressThread.times; } long perExeCost = totalTimes == 0 ? 0 : totaCost / totalTimes;// by zero System.out.println("\\n共耗时:" + totaCost + "ms"); System.out.println("总执行次数:" + totalTimes + "次"); System.out.println("平均:" + perExeCost + "ms/次"); } } }}//该片段来自于http://byrx.net
用户点评