// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Random;

public class MySqlPerf {
    private static Random rng = new Random();
    private static final int NROW = 10000;
    private static final int REP = 50;
    private static final String T = "This is a small test";
    private static Connection con;
    public static void create() throws SQLException {
        Statement stmt = con.createStatement();
        stmt.executeUpdate("CREATE TABLE ptest(id INTEGER NOT NULL PRIMARY KEY, s VARCHAR(50))");
        stmt.close();
    }
    public static void insert() throws SQLException {
        long t1 = System.currentTimeMillis();
        Statement stmt = con.createStatement();
        for(int i = 0; i < NROW; i++) {
            stmt.executeUpdate("INSERT INTO ptest VALUES(" + (i + 1) + ",'" + T + "')");
        }
        stmt.close();
        long t2 = System.currentTimeMillis();
        System.out.println("INSERT " + NROW + " rows: " + (t2 - t1) + " milliseconds");
    }
    public static void selectAll() throws SQLException {
        long t1 = System.currentTimeMillis();
        for(int j = 0; j < REP; j++) {
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT * FROM ptest");
            while(rs.next()) {
                int id = rs.getInt(1);
                String s = rs.getString(2);
                if(id <= 0 || !s.equals(T)) {
                    throw new RuntimeException("Ooops");
                }
            }
            stmt.close();
        }
        long t2 = System.currentTimeMillis();
        System.out.println("SELECT " + NROW + " rows " + REP + " times: " + (t2 - t1) + " milliseconds");
    }
    public static void selectRandom() throws SQLException {
        long t1 = System.currentTimeMillis();
        for(int i = 0; i < NROW; i++) {
            Statement stmt = con.createStatement();
            int id = rng.nextInt(NROW) + 1;
            ResultSet rs = stmt.executeQuery("SELECT s FROM ptest WHERE id = " + id);
            if(rs.next()) {
                String s = rs.getString(1);
                if(!s.equals(T)) {
                    throw new RuntimeException("Ooops");
                }
            } else {
                throw new RuntimeException("Ooops");
            }
            stmt.close();
        }
        long t2 = System.currentTimeMillis();
        System.out.println("SELECT 1 row " + NROW + " times: " + (t2 - t1) + " milliseconds");
    }
    public static void drop() throws SQLException {
        Statement stmt = con.createStatement();
        stmt.executeUpdate("DROP TABLE ptest");
        stmt.close();
    }
    public static void run(String name, String driver, String conurl) throws ClassNotFoundException, SQLException {
        System.out.println(name + ":");
        Class.forName(driver);
        con = DriverManager.getConnection(conurl);
        create();
        insert();
        selectAll();
        selectRandom();
        drop();
        con.close();
    }
    public static void main(String[] args) throws Exception {
        for(int k = 0; k < 3; k++) {
            run("MySQL Connector/J", "com.mysql.jdbc.Driver", "jdbc:mysql://localhost/Test");
            run("JDBC ODBC bridge", "sun.jdbc.odbc.JdbcOdbcDriver", "jdbc:odbc:;Driver={MySQL ODBC 3.51 Driver};Server=localhost;Option=16834;Database=Test;");
        }
    }
}
