jmeter - 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
.....
任何想法?提前致谢
解决方案
最后,我在脚本中创建了一个文件,并将其作为 CSV 数据集配置使用,它可以正常工作。谢谢
推荐阅读
- javascript - x 值的数量被裁剪
- c# - 使用 C# 调用 swagger Web 服务
- jquery - 选中某个复选框时将显示输入文本。取消勾选后会隐藏
- xamarin.forms - Xamarin ContentPage.ToolbarItems 移动到页面底部
- nullpointerexception - 如何在 getOntClass() 上调试 NullPointerException?
- php - this.map.tick 不是函数
- spring-data - _class 字段和 spring 数据 couchbase
- python - 如何将另一列上的一列乘以该月第一天的值
- odoo-12 - 如何在odoo 12中将字段添加到product.template
- jquery - 通过 jquery 操作 WebVTT 提示