4BenchmarkSQL数据库测试工具代码关于导入类概论.docx
《4BenchmarkSQL数据库测试工具代码关于导入类概论.docx》由会员分享,可在线阅读,更多相关《4BenchmarkSQL数据库测试工具代码关于导入类概论.docx(34页珍藏版)》请在冰豆网上搜索。
![4BenchmarkSQL数据库测试工具代码关于导入类概论.docx](https://file1.bdocx.com/fileroot1/2023-1/21/c8661148-ead5-43f9-b23c-14e52537e166/c8661148-ead5-43f9-b23c-14e52537e1661.gif)
4BenchmarkSQL数据库测试工具代码关于导入类概论
4.BenchmarkSQL数据库测试工具代码——关于导入类
导入类LoadData继续与配置类jTPCCConfig。
开始先设置私有静态变量,包括JDBC协议变量,通用变量。
1.主函数MAIN
设置warehouse变量,默认继承于配置类中的静态值。
如果参数有有numwarehouses,那么紧接其后的参数就是新的warehouse值。
此外还可以设置参数filelocation,如果存在该参数,紧接其后的参数就是新的filelocation值。
如果没有设置filelocation,则调用initJDBC函数。
完事后,初始化随机种子。
接着输出开始加载数据的时间。
然后调用loadWhse函数加载warehouse
然后调用loadItem
然后调用loadStock
然后调用loadDist
然后调用loadCust
然后调用loadOrder
然后输出结束时间。
最后根据需要(因为可能没有进行连接)关闭JDBC连接。
2.initJDBC
initJDBC函数,先加载属性文件
获取属性文件中的配置,driver,conn,user,password
加载driver变量设置的数据库驱动。
然后获得连接conn,关闭自动提交。
然后在数据库中创建语法对象(stmt=conn.createStatement()),接着创建9个表的预准备语句对象。
如果出错就回滚。
3.loadWhse
WAREHOUSE表不会随着测试而变化。
先输出导入多少WAREHOUSE。
如果设置了filelocation,则导出到warehouse.csv文件。
先实例化warehouse类。
然后根据warehouse数量,进行循环导入数据。
其中调用jTPCCUtil中的函数randomNumber和randomStr.
然后调用executeUpdate来执行SQL语句。
4.loadItem
导入物品ITEM。
共100000个物品。
这个表空间测试时候不会增加。
Item名字随机产生
i_price物品价格随机1~100的浮点。
其中i_data列:
90%的概率随机26~50长度的字符串
10%的概率中间带字符串ORIGINAL
I_im_id是1~10000随机值。
执行SQL的时候,为了提高性能,也是每次执行10000个。
5.loadStock
stock的记录数量是warehouse乘以100000.测试过程不会发生变化。
然后循环插入,每个物品的物品需要设置warehouse的ID和item的ID。
数量是随机10~100.
表中s_data列,90%概率是随机长度26~50的字符串
10%概率是字符串中间包含ORIGINAL字符
此外列s_dist_01是随机长度为24的字符串。
为了提高效率,进行批量插入,每次处理10000行。
6.loadDist
district表在测试过程中刚也不会变化。
数量为WAREHOUSE数量乘以10
根据WAREHOUSE_ID和D_ID来插入行,
其中列d_tax是0~0.2
列d_name是长度为6~10的字符串
列d_street_1是长度为10~20的字符串
列d_state是长度为3的字符串
列d_zip都是123456789
然后执行语句。
7.loadCust
加载CUSTOMER表和HISTORY表。
其中表HISTORY随着测试会发生变化,CUSTOMER表不会变化。
每插入一行到CUSTOMER,需要插入一行到HISTORY表中。
数量时WAREHOUSE乘以10乘以3000.(每个WAREHOUSE有10个分区,每个分区服务3000个客户)
主要有列c_id,c_d_id,c_w_id,
此外c_discount是0.01%到50%。
C_credit列,10%概率是BC(BadCredit),90%概率是GC(GoodCredit)
表history中的列h_c_id,h_c_d_id,h_c_w_id,h_d_id,h_w_id和customer基本一致。
提高效率也是10000个10000个进行插入执行。
8.loadOrder
加载ORDERLINE,OORDER,NEW_ORDER表。
这三个表在测试过程中发生变化。
该函数调用jdbcIO类。
jdbcIO类主要实现插入ORDER、NEW_ORDER、ORDERLINE表中。
列o_ol_cnt是随机5~15,表示订单中物品数量。
1都插入到oorder表中。
2如果用户ID大于2100时候,插入到new_order表中。
(30%)
3此外根据订单中数量(1条插入到order表中平均对应10条),插入到order_line表,其中ol_i_id是1~100000中的随机数,ol_o_id是客户id.如果ol_o_id小于2101(70%),则设置变量ol_amount=0,大于则设置为随机0.01~9999.99(30%)
Ol_supply_w_id设置为随机1~仓库数量,提供物品的WAREHOUSE_ID
Ol_qunantity是订单中该物品的数量为5。
为了提高插入效率,也是10000个为单位进行插入。
9.源码如下:
importjava.sql.*;
importjava.util.*;
importjava.io.*;
importjava.lang.Integer;
publicclassLoadDataimplementsjTPCCConfig{
//***********JDBCspecificvariables***********************
privatestaticConnectionconn=null;
privatestaticStatementstmt=null;
privatestaticjava.sql.Timestampsysdate=null;
privatestaticPreparedStatementcustPrepStmt;
privatestaticPreparedStatementdistPrepStmt;
privatestaticPreparedStatementhistPrepStmt;
privatestaticPreparedStatementitemPrepStmt;
privatestaticPreparedStatementnworPrepStmt;
privatestaticPreparedStatementordrPrepStmt;
privatestaticPreparedStatementorlnPrepStmt;
privatestaticPreparedStatementstckPrepStmt;
privatestaticPreparedStatementwhsePrepStmt;
//**********generalvars**********************************
privatestaticjava.util.Datenow=null;
privatestaticjava.util.DatestartDate=null;
privatestaticjava.util.DateendDate=null;
privatestaticRandomgen;
privatestaticStringdbType;
privatestaticintnumWarehouses=0;
privatestaticStringfileLocation="";
privatestaticbooleanoutputFiles=false;
privatestaticPrintWriterout=null;
privatestaticlonglastTimeMS=0;
publicstaticvoidmain(String[]args){
System.out.println("StartingBenchmarkSQLLoadData");
System.out.println("-----------------Initialization-------------------");
numWarehouses=configWhseCount;
for(inti=0;i{
System.out.println(args[i]);
Stringstr=args[i];
if(str.toLowerCase().startsWith("numwarehouses"))
{
Stringval=args[i+1];
numWarehouses=Integer.parseInt(val);
}
if(str.toLowerCase().startsWith("filelocation"))
{
fileLocation=args[i+1];
outputFiles=true;
}
}
if(outputFiles==false){
initJDBC();
}
//seedtherandomnumbergenerator
gen=newRandom(System.currentTimeMillis());
//#########################MAINLINE######################################
startDate=newjava.util.Date();
System.out.println("");
System.out.println("-------------LoadDataStartTime="+startDate+
"-------------");
longstartTimeMS=newjava.util.Date().getTime();
lastTimeMS=startTimeMS;
System.out.println("");
longtotalRows=loadWhse(numWarehouses);
System.out.println("");
totalRows+=loadItem(configItemCount);
System.out.println("");
totalRows+=loadStock(numWarehouses,configItemCount);
System.out.println("");
totalRows+=loadDist(numWarehouses,configDistPerWhse);
System.out.println("");
totalRows+=loadCust(numWarehouses,configDistPerWhse,configCustPerDist);
System.out.println("");
totalRows+=loadOrder(numWarehouses,configDistPerWhse,configCustPerDist);
longrunTimeMS=(newjava.util.Date().getTime())+1-startTimeMS;
endDate=newjava.util.Date();
System.out.println("");
System.out.println("-------------LoadJDBCStatistics--------------------");
System.out.println("StartTime="+startDate);
System.out.println("EndTime="+endDate);
System.out.println("RunTime="+(int)runTimeMS/1000+"Seconds");
System.out.println("RowsLoaded="+totalRows+"Rows");
System.out.println("RowsPerSecond="+(totalRows/(runTimeMS/1000))+"Rows/Sec");
System.out.println("------------------------------------------------------");
//exitCleanly
try{
if(outputFiles==false)
{
if(conn!
=null)
conn.close();
}
}catch(SQLExceptionse){
se.printStackTrace();
}//endtry
}//endmain
staticvoidtransRollback(){
if(outputFiles==false)
{
try{
conn.rollback();
}catch(SQLExceptionse){
System.out.println(se.getMessage());
}
}else{
out.close();
}
}
staticvoidtransCommit(){
if(outputFiles==false)
{
try{
mit();
}catch(SQLExceptionse){
System.out.println(se.getMessage());
transRollback();
}
}else{
out.close();
}
}
staticvoidinitJDBC(){
try{
//loadtheinifile
Propertiesini=newProperties();
ini.load(newFileInputStream(System.getProperty("prop")));
//displaythevaluesweneed
System.out.println("driver="+ini.getProperty("driver"));
System.out.println("conn="+ini.getProperty("conn"));
System.out.println("user="+ini.getProperty("user"));
System.out.println("password=******");
//RegisterjdbcDriver
Class.forName(ini.getProperty("driver"));
//makeconnection
conn=DriverManager.getConnection(ini.getProperty("conn"),
ini.getProperty("user"),ini.getProperty("password"));
conn.setAutoCommit(false);
//CreateStatement
stmt=conn.createStatement();
distPrepStmt=conn.prepareStatement
("INSERTINTObenchmarksql.district"+
"(d_id,d_w_id,d_ytd,d_tax,d_next_o_id,d_name,d_street_1,d_street_2,d_city,d_state,d_zip)"+
"VALUES(?
?
?
?
?
?
?
?
?
?
?
)");
itemPrepStmt=conn.prepareStatement
("INSERTINTObenchmarksql.item"+
"(i_id,i_name,i_price,i_data,i_im_id)"+
"VALUES(?
?
?
?
?
)");
custPrepStmt=conn.prepareStatement
("INSERTINTObenchmarksql.customer"+
"(c_id,c_d_id,c_w_id,"+
"c_discount,c_credit,c_last,c_first,c_credit_lim,"+
"c_balance,c_ytd_payment,c_payment_cnt,c_delivery_cnt,"+
"c_street_1,c_street_2,c_city,c_state,c_zip,"+
"c_phone,c_since,c_middle,c_data)"+
"VALUES(?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
)");
histPrepStmt=conn.prepareStatement
("INSERTINTObenchmarksql.history"+
"(hist_id,h_c_id,h_c_d_id,h_c_w_id,"+
"h_d_id,h_w_id,"+
"h_date,h_amount,h_data)"+
"VALUES(?
?
?
?
?
?
?
?
?
)");
ordrPrepStmt=conn.prepareStatement
("INSERTINTObenchmarksql.oorder"+
"(o_id,o_w_id,o_d_id,o_c_id,"+
"o_carrier_id,o_ol_cnt,o_all_local,o_entry_d)"+
"VALUES(?
?
?
?
?
?
?
?
)");
orlnPrepStmt=conn.prepareStatement
("INSERTINTObenchmarksql.order_line"+
"(ol_w_id,ol_d_id,ol_o_id,"+
"ol_number,ol_i_id,ol_delivery_d,"+
"ol_amount,ol_supply_w_id,ol_quantity,ol_dist_info)"+
"VALUES(?
?
?
?
?
?
?
?
?
?
)");
nworPrepStmt=conn.prepareStatement
("INSERTINTObenchmarksql.new_order"+
"(no_w_id,no_d_id,no_o_id)"+
"VALUES(?
?
?
)");
stckPrepStmt=conn.prepareStatement
("INSERTINTObenchmarksql.stock"+
"(s_i_id,s_w_id,s_quantity,s_ytd,s_order_cnt,s_remote_cnt,s_data,"+
"s_dist_01,s_dist_02,s_dist_03,s_dist_04,s_dist_05,"+
"s_dist_06,s_dist_07,s_dist_08,s_dist_09,s_dist_10)"+
"VALUES(?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
)");
whsePrepStmt=conn.prepareStatement
("INSERTINTObenchmarksql.warehouse"+
"(w_id,w_ytd,w_tax,w_name,w_street_1,w_street_2,w_city,w_state,w_zip)"+
"VALUES(?
?
?
?
?
?
?
?
?
)");
}catch(SQLExceptionse){
System.out.println(se.getMessage());
transRollback();
}catch(Exceptione){
e.printStackTrace();
transRollback();
}//endtry
}//endinitJDBC()
staticintloadItem(intitemKount){
intk=0;
intt=0;
intrandPct=0;
intlen=0;
intstartORIGINAL=0;
try{
now=newjava.util.Date();
t=itemKount;
System.out.println("StartItemLoadfor"+t+"Items@"+now+"...");
if(outputFiles==true)
{
out=newPrintWriter(newFileOutputStream(fileLocation+"item.csv"));
System.out.println("WritingItemfileto:
"+fileLocation+"item.csv");
}
Itemitem=newItem();
for(inti=1;i<=itemKount;i++){
item.i_id=i;
item.i_name=jTPCCUtil.randomStr(jT