【问题标题】:JMeter Create url dynamically based on a propertyJMeter根据属性动态创建url
【发布时间】:2019-07-27 10:35:15
【问题描述】:

我有一个设置线程组,它点击一个 url 并获得很多产品 id。

    /product/4564
    /product/4534
    /product/1234
    ....

我将它保存在这样的属性中:

    // Using jsr223

    import org.apache.http.HttpHeaders;
    import org.apache.http.client.config.RequestConfig;
    import org.apache.http.client.methods.HttpUriRequest;
    import org.apache.http.client.methods.RequestBuilder;
    import org.apache.http.impl.client.HttpClientBuilder;
    import org.apache.http.util.EntityUtils;
    import org.apache.http.entity.StringEntity;
    import com.google.gson.Gson;

    List<String> sendRequest(String url, String method,         Map<String,Object> body) {

    RequestConfig requestConfig = RequestConfig.custom()
            .setConnectTimeout(2000)
            .setSocketTimeout(3000)
            .build();

    StringEntity entity = new StringEntity(new Gson().toJson(body), "UTF-8");


    HttpUriRequest request = RequestBuilder.create(method)
            .setConfig(requestConfig)
            .setUri(url)
            .setHeader(HttpHeaders.CONTENT_TYPE, "application/json;charset=UTF-8")
            .setEntity(entity)
            .build();             

    HttpClientBuilder.create().build().withCloseable {httpClient ->

        httpClient.execute(request).withCloseable {response ->

            String res = response.getEntity() != null ? EntityUtils.toString(response.getEntity()) : "";                        
            return Arrays.asList("result", res);
        }
    }
}

    Map<String,Object> map = new LinkedHashMap<>();

    SampleResult.setIgnore();

    def test1 = sendRequest("localhost:8080/product/list","GET", map);

    ArrayList pathProduct = Arrays.toString(test1.get(1))
    props.put("myProperty", pathProduct)

然后我在另一个线程组中有一个吞吐量控制器,这就是使用属性而不是变量的原因。我读到,如果我使用一个变量,将不会在另一个线程上可用。

然后我有一个Http请求,我这样设置:

protocol: http
server: localhost
path: ${__groovy(props.get("myProperty"))}

它部分有效,因为我只得到 1 个 url 而不是 N,我得到的 url 是:

http://localhost/product/4564/product/4534/product/1234

我想得到:

http://localhost/product/4564
http://localhost/product/4534
http://localhost/product/1234
.....

有什么想法吗?提前致谢

【问题讨论】:

    标签: jmeter jsr223


    【解决方案1】:

    最后,我在脚本中创建了一个文件,并将其作为 CSV 数据集配置使用,并且它工作正常。谢谢

    【讨论】:

    • 使用 csv 数据配置是实现此类结果的更好选择。
    猜你喜欢
    • 1970-01-01
    • 2017-09-30
    • 1970-01-01
    • 2020-09-12
    • 1970-01-01
    • 1970-01-01
    • 2017-12-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多