JMeter调用python脚本

JMeter调用python脚本

前提

  • 具备python环境
  • 具备jdk环境

一、编写python脚本

python脚本如下:

import random

# 随机一个 1~100 的随机数
print(random.randint(1,100))

二、使用BeanShell取样器调用python脚本

String command = "python D:\\apache-jmeter-5.5\\bin\\test.py"; // 使用python程序运行脚本(注意:路径最好不要有空格、特殊字符、中文,可能会影响运行)
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(command);

pr.waitFor();

BufferedReader b = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line = "";
StringBuilder response = new StringBuilder();
while ((line = b.readLine()) != null) {
  response.append(line);
}

b.close();
vars.put("random_int",response.toString()); // 添加变量

成功运行
image

posted @ 2024-09-29 10:14  测试小罡  阅读(587)  评论(0)    收藏  举报