以下程序用于测试多线程情况下Mysql NDB的各种操作性能。
package com.mydomain.ewanbao;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
public class MysqlBenchMarkForMultiThread extends Thread{
private static int numOfThread = 1;
private static int numOfTable = 1;
private static int numOfOperationPerTrans = 1;
private static String operationType = "";
private int threadIndex = 0;
public static final int PROVISIONING_RECORD_NUMBER = 6000000;
public static final int MAX_RECORD_NUMBER = 1800000;
public MysqlBenchMarkForMultiThread(int threadIndex){
this.threadIndex = threadIndex;
start();
}
public void run(){
if(MysqlBenchMarkForMultiThread.operationType.equalsIgnoreCase("insertBackgroundRecord")){
insertBackgroundRecord();
}else if(MysqlBenchMarkForMultiThread.operationType.equalsIgnoreCase("insertRecord")){
insertRecord();
}else if(operationType.equalsIgnoreCase("selectOnPrimaryKey")){
selectOnPrimaryKey();
}else if(operationType.equalsIgnoreCase("selectOnIntIndex")){
selectOnIntIndex();
}else if(operationType.equalsIgnoreCase("selectOnIntUniqueKey")){
selectOnIntUniqueKey();
}else if(operationType.equalsIgnoreCase("selectOnVarCharIndex")){
selectOnVarCharIndex();
}else if(operationType.equalsIgnoreCase("selectOnVarCharUniqueKey")){
selectOnVarCharUniqueKey();
}else if(operationType.equalsIgnoreCase("updateOnPrimaryKey")){
updateOnPrimaryKey();
}else if(operationType.equalsIgnoreCase("updateOnIntIndex")){
updateOnIntIndex();
}else if(operationType.equalsIgnoreCase("updateOnIntUniqueKey")){
updateOnIntUniqueKey();
}else if(operationType.equalsIgnoreCase("updateOnVarCharIndex")){
updateOnVarCharIndex();
}else if(operationType.equalsIgnoreCase("updateOnVarCharUniqueKey")){
updateOnVarCharUniqueKey();
}else if(operationType.equalsIgnoreCase("deleteOnPrimaryKey")){
deleteOnPrimaryKey();
}else if(operationType.equalsIgnoreCase("deleteOnIntIndex")){
deleteOnIntIndex();
}else if(operationType.equalsIgnoreCase("deleteOnIntUniqueKey")){
deleteOnIntUniqueKey();
}else if(operationType.equalsIgnoreCase("deleteOnVarCharIndex")){
deleteOnVarCharIndex();
}else if(operationType.equalsIgnoreCase("deleteOnVarCharUniqueKey")){
deleteOnVarCharUniqueKey();
}
}
public static void main(String[] args) throws InterruptedException{
List<MysqlBenchMarkForMultiThread> threadList = new LinkedList<MysqlBenchMarkForMultiThread>();
//Analyze the command parameters
parseCommandLine(args);
//initialize the connectionmanager
ConnectionManager.init("com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/test");
//create the sub threads
printCurrentTime();
for(int i=0; i<numOfThread; i++){
threadList.add(new MysqlBenchMarkForMultiThread(i));
}
Iterator<MysqlBenchMarkForMultiThread> itor= threadList.iterator();
while(itor.hasNext()){
((MysqlBenchMarkForMultiThread)itor.next()).join();
}
printCurrentTime();
}
private void insertBackgroundRecord(){
try{
DBConn conn = ConnectionManager.getDBConn (false);
Statement stmt = null;
String dbOperationStr = "";
try{
int size = MysqlBenchMarkForMultiThread.PROVISIONING_RECORD_NUMBER/MysqlBenchMarkForMultiThread.numOfThread;
int start = size * threadIndex;
stmt = conn.createStatement();
for(int i=start; i<(start + size); ){
for(int j=0; j< numOfOperationPerTrans; j++){
String msisdnnumber = generateProvNumber(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();
}
}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{
int size = MysqlBenchMarkForMultiThread.MAX_RECORD_NUMBER/MysqlBenchMarkForMultiThread.numOfThread;
int start = size * threadIndex;
stmt = conn.createStatement();
for(int i=start; i<(start + size); ){
for(int j=0; j< numOfOperationPerTrans; j++){
String msisdnnumber = generateNumber(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();
}
}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{
int size = MysqlBenchMarkForMultiThread.MAX_RECORD_NUMBER/MysqlBenchMarkForMultiThread.numOfThread;
int start = size * threadIndex;
stmt = conn.createStatement();
for(int i=start; i<(start + size); ){
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();
}
}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{
int size = MysqlBenchMarkForMultiThread.MAX_RECORD_NUMBER/MysqlBenchMarkForMultiThread.numOfThread;
int start = size * threadIndex;
stmt = conn.createStatement();
for(int i=start; i<(start + size); ){
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();
}
}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{
int size = MysqlBenchMarkForMultiThread.MAX_RECORD_NUMBER/MysqlBenchMarkForMultiThread.numOfThread;
int start = size * threadIndex;
stmt = conn.createStatement();
for(int i=start; i<(start + size); ){
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();
}
}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{
int size = MysqlBenchMarkForMultiThread.MAX_RECORD_NUMBER/MysqlBenchMarkForMultiThread.numOfThread;
int start = size * threadIndex;
stmt = conn.createStatement();
for(int i=start; i<(start + size); ){
for(int j=0; j< numOfOperationPerTrans; j++){
String msisdnnumber = generateNumber(rand.nextInt(MAX_RECORD_NUMBER));
dbOperationStr = "select * from ENUMDNSCHED where enumdn=/"" + msisdnnumber + "/"";
stmt.execute(dbOperationStr);
i++;
}
conn.commit();
}
}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{
int size = MysqlBenchMarkForMultiThread.MAX_RECORD_NUMBER/MysqlBenchMarkForMultiThread.numOfThread;
int start = size * threadIndex;
stmt = conn.createStatement();
for(int i=start; i<(start + size); ){
for(int j=0; j< numOfOperationPerTrans; j++){
String msisdnnumber = generateNumber(rand.nextInt(MAX_RECORD_NUMBER));
dbOperationStr = "select * from ENUMDNSCHED where uniqueenumdn=/"" + msisdnnumber + "/"";
stmt.execute(dbOperationStr);
i++;
}
conn.commit();
}
}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{
int size = MysqlBenchMarkForMultiThread.MAX_RECORD_NUMBER/MysqlBenchMarkForMultiThread.numOfThread;
int start = size * threadIndex;
stmt = conn.createStatement();
for(int i=start; i<(start + size); ){
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();
}
}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{
int size = MysqlBenchMarkForMultiThread.MAX_RECORD_NUMBER/MysqlBenchMarkForMultiThread.numOfThread;
int start = size * threadIndex;
stmt = conn.createStatement();
for(int i=start; i<(start + size); ){
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();
}
}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{
int size = MysqlBenchMarkForMultiThread.MAX_RECORD_NUMBER/MysqlBenchMarkForMultiThread.numOfThread;
int start = size * threadIndex;
stmt = conn.createStatement();
for(int i=start; i<(start + size); ){
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();
}
}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{
int size = MysqlBenchMarkForMultiThread.MAX_RECORD_NUMBER/MysqlBenchMarkForMultiThread.numOfThread;
int start = size * threadIndex;
stmt = conn.createStatement();
for(int i=start; i<(start + size); ){
for(int j=0; j< numOfOperationPerTrans; j++){
String msisdnnumber = generateNumber(rand.nextInt(MAX_RECORD_NUMBER));
dbOperationStr = "update ENUMDNSCHED set updatelevel=1 where enumdn=/"" + msisdnnumber + "/"";
stmt.execute(dbOperationStr);
i++;
}
conn.commit();
}
}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{
int size = MysqlBenchMarkForMultiThread.MAX_RECORD_NUMBER/MysqlBenchMarkForMultiThread.numOfThread;
int start = size * threadIndex;
stmt = conn.createStatement();
for(int i=start; i<(start + size); ){
for(int j=0; j< numOfOperationPerTrans; j++){
String msisdnnumber = generateNumber(rand.nextInt(MAX_RECORD_NUMBER));
dbOperationStr = "update ENUMDNSCHED set updatelevel=1 where uniqueenumdn=/"" + msisdnnumber + "/"";
stmt.execute(dbOperationStr);
i++;
}
conn.commit();
}
}catch(Exception e){
e.printStackTrace();
}finally{
stmt.close();
ConnectionManager.returnDBConn (conn);
}
}catch(SQLException e){
e.printStackTrace();
}
}
private void deleteOnPrimaryKey(){
try{
DBConn conn = ConnectionManager.getDBConn (false);
Statement stmt = null;
String dbOperationStr = "";
try{
int size = MysqlBenchMarkForMultiThread.MAX_RECORD_NUMBER/MysqlBenchMarkForMultiThread.numOfThread;
int start = size * threadIndex;
stmt = conn.createStatement();
for(int i=start; i<(start + size); ){
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();
}
}catch(Exception e){
e.printStackTrace();
}finally{
stmt.close();
ConnectionManager.returnDBConn (conn);
}
}catch(SQLException e){
e.printStackTrace();
}
}
private void deleteOnIntIndex(){
try{
DBConn conn = ConnectionManager.getDBConn (false);
Statement stmt = null;
String dbOperationStr = "";
try{
int size = MysqlBenchMarkForMultiThread.MAX_RECORD_NUMBER/MysqlBenchMarkForMultiThread.numOfThread;
int start = size * threadIndex;
stmt = conn.createStatement();
for(int i=start; i<(start + size); ){
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();
}
}catch(Exception e){
e.printStackTrace();
}finally{
stmt.close();
ConnectionManager.returnDBConn (conn);
}
}catch(SQLException e){
e.printStackTrace();
}
}
private void deleteOnIntUniqueKey(){
try{
DBConn conn = ConnectionManager.getDBConn (false);
Statement stmt = null;
String dbOperationStr = "";
try{
int size = MysqlBenchMarkForMultiThread.MAX_RECORD_NUMBER/MysqlBenchMarkForMultiThread.numOfThread;
int start = size * threadIndex;
stmt = conn.createStatement();
for(int i=start; i<(start + size); ){
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();
}
}catch(Exception e){
e.printStackTrace();
}finally{
stmt.close();
ConnectionManager.returnDBConn (conn);
}
}catch(SQLException e){
e.printStackTrace();
}
}
private void deleteOnVarCharIndex(){
try{
DBConn conn = ConnectionManager.getDBConn (false);
Statement stmt = null;
String dbOperationStr = "";
try{
int size = MysqlBenchMarkForMultiThread.MAX_RECORD_NUMBER/MysqlBenchMarkForMultiThread.numOfThread;
int start = size * threadIndex;
stmt = conn.createStatement();
for(int i=start; i<(start + size); ){
for(int j=0; j< numOfOperationPerTrans; j++){
String msisdnnumber = generateNumber(i);
dbOperationStr = "delete from ENUMDNSCHED where enumdn=/"" + msisdnnumber + "/"";
stmt.execute(dbOperationStr);
i++;
}
conn.commit();
}
}catch(Exception e){
e.printStackTrace();
}finally{
stmt.close();
ConnectionManager.returnDBConn (conn);
}
}catch(SQLException e){
e.printStackTrace();
}
}
private void deleteOnVarCharUniqueKey(){
try{
DBConn conn = ConnectionManager.getDBConn (false);
Statement stmt = null;
String dbOperationStr = "";
try{
int size = MysqlBenchMarkForMultiThread.MAX_RECORD_NUMBER/MysqlBenchMarkForMultiThread.numOfThread;
int start = size * threadIndex;
stmt = conn.createStatement();
for(int i=start; i<(start + size); ){
for(int j=0; j< numOfOperationPerTrans; j++){
String msisdnnumber = generateNumber(i);
dbOperationStr = "delete from ENUMDNSCHED where uniqueenumdn=/"" + msisdnnumber + "/"";
stmt.execute(dbOperationStr);
i++;
}
conn.commit();
}
}catch(Exception e){
e.printStackTrace();
}finally{
stmt.close();
ConnectionManager.returnDBConn (conn);
}
}catch(SQLException e){
e.printStackTrace();
}
}
private static 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 String generateProvNumber(int index){
/* create the sequence msisdn array*/
Integer[] msisdnArray = new Integer[11];
int result = index;
int remainder = 0;
int pointer = 0;
for(int i=0; i<msisdnArray.length; i++){
msisdnArray[i] = 0;
}
msisdnArray[0] = 9;
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 j=0; j<msisdnArray.length; j++){
str.append(msisdnArray[j].toString());
}
String msisdn = str.toString();
return msisdn;
}
private String generateNumber(int index){
/* create the sequence msisdn array*/
Integer[] msisdnArray = new Integer[11];
int result = index;
int remainder = 0;
int pointer = 0;
for(int i=0; i<msisdnArray.length; i++){
msisdnArray[i] = 0;
}
msisdnArray[0] = 1;
msisdnArray[1] = 3;
msisdnArray[2] = 9;
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 j=0; j<msisdnArray.length; j++){
str.append(msisdnArray[j].toString());
}
String msisdn = str.toString();
return msisdn;
}
private static void printCurrentTime(){
/* record the start time */
Calendar current = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
System.out.println(format.format(current.getTime()));
}
}