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

查看:91
本文介绍了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"

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

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

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

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.

推荐答案

我相信 Beanshell您正在寻找PreProcessor .

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天全站免登陆