如何在 Jmeter 的预处理期间执行 HTTP 请求?

How to perform HTTP-Request during pre-processing in Jmeter?

在实际执行测试之前,我想调用一些 HTTP API 并解析其中的响应,然后再将其处理到我的 SSH 命令采样器。在 Jmeter 中最好的方法是什么?就像 JDBC 请求有一个预处理器,那么为什么没有 HTTP 请求的预处理器呢?

只需将 HTTP 调用放在 ThreadGroup 中的 SSH 命令采样器之前。 Pre-processors主要用于在执行采样器之前对请求进行一些修改。

JSR223 PreProcessor you can use to make an arbitrary HTTP Request using underlying Apache HttpComponent 个图书馆。

  1. 将 JSR223 预处理器添加为 SSH 命令采样器的子项
  2. 将以下代码放入"Script"区域:

    import org.apache.http.client.methods.HttpGet
    import org.apache.http.impl.client.HttpClientBuilder
    import org.apache.http.util.EntityUtils
    
    def httpClient = HttpClientBuilder.create().build()
    def httpGet = new HttpGet("http://example.com")
    def httpResponse = httpClient.execute(httpGet)
    def responseData = EntityUtils.toString(httpResponse.getEntity())
    
    log.info('----- Response -----')
    log.info(responseData)
    vars.put('response', responseData)
    
  3. 上面的代码执行简单的 HTTP GET request to http://example.com 网站,打印对 jmeter.log 的响应并将其另外存储到 ${response} JMeter 变量,您可以稍后在需要的地方重复使用。

演示:

参考文献: