nGrinder TestRunnerInsertMysqlMore2.groovy

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nGrinder TestRunnerInsertMysqlMore2.groovy相关的知识,希望对你有一定的参考价值。

s

C:\Users\Lindows\IdeaProjects\TestGroovy\src\com\iteye\lindows\TestRunnerInsert.groovy

package com.iteye.lindows;

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.Statement

import static net.grinder.script.Grinder.grinder
import static org.junit.Assert.assertTrue

/**
 * java代码示例,连接数据库进行查询
 *
 * @author Lindows
 */
@RunWith(GrinderRunner)
class TestRunnerInsert {
    public static GTest insertTable
    public static Connection conn;
    public static Statement stmt; //创建Statement对象
    public static List<String> dbLineList; // 准备参数化数据对象
    public static fileRownNumber;

    @BeforeProcess
    public static void beforeProcess() {
        insertTable = new GTest(1, "插入表数据")
        dbLineList = new File("C:/Users/Lindows/IdeaProjects/TestGroovy/src/resources/database.txt").readLines("UTF-8")
        System.out.println("tangxje:  " + dbLineList.size())
        fileRownNumber = new Random().nextInt(dbLineList.size()-1)+1 //文件database.txt,含titile行0,共10行。
        // 方法二,直接获取(文件行数如:10行-1)=9填入下面,nextInt从0~8取值
        //fileRownNumber = new Random().nextInt(9)
        grinder.logger.info("tangxje ::"  + fileRownNumber)
        try {
            //调用Class.forName()方法加载驱动程序
            Class.forName("com.mysql.jdbc.Driver");
            grinder.logger.info("成功加载MySQL驱动!");
            /*
             String url="jdbc:mysql://10.244.73.65:3306/performance_test";    //JDBC的URL
             String username = "fabu";
             String passwd = "[email protected]";
            */
            String url = dbLineList.get(fileRownNumber).split(",")[0];
            String username = dbLineList.get(fileRownNumber).split(",")[1];
            String passwd = dbLineList.get(fileRownNumber).split(",")[2];
            grinder.logger.debug(url)

            conn = DriverManager.getConnection(url, username, passwd);
            stmt = conn.createStatement(); //创建Statement对象
            grinder.logger.info("成功创建stmt!");
        } catch (Exception e) {
            e.printStackTrace()
        }
    }

    @BeforeThread
    public void beforeThread() {
        insertTable.record(this, "insertTable")
        grinder.statistics.delayReports=true
    }

    @Test
    public void insertTable() {
        try{
            grinder.logger.info("成功连接到数据库!");
            StringBuffer sql = new StringBuffer()
            sql.append("insert into tab_001(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 SuNing for 3 years‘,‘2017-06-12 18:00:00‘,‘2017-06-13 15:00:00‘)")
            grinder.logger.info(sql.toString())
            Thread.sleep(new Random().nextInt(10)) //这里可以设置思考时间10ms
            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\IdeaProjects\TestGroovy\src\resources\database.txt

url,username,password
jdbc:mysql://10.244.73.65:3306/performance_test,root,[email protected]
jdbc:mysql://10.244.73.65:3306/performance_test,root,[email protected]
jdbc:mysql://10.244.73.65:3306/performance_test,root,[email protected]

C:\Users\Lindows\Desktop\dev\ptsp_lib\

 1 antlr-2.7.7.jar
 2 asm-3.3.1.jar
 3 commons-collections-3.2.1.jar
 4 commons-compress-1.4.1.jar
 5 commons-io-2.0.1.jar
 6 commons-lang-2.6.jar
 7 commons-math3-3.6.1.jar
 8 dnsjava-2.1.1.jar
 9 dom4j-1.6.1.jar
10 grinder-core-3.9.1.jar
11 grinder-dcr-agent-3.9.1.jar
12 grinder-http-3.9.1.jar
13 grinder-http-patch-3.9.1-patch.jar
14 grinder-httpclient-3.9.1.jar
15 grinder-httpclient-patch-3.9.1-patch.jar
16 grinder-patch-3.9.1-patch.jar
17 groovy-all-2.2.1.jar
18 gson-2.2.jar
19 hamcrest-all-1.1.jar
20 hibernate-commons-annotations-4.0.2.Final.jar
21 hibernate-core-4.2.2.Final.jar
22 java-semver-0.9.0.jar
23 javassist-3.15.0-GA.jar
24 jboss-transaction-api_1.1_spec-1.0.1.Final.jar
25 jcl-over-slf4j-1.6.4.jar
26 jcommander-1.32.jar
27 jna-3.4.0.jar
28 json-20090211.jar
29 junit-dep-4.11.jar
30 jython-standalone-2.5.3.jar
31 log4j-1.2.15.jar
32 logback-classic-1.0.0.jar
33 logback-core-1.0.0.jar
34 ls.txt
35 mysql-connector-java-5.1.36.jar
36 ngrinder-core-3.4.jar
37 ngrinder-groovy-3.4.jar
38 ngrinder-runtime-3.4.jar
39 pf4j-0.12.0.jar
40 picocontainer-2.13.6.jar
41 sigar-1.6.4.jar
42 sigar-native-1.0.jar
43 slf4j-api-1.6.4.jar
44 xz-1.0.jar

 

 

 

end

以上是关于nGrinder TestRunnerInsertMysqlMore2.groovy的主要内容,如果未能解决你的问题,请参考以下文章

性能测试四十九:ngrinder压测平台

性能测试四十九:ngrinder压测平台

nGrinder性能测试框架安装步骤

nGrinder性能测试工具入门

ngrinder 压力测试脚本groovy 实战

性能工具之 nGrinder 源码安装