JMeter-在调用每个HTTP请求samp之前运行python脚本

2024-06-01 01:37:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我是新来的。我的HTTP请求采样器调用如下所示

Path= /image/**image_id**/list/
Header =  "Key" : "Key_Value"

键值是通过调用python脚本生成的,该脚本使用image_id来生成唯一的键。

在每个采样器之前,我想使用python脚本生成密钥,该脚本将作为头传递给下一个HTTP请求采样器。

我知道我必须用某种预处理器来做。有人能帮我用jmeter中的预处理器吗。


Tags: pathkeyimage脚本idhttpvalue密钥
3条回答

由尤金·卡扎科夫(Eugene Kazakov)发布的一个可能的解决方案:

JSR223 sampler has good possibility to write and execute some code, just put jython.jar into /lib directory, choose in "Language" pop-up menu jython and write your code in this sampler.

Sadly there is a bug in Jython, but there are some suggestion on the page.

More here.

我相信Beanshell PreProcessor是你要找的。

Beanshell代码示例如下:

import java.io.BufferedReader;
import java.io.InputStreamReader;

Runtime r = Runtime.getRuntime();
Process p = r.exec("/usr/bin/python /path/to/your/script.py");
p.waitFor();
BufferedReader b = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
StringBuilder response = new StringBuilder();
while ((line = b.readLine()) != null) {
    response.append(line);

}

b.close();
vars.put("ID",response.toString());

上面的代码将执行Python脚本并将其响应放入ID变量中。

您可以在HTTP请求中将其称为 /图像/${ID}/list/

有关Apache JMeter中Beanshell脚本的更多信息和一种Beanshell食谱,请参见How to use BeanShell: JMeter's favorite built-in component指南。

您还可以将请求放在Transaction Controller下,以从加载报告中排除预处理器执行时间。

您可以使用BSF预处理器。

首先下载Jython Library并保存到jmeter的lib目录。

在您的HTTP采样器上添加一个BSF预处理器,选择as language Jython并执行所需的魔术来获取id,作为一个示例,我使用了这个:

import random
randImageString = ""
for i in range(16):
    randImageString = randImageString + chr(random.randint(ord('A'),ord('Z')))

vars.put("randimage", randImageString)

注意vars.put("randimage",randImageString"),它将在稍后向jmeter插入可用的变量。

现在在您的测试中,您可以在需要时使用${randimage}

HTTP Sampler with parameters

现在,每个请求都会随着Python脚本中randmage的值而改变。

相关问题 更多 >