简体   繁体   English

在Jmeter中附加到Groovy中的文件

[英]Append to a file in Groovy in Jmeter

To log the total time of a test scenario I use Groovy scripts in JSR223 PostProcessors to capture the timestamp before and after the script. 为了记录测试场景的总时间,我在JSR223 PostProcessors中使用了Groovy脚本来捕获脚本前后的时间戳。

Script to capture the scenario start timestamp: 捕获场景开始时间戳的脚本:

def scenario_start = System.currentTimeMillis().toString();
vars.put("scenario_start", scenario_start);

When I want to append the results for one Customer I want to append the results to an output file using a second groovy script: 当我想为一个客户添加结果时,我想使用第二个Groovy脚本将结果添加到输出文件中:

def scenario_end = System.currentTimeMillis().toString();
def scenario_start = vars.get("scenario_start");
def cm = vars.get("CUSTOMER_NUMBER");

log.debug("scenario_start = " + scenario_start);
log.debug("scenario_end = " + scenario_end);
log.debug("CUSTOMER_NUMBER = " + cm);

File file = new File("logs/output/scenario_times_idtv.txt").newOutputStream().withWriter { out ->
    out.append(cm + ";" + scenario_start + ";" + scenario_end);
}

The actual results is the first line in my log: scenario_times_idtv.txt is overwritten each time. 实际结果是我日志中的第一行:每次都覆盖censage_times_idtv.txt。

I've used out.append , out.println , new File("...").withWriter {...} , new File("...").newOutputStream().withWriter {...} but I get ONE line of data each time in my log. 我用过out.appendout.printlnnew File("...").withWriter {...}new File("...").newOutputStream().withWriter {...}每次在日志中获得一行数据。

I can't find how to properly APPEND new results to the log file. 我找不到如何正确将新结果追加到日志文件的方法。

Thanks for your experienced input! 感谢您的丰富经验!

I'm not very Groovy expert, but recall answering something similar at JMeter soap response - data analysis 我不是Groovy专家,但记得在JMeter soap响应中回答类似的问题-数据分析

using Beanshell . 使用Beanshell

I guess that if you change your code line 我想如果您更改代码行

File file = new File("logs/output/scenario_times_idtv.txt").newOutputStream().withWriter { out ->
out.append(cm + ";" + scenario_start + ";" + scenario_end);

to something like: 像这样:

FileOutputStream file = new FileOutputStream("logs/output/scenario_times_idtv.txt", true);
file.append((cm + ";" + scenario_start + ";" + scenario_end).getBytes());
file.flush();
file.close();

your script will behave as expected. 您的脚本将按预期运行。 Mind the true parameter of FileOutputStream. 注意FileOutputStream的true参数。 It tells the stream to append existing file. 它告诉流附加现有文件。 Perhaps there is a Groovy analogue, but this bit should work as well. 也许有一个Groovy类似物,但这一点也应该起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM