javascript 如何将 Jmeter 变量保存到 csv 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26179894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 05:36:56  来源:igfitidea点击:

How to save Jmeter Variables to csv file

javajavascriptjmeterjmeter-pluginsbeanshell

提问by Roxana

Does anyone knoe how to save specific Jmeter Variables into a csv file? I have already tried this topic with no succes: Write extracted data to a file using jmeterand this code:

有谁知道如何将特定的 Jmeter 变量保存到 csv 文件中?我已经尝试过这个主题,但没有成功:使用 jmeter和以下代码将提取的数据写入文件

FileWriter fstream = new FileWriter("result.csv",true);
BufferedWriter out = new BufferedWriter(fstream);
out.write(${account_id});
out.close();

Thank you.

谢谢你。

回答by Dmitri T

  1. Replace your out.write(${account_id});stanza with out.write(vars.get("account_id"));
  2. It is better to close fstreaminstance as well to avoid open handles lack
  3. If you're going to reuse this file, i.e. store > 1 variable, add a separator, i.e. new line
  1. 替换你的out.write(${account_id});out.write(vars.get("account_id"));
  2. 最好也关闭fstream实例以避免缺少打开的句柄
  3. 如果要重用此文件,即 store > 1 个变量,请添加分隔符,即换行

Final code:

最终代码:

FileWriter fstream = new FileWriter("result.csv",true);
BufferedWriter out = new BufferedWriter(fstream);
out.write(vars.get("account_id"));
out.write(System.getProperty("line.separator"));
out.close();
fstream.close();

See How to use BeanShell: JMeter's favorite built-in componentfor comprehensive information on Beanshell scripting

有关Beanshell 脚本的全面信息,请参阅如何使用 BeanShell:JMeter 最喜欢的内置组件

回答by Meet

You can use this code in your BeanShellPostProcessor. It may help You.

您可以在 BeanShellPostProcessor 中使用此代码。它可能会帮助你。

String acid="${account_id}";
FileWriter fstream = new FileWriter("result.csv",true);
fstream.write(acid+"\n");
fstream.close();