JMeter - 在调用每个 HTTP 请求采样器之前运行 python 脚本 [英] JMeter - Run a python script before calling each HTTP request sampler

查看:59
本文介绍了JMeter - 在调用每个 HTTP 请求采样器之前运行 python 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Jmeter 的新手.我的 HTTP 请求采样器调用看起来像这样

I am new to Jmeter. My HTTP request sampler call looks like this

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

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

Key value is generated by calling a python script which uses the image_id to generate a unique key.

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

Before each sampler I wanted to generate the key using python script which will be passed as a header to the next HTTP Request sampler.

我知道我必须使用某种预处理器来做到这一点.任何人都可以使用 jmeter 中的预处理器帮助我做到这一点.

I know I have to used some kind of preprocessor to do that. Can anyone help me do it using a preprocessor in jmeter.

推荐答案

我相信 BeanshellPreProcessor 正是您要找的.

I believe that Beanshell PreProcessor is what you're looking for.

示例 Beanshell 代码如下所示:

Example Beanshell code will look as follows:

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 变量中.

The code above will execute Python script and put it's response into ID variable.

您可以在 HTTP 请求中将其引用为/image/${ID}/list/

You will be able to refer it in your HTTP Request as /image/${ID}/list/

参见 如何使用 BeanShell:JMeter 最喜欢的内置组件 有关 Apache JMeter 中 Beanshell 脚本的更多信息的指南和一种 Beanshell 食谱.

See How to use BeanShell: JMeter's favorite built-in component guide for more information on Beanshell scripting in Apache JMeter and a kind of Beanshell cookbook.

您也可以将您的请求放在事务控制器下以排除预处理器执行时间从负载报告.

You can also put your request under Transaction Controller to exclude PreProcessor execution time from load report.

这篇关于JMeter - 在调用每个 HTTP 请求采样器之前运行 python 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆