JMeter的,使用动态增加的值
线程我已经创建了一个JMeter的功能测试,其基本上:JMeter的,使用动态增加的值
- 创建用户;
- 用户登录;
- 删除用户。
现在,我需要能够线程这一点,并动态地生成一个默认的前缀和数字增加后缀(即TestUser_1,TestUser_2,...等),一个用户名。
我使用了计数器,并且事情工作正常,直到我真正冲出线程数/循环数。当我这样做时,我正在与柜台发生冲突。一些线程试图读取计数器,但计数器已经被另一个线程增加。这导致尝试删除刚刚创建的线程,然后尝试使用刚删除的线程登录。
项目设置是这样的:
Test Plan Thread group Counter User Defined Variables Samplers
我希望通过计数器来追加一个数字,在线程执行用户定义的变量来解决这个问题,但计数器不能访问用户定义的变量。
关于如何解决此问题的任何想法?
预先感谢您。
我用下面的方式成功地测试用户的任何金额:
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