Jmeter -在Windows上执行.bash脚本(使用groovy),并输出结果

jhkqcmku  于 2022-11-23  发布在  Windows
关注(0)|答案(2)|浏览(202)

基于此查询:Jmeter - Execute bash script using OS Process Sampler via Windows os
我能够使用OS进程采样器在Windows上执行bash命令。
现在我需要使用JSR223采样器执行。

String playerToken = vars.get("playerToken");
String command = "C:/Windows/System32/bash.exe /c cd C:/app/docs/release/ && ./no_longer_duplicate.bash ${playerToken} 6565";
StringBuffer output = new StringBuffer();

    Process p;
    try {
        p = Runtime.getRuntime().exec(command);
        p.waitFor();
        BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));

        String line = "";           
        while ((line = reader.readLine())!= null) {
            output.append(line + "\n");
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    log.warn(output.toString());

但是我没有从执行中得到任何输出。任何帮助都很感激。

igetnqfo

igetnqfo1#

您要将此输出放在何处?
如果要将其作为响应数据-请在脚本末尾添加下一行:

SampleResult.setResponseData(output.toString(), 'UTF-8')

如果您希望在jmeter.log文件中看到它-请在脚本末尾添加下一行:

log.info(output.toString())

如果您想在STDOUT中看到它-请在脚本末尾添加下一行:

println(output.toString())

更多信息:Top 8 JMeter Java Classes You Should Be Using with Groovy

ddrv8njm

ddrv8njm2#

虽然之前我使用Runtime.getRuntime().exec发布了问题,但这对我从来没有起作用,
成功的方法是使用不同的方法和processBuilder的帮助

String playerToken = vars.get("playerToken");
ProcessBuilder processBuilder = new ProcessBuilder("C:/Program Files/Git/bin/bash.exe", "-c", "cd C:/app/docs/release/ && ./no_longer_duplicate.bash ${playerToken} 6565");

        try {

            Process process = processBuilder.start();
            StringBuilder output = new StringBuilder();
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                output.append(line + "\n");
            }

            int exitVal = process.waitFor();
            if (exitVal == 0) {
                System.out.println("Success!");
                System.out.println(output);
                //log.warn(output.toString());
                SampleResult.setResponseData(output.toString(), 'UTF-8')
            } else {
                //abnormal...
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

有了这段代码,我就可以使用“JSR223 Sampler”和groovy在Windows上执行.bash脚本,并输出结果。

相关问题