码迷,mamicode.com
首页 > 数据库 > 详细

nGrinder TestRunnerInsertMysqlMore2.groovy

时间:2017-11-11 20:47:54      阅读:294      评论:0      收藏:0      [点我收藏+]

标签:creat   cut   mysql   连接   project   执行sql   log   client   获取   

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 = "SuningRds@123";
            */
            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,root@123456
jdbc:mysql://10.244.73.65:3306/performance_test,root,root@123456
jdbc:mysql://10.244.73.65:3306/performance_test,root,root@123456

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

标签:creat   cut   mysql   连接   project   执行sql   log   client   获取   

原文地址:http://www.cnblogs.com/lindows/p/7819852.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!