模拟多用户进行压力测试
2011-06-21 17:24
471 查看
这周二接到mentor分配的任务,写压力测试,主要模拟大约1000个用户,调用unit test对系统进行压力测试。刚开始mentor的意思是给每一个unit test写一个test,以多线程方式调用它进行压力测试。一时头大,那得写很多个测试,还得一个方法一个方法的调用。后来懒了一下,稍作改变写了以下一个test,可以对所有的unit test 压力测试。思路如下: 主要采用了java反射和线程池两种技术。运行这个test,要求输入两个参数,一个是目标unit test的完整类名,一个是要模拟的用户数目。在该test中,建立线程池,根据输入的用户数目决定要运行的线程数(最终要求是运行1000个)。在每个线程中需要做的事是:利用完整类名,通过反射获取该类,以及其他属性,并获取、调用所有测试方法,并将测试结果保存在一个全局对象中。当所有线程运行完后,在主进程中打印所有测试结果。 期间碰到种种问题,有种种由于线程并发带来的问题,最终得到下面一串代码。
package com.ssc.sats.usertestall;import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.reflect.Method;
import java.lang.reflect.Field;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import junit.framework.TestCase;
import org.apache.log4j.Logger;
import com.ssc.faw.util.GenCache;
import com.ssc.faw.util.GenCacheObject;
import com.ssc.sats.cab.workflow.idf.IDF_7831011;/**
*
* @author a522381
*
* This class is used to test the project as if there are many users
*
*/
public class IDF_pressure_test extends TestCase
{
private transient static Logger log = Logger.getLogger(IDF_pressure_test.class);
private int waitTime = 30;
private int poolSize = 20;
// define a thread lock
public static final byte[] lock = new byte[0]; // invoke test() by using thread
public class MyThread extends Thread
{ private String testName;
private int index;
private String set;
private String down, down2; MyThread(String testName, int index)
{
this.testName = testName;
this.index = index;
set = "setUp";
down = "tearDown";
down2 = "tearDown2";
} @Override
public void run()
{ try
{ //get class by class name
Class cl = Class.forName(this.testName);
Object cls = cl.newInstance(); // set data to count
Field field = cl.getField("count");
field.set(cls, this.index); // set data to String[] firstData field = cl.getField("firstData");
String[] temp = (String[]) field.get(cls);
int length = temp.length;
for (int i = 0; i < length; i++)
temp[i] += ("_" + String.valueOf(this.index));
field.set(cls, temp); // get all methods
Method[] methods = cl.getDeclaredMethods(); // get setUp() and tearDown()
Method setUp = cl.getMethod(set, null);
Method tearDown = cl.getMethod(down, null);
Method tearDown2 = cl.getMethod(down2, null); //invoke all the test methods try
{
setUp.invoke(cls, null);
for (Method method : methods)
{
if (!(method.getName().equals(set)) && !(method.getName().equals(down))
&& !(method.getName().equals(down2)))
{
method.invoke(cls, null);
}
}
}
catch (Exception e)
{
log.fatal("Fata Error", e);
throw e; }
finally
{
tearDown2.invoke(cls, null);
} }
catch (Exception e)
{
e.printStackTrace();
}
}
} //this method is used to run thread for testting public void execute() throws Exception
{ int m = 0, i;
String testName = "";
String temp, key; // get inputs
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
try
{
System.out.println("TestName:");
testName = in.readLine();
System.out.println("User Number:");
m = Integer.parseInt(in.readLine());
System.out.println("TestName:" + testName + ", User Number:" + m);
}
catch (Exception e)
{
e.printStackTrace();
} //build a thread pool
ExecutorService pool = Executors.newFixedThreadPool(poolSize); // 1000 users run the test
for (i = 0; i < m; i++)
{
try
{
System.out.println("Thread " + i + " :");
pool.submit(new MyThread(testName, i)); }
catch (Exception e)
{
e.printStackTrace(); }
} //shut down the thread pool pool.shutdown();
while (!pool.isTerminated())
{Thread.sleep(500) ;}
pool.shutdownNow(); //print all the test results temp = testName.substring(testName.lastIndexOf(".") + 1);
for (i = 0; i < m; i++)
{
key = temp + String.valueOf(i);
try
{
System.out.println("User " + i + " test " + ((GenCacheObject) GenCache.get(key)).getCachedObject().equals(true));
}
catch (Exception e)
{
e.printStackTrace();
}
} GenCache.clear();
}}
package com.ssc.sats.usertestall;import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.reflect.Method;
import java.lang.reflect.Field;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import junit.framework.TestCase;
import org.apache.log4j.Logger;
import com.ssc.faw.util.GenCache;
import com.ssc.faw.util.GenCacheObject;
import com.ssc.sats.cab.workflow.idf.IDF_7831011;/**
*
* @author a522381
*
* This class is used to test the project as if there are many users
*
*/
public class IDF_pressure_test extends TestCase
{
private transient static Logger log = Logger.getLogger(IDF_pressure_test.class);
private int waitTime = 30;
private int poolSize = 20;
// define a thread lock
public static final byte[] lock = new byte[0]; // invoke test() by using thread
public class MyThread extends Thread
{ private String testName;
private int index;
private String set;
private String down, down2; MyThread(String testName, int index)
{
this.testName = testName;
this.index = index;
set = "setUp";
down = "tearDown";
down2 = "tearDown2";
} @Override
public void run()
{ try
{ //get class by class name
Class cl = Class.forName(this.testName);
Object cls = cl.newInstance(); // set data to count
Field field = cl.getField("count");
field.set(cls, this.index); // set data to String[] firstData field = cl.getField("firstData");
String[] temp = (String[]) field.get(cls);
int length = temp.length;
for (int i = 0; i < length; i++)
temp[i] += ("_" + String.valueOf(this.index));
field.set(cls, temp); // get all methods
Method[] methods = cl.getDeclaredMethods(); // get setUp() and tearDown()
Method setUp = cl.getMethod(set, null);
Method tearDown = cl.getMethod(down, null);
Method tearDown2 = cl.getMethod(down2, null); //invoke all the test methods try
{
setUp.invoke(cls, null);
for (Method method : methods)
{
if (!(method.getName().equals(set)) && !(method.getName().equals(down))
&& !(method.getName().equals(down2)))
{
method.invoke(cls, null);
}
}
}
catch (Exception e)
{
log.fatal("Fata Error", e);
throw e; }
finally
{
tearDown2.invoke(cls, null);
} }
catch (Exception e)
{
e.printStackTrace();
}
}
} //this method is used to run thread for testting public void execute() throws Exception
{ int m = 0, i;
String testName = "";
String temp, key; // get inputs
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
try
{
System.out.println("TestName:");
testName = in.readLine();
System.out.println("User Number:");
m = Integer.parseInt(in.readLine());
System.out.println("TestName:" + testName + ", User Number:" + m);
}
catch (Exception e)
{
e.printStackTrace();
} //build a thread pool
ExecutorService pool = Executors.newFixedThreadPool(poolSize); // 1000 users run the test
for (i = 0; i < m; i++)
{
try
{
System.out.println("Thread " + i + " :");
pool.submit(new MyThread(testName, i)); }
catch (Exception e)
{
e.printStackTrace(); }
} //shut down the thread pool pool.shutdown();
while (!pool.isTerminated())
{Thread.sleep(500) ;}
pool.shutdownNow(); //print all the test results temp = testName.substring(testName.lastIndexOf(".") + 1);
for (i = 0; i < m; i++)
{
key = temp + String.valueOf(i);
try
{
System.out.println("User " + i + " test " + ((GenCacheObject) GenCache.get(key)).getCachedObject().equals(true));
}
catch (Exception e)
{
e.printStackTrace();
}
} GenCache.clear();
}}
相关文章推荐
- 单机模拟高并发进行压力测试
- 用Tsung对XMPP进行压力测试,登陆用户只有十几个,无法再添加,何故?
- java模拟并发操作进行压力测试
- Apache如何进行模拟高并发压力测试?
- java模拟并发操作进行压力测试
- C#模拟进行压力测试
- java模拟并发操作进行压力测试
- (草稿)模拟真实的用户进行压力的并发原理
- 在linux下安装Siege进行压力模拟测试
- Java模拟并发操作进行压力测试代码
- PHP多进程模拟多客户端并发访问远程mysql数据库进行网络压力测试
- PHP多进程模拟多客户端并发访问远程mysql数据库进行网络压力测试
- C# winform 窗体接收命令行参数自动登录进行系统,模拟600个WCF客户端的并发压力测试
- Android---Monkey指令进行压力测试实例(模拟点击)
- Java模拟并发操作进行压力测试代码
- 用jmeter进行多用户并发压力测试 [转]
- 用jmeter进行多用户并发压力测试
- java模拟并发操作进行压力测试
- 使用python对网站进行测试(模拟用户行为)
- 用jmeter进行多用户并发压力测试