先展示示例(基于apache-jmeter-5.6.3版本)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
def cmd_list = ["pwd", "ls", "ping 127.0.0.1 -c 1", "who"] def random = new Random() def cmd = cmd_list[random.nextInt(cmd_list.size())] log.info("cmd: ${cmd}") def process = cmd.execute() process.waitFor() def exitCode = process.exitValue() def output = process.text //def error = process.err.text log.info("exit: ${exitCode}, output: ${output}") println("exit: ${exitCode}, output: ${output}") SampleResult.setResponseCode(exitCode.toString()) // 设置响应码(如 0 表示成功) SampleResult.setResponseMessage("${output}") // 设置响应消息 SampleResult.setResponseData(output, "UTF-8") // 设置响应数据 SampleResult.setDataType("text") // 响应数据类型 SampleResult.setDataEncoding("UTF-8") SampleResult.setSampleLabel("Shell Script Executed") // 自定义标签 |
坑点: 1,网上有说要import org.apache.jmeter.samplers.SampleResult,千万不要做这个操作,否则SampleResult对象操作回报错误 2,获取execute错误输出会提示IO Stream已经关闭(待解)