Mysql性能1:基于JDBC的MySQL NDB性能测试程序

以下程序用于测试JDBC访问MySQL NDB的性能,稍作修改可以供大家在设计数据库时给予预分析:

 

package com.mydomain.ewanbao;

import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Random;

public class MysqlBenchMark {
 private int numOfThread = 1;
 private int numOfTable =  1;
 private int numOfOperationPerTrans = 1;
 private String operationType = "";
 
 public static final int PROVISIONING_RECORD_NUMBER = 2000000;
 public static final int MAX_RECORD_NUMBER = 900000;
 public static final Integer[] PROVISIONING_BASE = {9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
 public static final Integer[] VERIFY_BASE = {1, 3, 9, 0, 0, 0, 0, 0, 0, 0, 0};
 
 public MysqlBenchMark(){
  ConnectionManager.init("com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/test");
 }
 
 public static void main(String[] args) {
  //Analyze the command parameters
  MysqlBenchMark benchMark = new MysqlBenchMark();
  benchMark.parseCommandLine(args);
  
  
  if(benchMark.operationType.equalsIgnoreCase("createTable")){
   benchMark.createTable();
     }else if(benchMark.operationType.equalsIgnoreCase("insertBackgroundRecord")){
   benchMark.insertBackgroundRecord();
  }else if(benchMark.operationType.equalsIgnoreCase("insertRecord")){
   benchMark.insertRecord();
  }else if(benchMark.operationType.equalsIgnoreCase("selectOnPrimaryKey")){
   benchMark.selectOnPrimaryKey();
  }else if(benchMark.operationType.equalsIgnoreCase("selectOnIntIndex")){
   benchMark.selectOnIntIndex();
  }else if(benchMark.operationType.equalsIgnoreCase("selectOnIntUniqueKey")){
   benchMark.selectOnIntUniqueKey();
  }else if(benchMark.operationType.equalsIgnoreCase("selectOnVarCharIndex")){
   benchMark.selectOnVarCharIndex();
  }else if(benchMark.operationType.equalsIgnoreCase("selectOnVarCharUniqueKey")){
   benchMark.selectOnVarCharUniqueKey();
  }else if(benchMark.operationType.equalsIgnoreCase("updateOnPrimaryKey")){
   benchMark.updateOnPrimaryKey();
  }else if(benchMark.operationType.equalsIgnoreCase("updateOnIntIndex")){
   benchMark.updateOnIntIndex();
  }else if(benchMark.operationType.equalsIgnoreCase("updateOnIntUniqueKey")){
   benchMark.updateOnIntUniqueKey();
  }else if(benchMark.operationType.equalsIgnoreCase("updateOnVarCharIndex")){
   benchMark.updateOnVarCharIndex();
  }else if(benchMark.operationType.equalsIgnoreCase("updateOnVarCharUniqueKey")){
   benchMark.updateOnVarCharUniqueKey();
  }else if(benchMark.operationType.equalsIgnoreCase("deleteOnPrimaryKey")){
   benchMark.deleteOnPrimaryKey();
  }else if(benchMark.operationType.equalsIgnoreCase("deleteOnIntIndex")){
   benchMark.deleteOnIntIndex();
  }else if(benchMark.operationType.equalsIgnoreCase("deleteOnIntUniqueKey")){
   benchMark.deleteOnIntUniqueKey();
  }else if(benchMark.operationType.equalsIgnoreCase("deleteOnVarCharIndex")){
   benchMark.deleteOnVarCharIndex();
  }else if(benchMark.operationType.equalsIgnoreCase("deleteOnVarCharUniqueKey")){
   benchMark.deleteOnVarCharUniqueKey();
  }
 }
 
 private void parseCommandLine(String[] args){
  for(String arg:args){
   if((arg.length() > 13) && (arg.substring(0, 13).equals("--numoftable="))){
    numOfTable = Integer.parseInt(arg.substring(13, arg.length()));
   }
   else if((arg.length() > 14) && (arg.substring(0, 14).equals("--numofthread="))){
    numOfThread = Integer.parseInt(arg.substring(14, arg.length()));
   }
   else if((arg.length() > 16) && (arg.substring(0, 16).equals("--operationtype="))){
    operationType = arg.substring(16, arg.length());
   }
   else if((arg.length() > 26) && (arg.substring(0, 26).equals("--numbofoperationpertrans="))){
    numOfOperationPerTrans = Integer.parseInt(arg.substring(26, arg.length()));
   }
   else{
    System.out.println("usage: ewanbao.mydomain.com.MysqlBenchMark --numoftable=? " +
      "--numofthread=? " +
      "--operationtype=? " +
      "--numbofoperationpertrans=?/n");
    System.exit(-1);
   }
  }
  
  System.out.println("Number of Threads: " + numOfThread + "/n" +
    "Number of Tables: " + numOfTable + "/n" +
    "Number of Operation Per Transaction: " + numOfOperationPerTrans + "/n" +
    "Type of Operation: " + operationType + "/n");
 }
 
 private void createTable(){
  try{
      DBConn conn = ConnectionManager.getDBConn (false);
      Statement stmt = null;
      String dbOperationStr = "";
     
      try{
       stmt = conn.createStatement();
       
       dbOperationStr = "CREATE TABLE ENUMDNSCHED (" +
                                     "id INTEGER NOT NULL PRIMARY KEY, " +
                                     "updatelevel SMALLINT UNSIGNED NOT NULL," +
                                     "enumzoneid SMALLINT UNSIGNED NOT NULL," +
                                     "number INTEGER NOT NULL," +
                                     "uniquenumber INTEGER NOT NULL," +
                                     "enumdn VARCHAR(22) NOT NULL," +
                                     "uniqueenumdn VARCHAR(22) NOT NULL," +
                                     "updatetype SMALLINT UNSIGNED," +
                                     "ttl INTEGER," +
                                     "propblocking TINYINT UNSIGNED," +
                                     "txt VARCHAR(255)," +
                                     "naptrflags VARCHAR(2)," +
                                     "naptrorder SMALLINT UNSIGNED," +
                                     "naptrpreference SMALLINT UNSIGNED," +
                                     "naptrservice VARCHAR(32)," +
                                     "naptrtxt VARCHAR(255)," +
                                     "naptrflags2 VARCHAR(2)," +
                                     "naptrorder2 SMALLINT UNSIGNED," +
                                     "naptrpreference2 SMALLINT UNSIGNED," +
                                     "naptrservice2 VARCHAR(32)," +
                                     "naptrtxt2 VARCHAR(255)," +
                                     "naptrflags3 VARCHAR(2)," +
                                     "naptrorder3 SMALLINT UNSIGNED," +
                                     "naptrpreference3 SMALLINT UNSIGNED," +
                                     "naptrservice3 VARCHAR(32)," +
                                     "naptrtxt3 VARCHAR(255)," +
                                     "naptrflags4 VARCHAR(2)," +
                                     "naptrorder4 SMALLINT UNSIGNED," +
                                     "naptrpreference4 SMALLINT UNSIGNED," +
                                     "naptrservice4 VARCHAR(32)," +
                                     "naptrtxt4 VARCHAR(255)," +
                                     "naptrflags5 VARCHAR(2)," +
                                     "naptrorder5 SMALLINT UNSIGNED," +
                                     "naptrpreference5 SMALLINT UNSIGNED," +
                                     "naptrservice5 VARCHAR(32)," +
                                     "naptrtxt5 VARCHAR(255)," +
                                     "UNIQUE INDEX unique_number_index (uniquenumber)," +
                                     "UNIQUE INDEX unique_enumdn_index (uniqueenumdn), " +
                                     "INDEX number_index (number)," +
                                     "INDEX enumdn_index (enumdn)) ENGINE=NDB;";
       stmt.execute(dbOperationStr);
       
       conn.commit();
      }catch(Exception e){
       e.printStackTrace();
      }finally{
       stmt.close();
       ConnectionManager.returnDBConn (conn);
      }     
  }catch(SQLException e){
   e.printStackTrace();
  }  
 }
 
 private void insertBackgroundRecord(){
  try{
      DBConn conn = ConnectionManager.getDBConn (false);
      Statement stmt = null;
      String dbOperationStr = "";
     
      try{
       printCurrentTime();
       stmt = conn.createStatement();
       
       for(int i=0; i<MAX_RECORD_NUMBER; ){       
        for(int j=0; j< numOfOperationPerTrans; j++){
         String msisdnnumber = generateNumber(PROVISIONING_BASE, i);
         dbOperationStr = "INSERT INTO `ENUMDNSCHED` (`id`, `updatelevel`, `enumzoneid`, `number`, `uniquenumber`, `enumdn`, `uniqueenumdn`, `updatetype`, `propblocking`, `txt`, `naptrflags`, `naptrorder`, `naptrpreference`, `naptrservice`, `naptrtxt`, `naptrflags2`, `naptrorder2`, `naptrpreference2`, `naptrservice2`, `naptrtxt2`, `naptrflags3`, `naptrorder3`, `naptrpreference3`, `naptrservice3`, `naptrtxt3`, `naptrflags4`, `naptrorder4`, `naptrpreference4`, `naptrservice4`, `naptrtxt4`, `naptrflags5`, `naptrorder5`, `naptrpreference5`, `naptrservice5`, `naptrtxt5`) VALUES (" +
                         i + ",0,1," + i + "," + i + ",'" + msisdnnumber + "','"  + msisdnnumber + "',0,0,NULL,'nU',12,10,'E2U+SIP','/^.*$/sip:" + msisdnnumber + "[email protected]/',NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL)";
         stmt.execute(dbOperationStr);
         
         i++;
        }
        conn.commit();        
       }
       
       printCurrentTime();
      }catch(Exception e){
       e.printStackTrace();
      }finally{
       stmt.close();
       ConnectionManager.returnDBConn (conn);
      }     
  }catch(SQLException e){
   e.printStackTrace();
  }  
 }
 
 private void insertRecord(){
  try{
      DBConn conn = ConnectionManager.getDBConn (false);
      Statement stmt = null;
      String dbOperationStr = "";
     
      try{
       printCurrentTime();
       stmt = conn.createStatement();
       
       for(int i=0; i<MAX_RECORD_NUMBER; ){       
        for(int j=0; j< numOfOperationPerTrans; j++){
         String msisdnnumber = generateNumber(VERIFY_BASE, i);
         int value = PROVISIONING_RECORD_NUMBER + i;
         dbOperationStr = "INSERT INTO `ENUMDNSCHED` (`id`, `updatelevel`, `enumzoneid`, `number`, `uniquenumber`, `enumdn`, `uniqueenumdn`, `updatetype`, `propblocking`, `txt`, `naptrflags`, `naptrorder`, `naptrpreference`, `naptrservice`, `naptrtxt`, `naptrflags2`, `naptrorder2`, `naptrpreference2`, `naptrservice2`, `naptrtxt2`, `naptrflags3`, `naptrorder3`, `naptrpreference3`, `naptrservice3`, `naptrtxt3`, `naptrflags4`, `naptrorder4`, `naptrpreference4`, `naptrservice4`, `naptrtxt4`, `naptrflags5`, `naptrorder5`, `naptrpreference5`, `naptrservice5`, `naptrtxt5`) VALUES (" +
                         value + ",0,1," + value + "," + value + ",'" + msisdnnumber + "','"  + msisdnnumber + "',0,0,NULL,'nU',12,10,'E2U+SIP','/^.*$/sip:" + msisdnnumber + "[email protected]/',NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL)";
         stmt.execute(dbOperationStr);
         
         i++;
        }
        conn.commit();        
       }
       
       printCurrentTime();
      }catch(Exception e){
       e.printStackTrace();
      }finally{
       stmt.close();
       ConnectionManager.returnDBConn (conn);
      }     
  }catch(SQLException e){
   e.printStackTrace();
  }  
 }
 
 private void selectOnPrimaryKey(){
  Random rand = new Random();
  try{
      DBConn conn = ConnectionManager.getDBConn (false);
      Statement stmt = null;
      String dbOperationStr = "";
     
      try{
       printCurrentTime();
       stmt = conn.createStatement();
       
       for(int i=0; i<MAX_RECORD_NUMBER; ){       
        for(int j=0; j< numOfOperationPerTrans; j++){
         int value = PROVISIONING_RECORD_NUMBER + rand.nextInt(MAX_RECORD_NUMBER);
         dbOperationStr = "select * from ENUMDNSCHED where id=" + value + ";";
         stmt.execute(dbOperationStr);
         
         i++;
        }
        conn.commit();        
       }
       
       printCurrentTime();
      }catch(Exception e){
       e.printStackTrace();
      }finally{
       stmt.close();
       ConnectionManager.returnDBConn (conn);
      }     
  }catch(SQLException e){
   e.printStackTrace();
  }  
 }
 
 private void selectOnIntIndex(){
  Random rand = new Random();
  try{
      DBConn conn = ConnectionManager.getDBConn (false);
      Statement stmt = null;
      String dbOperationStr = "";
     
      try{
       printCurrentTime();
       stmt = conn.createStatement();
       
       for(int i=0; i<MAX_RECORD_NUMBER; ){       
        for(int j=0; j< numOfOperationPerTrans; j++){
         int value = PROVISIONING_RECORD_NUMBER + rand.nextInt(MAX_RECORD_NUMBER);
         dbOperationStr = "select * from ENUMDNSCHED where number=" + value ;
         stmt.execute(dbOperationStr);
         
         i++;
        }
        conn.commit();        
       }
       
       printCurrentTime();
      }catch(Exception e){
       e.printStackTrace();
      }finally{
       stmt.close();
       ConnectionManager.returnDBConn (conn);
      }     
  }catch(SQLException e){
   e.printStackTrace();
  }  
 }
 
 private void selectOnIntUniqueKey(){
  Random rand = new Random();
  try{
      DBConn conn = ConnectionManager.getDBConn (false);
      Statement stmt = null;
      String dbOperationStr = "";
     
      try{
       printCurrentTime();
       stmt = conn.createStatement();
       
       for(int i=0; i<MAX_RECORD_NUMBER; ){       
        for(int j=0; j< numOfOperationPerTrans; j++){
         int value = PROVISIONING_RECORD_NUMBER + rand.nextInt(MAX_RECORD_NUMBER);
         dbOperationStr = "select * from ENUMDNSCHED where uniquenumber=" + value ;
         stmt.execute(dbOperationStr);
         
         i++;
        }
        conn.commit();        
       }
       
       printCurrentTime();
      }catch(Exception e){
       e.printStackTrace();
      }finally{
       stmt.close();
       ConnectionManager.returnDBConn (conn);
      }     
  }catch(SQLException e){
   e.printStackTrace();
  }  
 } 
 
 private void selectOnVarCharIndex(){
  Random rand = new Random();
  try{
      DBConn conn = ConnectionManager.getDBConn (false);
      Statement stmt = null;
      String dbOperationStr = "";
     
      try{
       printCurrentTime();
       stmt = conn.createStatement();
       
       for(int i=0; i<MAX_RECORD_NUMBER; ){       
        for(int j=0; j< numOfOperationPerTrans; j++){
         String msisdnnumber = generateNumber(VERIFY_BASE, rand.nextInt(MAX_RECORD_NUMBER));
         dbOperationStr = "select * from ENUMDNSCHED where enumdn=/"" + msisdnnumber + "/"";
         stmt.execute(dbOperationStr);
         
         i++;
        }
        conn.commit();        
       }
       
       printCurrentTime();
      }catch(Exception e){
       e.printStackTrace();
      }finally{
       stmt.close();
       ConnectionManager.returnDBConn (conn);
      }     
  }catch(SQLException e){
   e.printStackTrace();
  }  
 }
 
 private void selectOnVarCharUniqueKey(){
  Random rand = new Random();
  try{
      DBConn conn = ConnectionManager.getDBConn (false);
      Statement stmt = null;
      String dbOperationStr = "";
     
      try{
       printCurrentTime();
       stmt = conn.createStatement();
       
       for(int i=0; i<MAX_RECORD_NUMBER; ){       
        for(int j=0; j< numOfOperationPerTrans; j++){
         String msisdnnumber = generateNumber(VERIFY_BASE, rand.nextInt(MAX_RECORD_NUMBER));
         dbOperationStr = "select * from ENUMDNSCHED where uniqueenumdn=/"" + msisdnnumber + "/"";
         stmt.execute(dbOperationStr);
         
         i++;
        }
        conn.commit();        
       }
       
       printCurrentTime();
      }catch(Exception e){
       e.printStackTrace();
      }finally{
       stmt.close();
       ConnectionManager.returnDBConn (conn);
      }     
  }catch(SQLException e){
   e.printStackTrace();
  }  
 }
 
 private void updateOnPrimaryKey(){
  Random rand = new Random();
  try{
      DBConn conn = ConnectionManager.getDBConn (false);
      Statement stmt = null;
      String dbOperationStr = "";
     
      try{
       printCurrentTime();
       stmt = conn.createStatement();
       
       for(int i=0; i<MAX_RECORD_NUMBER; ){       
        for(int j=0; j< numOfOperationPerTrans; j++){
         int value = PROVISIONING_RECORD_NUMBER + rand.nextInt(MAX_RECORD_NUMBER);
         dbOperationStr = "update ENUMDNSCHED set updatelevel=1 where id=" + value + ";";
         stmt.execute(dbOperationStr);
         
         i++;
        }
        conn.commit();        
       }
       
       printCurrentTime();
      }catch(Exception e){
       e.printStackTrace();
      }finally{
       stmt.close();
       ConnectionManager.returnDBConn (conn);
      }     
  }catch(SQLException e){
   e.printStackTrace();
  }  
 }
 
 private void updateOnIntIndex(){
  Random rand = new Random();
  try{
      DBConn conn = ConnectionManager.getDBConn (false);
      Statement stmt = null;
      String dbOperationStr = "";
     
      try{
       printCurrentTime();
       stmt = conn.createStatement();
       
       for(int i=0; i<MAX_RECORD_NUMBER; ){       
        for(int j=0; j< numOfOperationPerTrans; j++){
         int value = PROVISIONING_RECORD_NUMBER + rand.nextInt(MAX_RECORD_NUMBER);
         dbOperationStr = "update ENUMDNSCHED set updatelevel=1 where number=" + value + ";";
         stmt.execute(dbOperationStr);
         
         i++;
        }
        conn.commit();        
       }
       
       printCurrentTime();
      }catch(Exception e){
       e.printStackTrace();
      }finally{
       stmt.close();
       ConnectionManager.returnDBConn (conn);
      }     
  }catch(SQLException e){
   e.printStackTrace();
  }  
 }
 
 private void updateOnIntUniqueKey(){
  Random rand = new Random();
  try{
      DBConn conn = ConnectionManager.getDBConn (false);
      Statement stmt = null;
      String dbOperationStr = "";
     
      try{
       printCurrentTime();
       stmt = conn.createStatement();
       
       for(int i=0; i<MAX_RECORD_NUMBER; ){       
        for(int j=0; j< numOfOperationPerTrans; j++){
         int value = PROVISIONING_RECORD_NUMBER + rand.nextInt(MAX_RECORD_NUMBER);
         dbOperationStr = "update ENUMDNSCHED set updatelevel=1 where uniquenumber=" + value + ";";
         stmt.execute(dbOperationStr);
         
         i++;
        }
        conn.commit();        
       }
       
       printCurrentTime();
      }catch(Exception e){
       e.printStackTrace();
      }finally{
       stmt.close();
       ConnectionManager.returnDBConn (conn);
      }     
  }catch(SQLException e){
   e.printStackTrace();
  }  
 } 
 
 private void updateOnVarCharIndex(){
  Random rand = new Random();
  try{
      DBConn conn = ConnectionManager.getDBConn (false);
      Statement stmt = null;
      String dbOperationStr = "";
     
      try{
       printCurrentTime();
       stmt = conn.createStatement();
       
       for(int i=0; i<MAX_RECORD_NUMBER; ){       
        for(int j=0; j< numOfOperationPerTrans; j++){
         String msisdnnumber = generateNumber(VERIFY_BASE, rand.nextInt(MAX_RECORD_NUMBER));
         dbOperationStr = "update ENUMDNSCHED set updatelevel=1 where enumdn=/"" + msisdnnumber + "/"";
         stmt.execute(dbOperationStr);
         
         i++;
        }
        conn.commit();        
       }
       
       printCurrentTime();
      }catch(Exception e){
       e.printStackTrace();
      }finally{
       stmt.close();
       ConnectionManager.returnDBConn (conn);
      }     
  }catch(SQLException e){
   e.printStackTrace();
  }  
 }
 
 private void updateOnVarCharUniqueKey(){
  Random rand = new Random();
  try{
      DBConn conn = ConnectionManager.getDBConn (false);
      Statement stmt = null;
      String dbOperationStr = "";
     
      try{
       printCurrentTime();
       stmt = conn.createStatement();
       
       for(int i=0; i<MAX_RECORD_NUMBER; ){       
        for(int j=0; j< numOfOperationPerTrans; j++){
         String msisdnnumber = generateNumber(VERIFY_BASE, rand.nextInt(MAX_RECORD_NUMBER));
         dbOperationStr = "update ENUMDNSCHED set updatelevel=1 where uniqueenumdn=/"" + msisdnnumber + "/"";
         stmt.execute(dbOperationStr);
         
         i++;
        }
        conn.commit();        
       }
       
       printCurrentTime();
      }catch(Exception e){
       e.printStackTrace();
      }finally{
       stmt.close();
       ConnectionManager.returnDBConn (conn);
      }     
  }catch(SQLException e){
   e.printStackTrace();
  }  
 } 
 
 private void deleteOnPrimaryKey(){
  Random rand = new Random();
  try{
      DBConn conn = ConnectionManager.getDBConn (false);
      Statement stmt = null;
      String dbOperationStr = "";
     
      try{
       printCurrentTime();
       stmt = conn.createStatement();
       
       for(int i=0; i<MAX_RECORD_NUMBER; ){       
        for(int j=0; j< numOfOperationPerTrans; j++){
         int value = PROVISIONING_RECORD_NUMBER + i;
         dbOperationStr = "delete from ENUMDNSCHED where id=" + value + ";";
         stmt.execute(dbOperationStr);
         
         i++;
        }
        conn.commit();        
       }
       
       printCurrentTime();
      }catch(Exception e){
       e.printStackTrace();
      }finally{
       stmt.close();
       ConnectionManager.returnDBConn (conn);
      }     
  }catch(SQLException e){
   e.printStackTrace();
  }  
 }
 
 private void deleteOnIntIndex(){
  Random rand = new Random();
  try{
      DBConn conn = ConnectionManager.getDBConn (false);
      Statement stmt = null;
      String dbOperationStr = "";
     
      try{
       printCurrentTime();
       stmt = conn.createStatement();
       
       for(int i=0; i<MAX_RECORD_NUMBER; ){       
        for(int j=0; j< numOfOperationPerTrans; j++){
         int value = PROVISIONING_RECORD_NUMBER + i;
         dbOperationStr = "delete from ENUMDNSCHED where number=" + value + ";";
         stmt.execute(dbOperationStr);
         
         i++;
        }
        conn.commit();        
       }
       
       printCurrentTime();
      }catch(Exception e){
       e.printStackTrace();
      }finally{
       stmt.close();
       ConnectionManager.returnDBConn (conn);
      }     
  }catch(SQLException e){
   e.printStackTrace();
  }  
 }
 
 private void deleteOnIntUniqueKey(){
  Random rand = new Random();
  try{
      DBConn conn = ConnectionManager.getDBConn (false);
      Statement stmt = null;
      String dbOperationStr = "";
     
      try{
       printCurrentTime();
       stmt = conn.createStatement();
       
       for(int i=0; i<MAX_RECORD_NUMBER; ){       
        for(int j=0; j< numOfOperationPerTrans; j++){
         int value = PROVISIONING_RECORD_NUMBER + i;
         dbOperationStr = "delete from ENUMDNSCHED where uniquenumber=" + value + ";";
         stmt.execute(dbOperationStr);
         
         i++;
        }
        conn.commit();        
       }
       
       printCurrentTime();
      }catch(Exception e){
       e.printStackTrace();
      }finally{
       stmt.close();
       ConnectionManager.returnDBConn (conn);
      }     
  }catch(SQLException e){
   e.printStackTrace();
  }  
 } 
 
 private void deleteOnVarCharIndex(){
  Random rand = new Random();
  try{
      DBConn conn = ConnectionManager.getDBConn (false);
      Statement stmt = null;
      String dbOperationStr = "";
     
      try{
       printCurrentTime();
       stmt = conn.createStatement();
       
       for(int i=0; i<MAX_RECORD_NUMBER; ){       
        for(int j=0; j< numOfOperationPerTrans; j++){
         String msisdnnumber = generateNumber(VERIFY_BASE, i);
         dbOperationStr = "delete from ENUMDNSCHED where enumdn=/"" + msisdnnumber + "/"";
         stmt.execute(dbOperationStr);
         
         i++;
        }
        conn.commit();        
       }
       
       printCurrentTime();
      }catch(Exception e){
       e.printStackTrace();
      }finally{
       stmt.close();
       ConnectionManager.returnDBConn (conn);
      }     
  }catch(SQLException e){
   e.printStackTrace();
  }  
 }
 
 private void deleteOnVarCharUniqueKey(){
  Random rand = new Random();
  try{
      DBConn conn = ConnectionManager.getDBConn (false);
      Statement stmt = null;
      String dbOperationStr = "";
     
      try{
       printCurrentTime();
       stmt = conn.createStatement();
       
       for(int i=0; i<MAX_RECORD_NUMBER; ){       
        for(int j=0; j< numOfOperationPerTrans; j++){
         String msisdnnumber = generateNumber(VERIFY_BASE, i);
         dbOperationStr = "delete from ENUMDNSCHED where uniqueenumdn=/"" + msisdnnumber + "/"";
         stmt.execute(dbOperationStr);
         
         i++;
        }
        conn.commit();        
       }
       
       printCurrentTime();
      }catch(Exception e){
       e.printStackTrace();
      }finally{
       stmt.close();
       ConnectionManager.returnDBConn (conn);
      }     
  }catch(SQLException e){
   e.printStackTrace();
  }  
 }
 
 private String generateNumber(Integer[] base, int index){
  /* create the sequence msisdn array*/
  Integer[] msisdnArray = base;
  int result = index;
  int remainder = 0;
  int pointer = 0;
  while (result != 0) {
   remainder = result % 10;
   result = result / 10;
  
   msisdnArray[msisdnArray.length - pointer - 1] = remainder;
   pointer++;
  }
  
  /* create the msisdn */
  StringBuffer str = new StringBuffer();
  for(int i=0; i<msisdnArray.length; i++){
   str.append(msisdnArray[i].toString());
  }
  String msisdn = str.toString();
  
  return msisdn;
 }
 
 private void printCurrentTime(){
        /* record the start time */
        Calendar current = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
        System.out.println(format.format(current.getTime()));  
 }
}

你可能感兴趣的:(Mysql性能1:基于JDBC的MySQL NDB性能测试程序)