首页 > 解决方案 > JMeter根据属性动态创建url

问题描述

我有一个设置线程组,它点击一个 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
.....

任何想法?提前致谢

标签: jmeterjsr223

解决方案


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


推荐阅读