Java+Oracleに複数のスレッドでinsert性能検証を実現する方法

Javaコード:
public class ThreadImport {
private String url="jdbc:oracle:thin:@localhost:1521:orcl";
private String user="testuser";
private String password="testpwd";
public Connection getConnect(){
Connection con = null;
try {
Class.forName(“oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection(url, user, password);
} catch (Exception e) {
e.printStackTrace();
}
return con;
}
public void multiThreadImport( final int ThreadNum){
final CountDownLatch dwt= new CountDownLatch(ThreadNum);
long starttime=System.currentTimeMillis();
for(int k=1;k<=ThreadNum;k++){
new Thread(new Runnable() {
@Override
public void run() {
Connection con=getConnect();
try {
Statement st=con.createStatement();
for(int i=1;i<=80000/ThreadNum;i++){
String uuid=UUID.randomUUID().toString();
st.addBatch(“insert into testtbl(userid,usernum) values('"+uuid+"','"+uuid+"')");
if(i%500==0){
st.executeBatch();
}
}
dwt.countDown();
} catch (Exception e) {
}finally{
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}).start();
}
try {
dwt.await();
long spendtime=System.currentTimeMillis()-starttime;
System.out.println(“スレッド数" ThreadNum+"–実行時間:"+spendtime);
} catch (InterruptedException e) {
e.printStackTrace();
}

}

public static void main(String[] args) throws Exception {
ThreadImport tt=new ThreadImport();
tt.multiThreadImport(1);
tt.multiThreadImport(5);
tt.multiThreadImport(8);
tt.multiThreadImport(10);
tt.multiThreadImport(20);
System.out.println(“CPU数:"+Runtime.getRuntime().availableProcessors());
}

}
実行結果:
スレッド数1–実行時間:56407
スレッド数5–実行時間:21588
スレッド数8–実行時間:16525
スレッド数10–実行時間:16198
スレッド数20–実行時間:19582
CPU数:8

Java

Posted by arkgame