本文介绍了JMeter,使用动态增量值的线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个JMeter功能测试,该测试实质上是:

I have created a JMeter functional test that essentially:

  1. 创建用户;
  2. 使用用户登录;
  3. 删除用户.

现在,我需要能够对此进行线程化,并动态生成具有默认前缀和数字后缀(即TestUser_1,TestUser_2等)的用户名.

Now, I need to be able to thread this, and dynamically generate a username with a default prefix and a numerically incremented suffix (ie TestUser_1, TestUser_2, ... etc).

我使用了计数器,直到我真正增加了线程/循环的数量之前,一切运转良好.当我这样做时,我与柜台发生冲突.一些线程试图读取该计数器,但是该计数器已经被另一个线程递增.这导致尝试删除刚刚创建的线程,然后尝试使用刚刚删除的线程登录.

I used the counter, and things were working fine until I really punched up the number of threads/loops. When I did this, I was getting a conflict with the counter. Some threads were trying to read the counter, but the counter had already been incremented by another thread. This resulted in trying to delete a thread that was just created, then trying to log in with a thread that was just deleted.

该项目的设置如下:


Test Plan
    Thread group
        Counter
        User Defined Variables
        Samplers

我希望通过使用计数器在线程执行时将数字附加到用户定义的变量上来解决此问题,但是无法在用户定义的变量中访问该计数器.

I was hoping to solve this problem by using the counter to append a number to the user defined variables upon thread execution, but the counter cannot be accessed in the user defined variables.

关于如何解决此问题的任何想法?

Any ideas on how I can solve this problem?

谢谢.

推荐答案

我已成功将以下方案用于任何数量的测试用户:

I've used the following scheme successfully with any amount of test users:

1.使用beanshell脚本生成(在 BeanShell中采样器(例如)带有测试用户详细信息的csv文件,例如:

1. Generate using beanshell-script (in BeanShell Sampler e.g.) csv-file with test-user details, for example:

testUserName001,testPwd001
testUserName002,testPwd002
. . .
testUserName00N,testPwd00N

包含测试运行所需的条目数量.
此操作在 setUp中的单独线程组中,每个"N个用户测试运行"都完成一次.线程组,甚至在单独的jmx脚本中……都没有区别.

with the amount of entries you need for the test-run.
This is done once per "N users test-run", in separate Thread Group, in setUp Thread Group or maybe even in separate jmx-script... makes no difference.

您可以在下面找到有效的beanshell代码.

You can please find working beanshell-code below.


2.使用先前创建的用户列表在测试应用程序"中创建测试用户.
如果您不需要在应用程序中创建,则可以跳过此操作.

2. Create your test users IN TEST APPLICATION using previously created users-list.
If you don't need create in application you may skip this.


    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个线程执行的.

3. Perform multi-user logic.Schema like the given above one but Thread Group executed not for 1 but for N threads.


    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
                . . .


关键思想是使用线程组+ While控制器+ CSV数据集配置组合:

The key idea is in using Thread Group + While Controller + CSV Data Set Config combination:

3.1. CSV数据集配置从生成的文件中读取每个测试用户的详细信息:
. . .一种.仅一次-由于在EOF上停止线程?= True";
. . . b.不会阻止文件进行进一步访问(例如,在其他线程组中,如果有的话)-因为共享模式=当前线程组";
. . . C.指向变量-变量名称= newUserName,newUserPwd"-您将在进一步的测试操作中使用该变量;
3.2.当Controller强制CSV数据集配置从定义的条件(直到EOF为止)读取生成的文件中的所有条目时.
3.3.线程组将以定义的加速启动所有线程-如果加速= 0,则同时启动.

3.1. CSV Data Set Config reads details for each the test users from generated file:
. . . a. only once - because of "Stop thread on EOF? = True";
. . . b. doesn't block file for further access (in another thread groups, e.g., if there are any) - because of "Sharing Mode = Current thread group";
. . . c. pointed variables - "Variable Names = newUserName,newUserPwd" - you will use in further test-actions;
3.2. While Controller forces CSV Data Set Config to read all the entries from generated file - because of defined condition (until the EOF).
3.3. Thread Group will start all the threads with defined ramp-up - or simultaneously if ramp-up = 0.


您可以在此处获取所描述架构的模板脚本: multiuser.jmx .

You can take here template script for described schema: multiuser.jmx.


用于生成测试用户详细信息的Beanshell脚本如下所示,并带有以下args:
-测试用户数
-测试用户名模板(在您的情况下为"TestUser_")
-测试用户名格式(例如0-获得TestUser_1、00-获得TestUser_01、000-获得TestUser_001,等等...您只需对此进行硬编码或完全排除)
-生成的文件的名称.

Beanshell script to generate test-users details looks like below and takes the following args:
- test-users count
- test-user name template ("TestUser_" in your case)
- test-user name format (e.g. 0 - to get TestUser_1, 00 - to get TestUser_01, 000- for TestUser_001, etc... you can simply hardcode this orexclude at all)
- name of generated file.

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());
}


总的来说,会像这样:

对不起,如果答案太重.
希望这会有所帮助.

Sorry if answer is too overloaded.
Hope this helps.

这篇关于JMeter,使用动态增量值的线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 21:08