天天看点

多线程测试工具groboutils的使用

  引入groboutils jar包,其实我主要使用multithreadedtestrunner类和testrunnable类。

  原有的junit框架不做改变,导入grobotestingjunit-1.2.1-core.jar包

  代码如下

public class faultservicetest extends testcase {

/**

* @param args

* @throws faultexception

* @throws expparamnotfoundexception

* @throws parseexception

*/

private ifaultservice faultservice;

private static final int num_thread = 100; // 测试线程总数

public faultservicetest() {

super();

iinitservice initservice = (iinitservice) custombeanfactory

.getbean("initservice");

initservice.initsitedatabase();

this.faultservice = (ifaultservice) custombeanfactory

.getbean("faultservice");

}

public faultservicetest(string name) {

super(name);

// 高并发测试

public void testgetequipeventalertlistbypage() throws throwable {

equipmentquerybean equipmentquerybean = new equipmentquerybean();

// 生成所有测试线程

long start = system.currenttimemillis();

for (int i = 0; i < test.length; i++) {

test[i] = new faultservicethread(faultservice, equipmentquerybean);

// 生成测试线程运行器

multithreadedtestrunner mttr = new multithreadedtestrunner(test);

// 运行测试线程

mttr.runtestrunnables();

long used = system.currenttimemillis() - start;

system.out.printf("%s 调用花费 %s milli-seconds.\n", num_thread, used);

public static test suite() {

testsuite test = new testsuite("healthservice接口类测试");

test.addtest(new faultservicetest("testgetequipeventalertlistbypage"));

return test;

/*

* 测试线程类定义

private static class faultservicethread extends testrunnable {

private equipmentquerybean equipmentquerybean;

public faultservicethread(ifaultservice faultservice,

equipmentquerybean equipmentquerybean) {

this.faultservice = faultservice;

this.equipmentquerybean = equipmentquerybean;

@override

public void runtest() throws throwable {

faultservice.getequipeventalertlistbypage(equipmentquerybean);

 运行代码,并发数开到100个后观察运行时间发现运行运行时间到了12秒了,看来问题出在dao。需要进行sql代码优化了

  导入的测试包有:

import net.sourceforge.groboutils.junit.v1.multithreadedtestrunner;

import net.sourceforge.groboutils.junit.v1.testrunnable;

import junit.framework.test;

import junit.framework.testcase;

import junit.framework.testsuite;

最新内容请见作者的github页:http://qaseven.github.io/