我用下面的方式成功地测试用户的任何金额:
1.生成使用BeanShell的脚本(在BeanShell Sampler EG)的CSV文件,测试用户的详细信息,例如:
testUserName001,testPwd001
testUserName002,testPwd002
. . .
testUserName00N,testPwd00N
包含测试运行所需的条目数量。
这是每个“N用户测试运行”,在单独的线程组中,在setUp Thread Group或甚至可能在单独的jmx脚本中完成一次......没有区别。
您可以在下面找到工作beanshell代码。
2.创建使用以前创建的用户列表测试应用测试用户。
如果你不需要在应用程序中创建,你可以跳过这个。
Thread Group
Number of Threads = 1
Loop Count = 1
. . .
While Controller
Condition = ${__javaScript("${newUserName}"!="",)} // this will repeat until EOF
CSV Data Set Config
Filename = ${__property(user.dir)}${__BeanShell(File.separator,)}${__P(users-list,)} // path to generated users-list
Variable Names = newUserName,newUserPwd // these are test-users details read from file into pointed variables
Delimiter = '
Recycle on EOF? = False
Stop thread on EOF? = True
Sharing Mode = Current thread group
[CREATE TEST USERS LOGIC HERE]// here are actions to create separate user in application
. . .
3.执行多用户的逻辑。 像上面给出的模式,但线程组不是执行1,而是执行N个线程。
Thread Group
Number of Threads = ${__P(usersCount,)} // set number of users you need to test
Ramp-Up Period = ${__P(rampUpPeriod,)}
Loop Count = X
. . .
While Controller
Condition = ${__javaScript("${newUserName}"!="",)} // this will repeat until EOF
CSV Data Set Config
Filename = ${__property(user.dir)}${__BeanShell(File.separator,)}${__P(users-list,)} // path to generated users-list
Variable Names = newUserName,newUserPwd // these are test-users details read from file into pointed variables
Delimiter = '
Recycle on EOF? = False
Stop thread on EOF? = True
Sharing Mode = Current thread group
[TEST LOGIC HERE]// here are test actions
. . .
的核心思想是使用线程组+虽然控制器+ CSV数据集配置组合:
3.1。 CSV数据集配置从生成的文件中读取每个测试用户的详细信息:
。 。 。一个。只有一次 - 因为“在EOF上停止线程?=真”;
。 。 。湾由于“共享模式=当前线程组”,不会阻止文件进一步访问(在另一个线程组中,例如,如果有的话)。
。 。 。 C。指向变量 - “变量名称= newUserName,newUserPwd” - 您将用于进一步的测试操作;
3.2。控制器强制CSV数据集配置读取生成文件中的所有条目 - 由于定义的条件(直到EOF)。
3.3。线程组将启动所有的线程与定义斜坡上升 - 或者,如果同时斜升= 0
你可以在这里采取的模式描述模板脚本:multiuser.jmx。
的BeanShell脚本生成测试用户的详细信息看起来像下面,并采取以下ARGS:
- 测试用户数
- (你的情况“TestUser_”)测试用户名模板
- 测试的用户名的格式(例如0 - 让TestUser_1,00 - 让TestUser_01,000-对于TestUser_001,等等......你可以简单的硬编码这个orexclude在所有)
- 生成的文件名。
import java.text.*;
import java.io.*;
import java.util.*;
String [] params = Parameters.split(",");
int count = Integer.valueOf(params[0]);
String testName = params[1];
String nameFormat = params[2];
String usersList = params[3];
StringBuilder contents = new StringBuilder();
try {
DecimalFormat formatter = new DecimalFormat(nameFormat);
FileOutputStream fos = new FileOutputStream(System.getProperty("user.dir") + File.separator + usersList);
for (int i = 1; i <= count; i++) {
String s = formatter.format(i);
String testUser = testName + s;
contents.append(testUser).append(",").append(testUser);
if (i < count) {
contents.append("\n");
}
}
byte [] buffer = contents.toString().getBytes();
fos.write(buffer);
fos.close();
}
catch (Exception ex) {
IsSuccess = false;
log.error(ex.getMessage());
System.err.println(ex.getMessage());
}
catch (Throwable thex) {
System.err.println(thex.getMessage());
}
总之它看起来像:
对不起,如果答案是太超载。
希望这会有所帮助。
[得到了答案?](http://stackoverflow.com/faq#howtoask) – 2012-09-08 10:52:14