nGrinder TestRunnerInsertMysqlMore.groovy

时间:2023-03-08 16:11:44

s

C:\Users\Lindows\workspace\GroovyTest\src\com\iteye\lindows\mysql\TestRunnerInsertMysqlMore.groovy

 package com.iteye.lindows.mysql

 import junit.framework.Assert
import net.grinder.script.GTest
import net.grinder.scriptengine.groovy.junit.GrinderRunner
import net.grinder.scriptengine.groovy.junit.annotation.AfterThread
import net.grinder.scriptengine.groovy.junit.annotation.BeforeProcess
import net.grinder.scriptengine.groovy.junit.annotation.BeforeThread
import org.junit.Test
import org.junit.runner.RunWith import java.sql.Connection
import java.sql.DriverManager
import java.sql.ResultSet
import java.sql.Statement import static net.grinder.script.Grinder.grinder
import static org.junit.Assert.assertTrue /**
* java代码示例,连接数据库进行插入
*
* @author 15071743(zhangl)
*/
@RunWith(GrinderRunner)
class TestRunnerInsertMysqlMore {
public static GTest insertTable
public static Connection conn;
public static Statement stmt; //创建Statement对象
public static List<String> databaseIP; //参数化文件多数据库IP
public static fileRowNumber;
@BeforeProcess
 � //LoggerFactory.getLogger("worker").setLevel(Level.ERROR); //可以关闭该类打印的日志
public static void beforeProcess() {
insertTable = new GTest(1, "插入表数据")
//参数化文件读取--随机数据库IP
databaseIP = new File("C:\\Users\\Lindows\\workspace\\GroovyTest\\src\\resources\\databaseIP.txt").readLines()
fileRowNumber = new Random().nextInt(databaseIP.size()-1)
} @BeforeThread
public void beforeThread() {
insertTable.record(this, "insertTable")
grinder.statistics.delayReports=true
} @Test
public void insertTable() {
try{
//调用Class.forName()方法加载驱动程序
Class.forName("com.mysql.jdbc.Driver");
grinder.logger.info("成功加载MySQL驱动!");
StringBuffer url = new StringBuffer()
url.append("jdbc:mysql://")
.append(databaseIP.get(fileRowNumber).split(",")[0]) // 随机获取第一列数据库ip地址
.append(":3306/performance_test")
grinder.logger.info("tangxje_url: " + url.toString());
String username = "performance_user";
String passwd = "performance!QAZ";
conn = DriverManager.getConnection(url.toString(), username, passwd);
stmt = conn.createStatement();
grinder.logger.info("成功创建stmt!");
grinder.logger.info("成功连接到数据库!");
StringBuffer sql = new StringBuffer()
grinder.logger.info("tangxje_tab:" + databaseIP.get(fileRowNumber).split(",")[1]);
sql.append("insert into ")
.append(databaseIP.get(fileRowNumber).split(",")[1]) // 随机获取第二列数据库表名,同行取值列1
.append(" (column_int,column_double,column_decimal,column_varchar_name,column_varchar_address,column_text,column_timestamp_create_time,column_timestamp_update_time) values (1000,300.25,600.98,'jack','")
.append("China BeiJing")
.append(new Random().nextInt(99999999))
.append("', 'work in lindows for 3 years','2017-06-12 18:00:00','2017-06-13 15:00:00')")
grinder.logger.info("tangxje_sql:" + sql.toString())
assertTrue(!stmt.execute(sql.toString()))//执行sql insert,!stmt.execute(sql)该写法只于insert true确认
//assertTrue(stmt.execute(sql));//执行sql query , !stmt.execute(sql)该写法只适用于query true确认
}catch(Exception e) {
e.printStackTrace();
}
} @AfterThread
public void afterThread() {
stmt.close();
conn.close();
}
}

文件:C:\Users\Lindows\workspace\GroovyTest\src\resources\databaseIP.txt

内容:

10.37.153.1,tab_102
10.37.136.165,tab_002
10.37.136.162,tab_002
10.37.136.163,tab_102

C:\Users\Lindows\Desktop\lab\groovy\libs

nGrinder TestRunnerInsertMysqlMore.groovy
 1 asm-3.3.1.jar
2 commons-lang-2.6.jar
3 commons-lang3-3.3.2.jar
4 commons-logging-1.0.4.jar
5 grinder-core-3.9.1.jar
6 grinder-dcr-agent-3.9.1.jar
7 grinder-http-3.9.1.jar
8 grinder-http-patch-3.9.1-patch.jar
9 grinder-httpclient-3.9.1.jar
10 grinder-httpclient-patch-3.9.1-patch.jar
11 grinder-patch-3.9.1-patch.jar
12 hamcrest-all-1.1.jar
13 json-20090211.jar
14 junit-dep-4.11.jar
15 log4j-1.2.15.jar
16 logback-classic-1.0.0.jar
17 logback-core-1.0.0.jar
18 mysql-connector-java-5.1.36 (1).jar
19 ngrinder-core-3.4.jar
20 ngrinder-groovy-3.4.jar
21 ngrinder-runtime-3.4.jar
22 ngrinder-sh-3.4.jar
23 slf4j-api-1.6.4.jar
nGrinder TestRunnerInsertMysqlMore.groovy

end