// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
package cache.ignitejdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

public class TestPerformance {
    private static void put(Connection con, String key, Integer value) throws SQLException {
        PreparedStatement pstmt = con.prepareStatement("MERGE INTO performance_cache(ckey,cvalue) VALUES(?,?)");
        pstmt.setString(1, key);
        pstmt.setInt(2, value);
        pstmt.executeUpdate();
        pstmt.close();
    }
    private static final int N = 10000;
    private static void test(Connection con) throws InterruptedException, SQLException {
        // put speed
        long t1 = System.currentTimeMillis();
        for(int i = 0; i < N; i++) {
            put(con, "K#" + (i + 1), i + 1);
        }
        long t2 = System.currentTimeMillis();
        System.out.printf("%d puts per second\n", N * 1000 / (t2 - t1));
    }
    private static final int REP = 5;
    public static void main(String[] args) throws Exception {
        Connection con = DriverManager.getConnection("jdbc:ignite:thin://localhost:10800");
        Statement stmt = con.createStatement();
        stmt.executeUpdate("CREATE TABLE IF NOT EXISTS performance_cache (ckey VARCHAR(16) NOT NULL, cvalue INTEGER NOT NULL, PRIMARY KEY(ckey))");
        stmt.close();
        for(int i = 0; i < REP; i++) {
            test(con);
        }
        con.close();
    }
}
