在我的JMeter测试计划中,我试图将所有错误写到日志中。我正在使用配置如下的BeanShell后处理器
import org.apache.jmeter.services.FileServer;
if (ResponseCode != null && ResponseCode.equals("200") == false) {
Failure = true;
// displays in Results Tree
FailureMessage ="Creation of a new CAE record failed. Response code " + ResponseCode + "." ;
// Static elements
part1 = "Creation of a new record failed. Response code: ";
part2 = ". Sorry!";
// Open File(s)
FileOutputStream f = new FileOutputStream("d:\error.csv", true);
PrintStream p = new PrintStream(f);
// Write data to file
p.println( part1 + ResponseCode + part2 );
// Close File(s)
p.close();
f.close();
}
我正在尝试做一个简单的测试,其中 HTTP 请求正在执行一个 POST,该 POST 从目录不再存在的 c:jmeter/tests/payloads 传入一个 json 文件。(假设有人不小心删除了它...
问题是测试正在停止(见下文),并且永远不会到达 BeanShell 将错误写出到日志文件中。我需要捕获所有错误响应,并且仅捕获错误响应。
我不确定如何处理这个问题。我读过Jmeter。BeanShell PostProcessor 等,但他们没有解决当它没有到达 BeanShell 时会发生什么的问题。
任何帮助不胜感激!
org.apache.jorphan.util.JMeterStopThreadException: End of sequence
at org.apache.jmeter.functions.FileToString.execute(FileToString.java:105)
at org.apache.jmeter.engine.util.CompoundVariable.execute(CompoundVariable.java:142)
at org.apache.jmeter.engine.util.CompoundVariable.execute(CompoundVariable.java:118)
at org.apache.jmeter.testelement.property.FunctionProperty.getStringValue(FunctionProperty.java:101)
at org.apache.jmeter.testelement.AbstractTestElement.getPropertyAsString(AbstractTestElement.java:274)
at org.apache.jmeter.config.Argument.getValue(Argument.java:146)
at org.apache.jmeter.protocol.http.util.HTTPArgument.getEncodedValue(HTTPArgument.java:236)
at org.apache.jmeter.protocol.http.sampler.HTTPHC4Impl.sendPostData(HTTPHC4Impl.java:1111)
at org.apache.jmeter.protocol.http.sampler.HTTPHC4Impl.handleMethod(HTTPHC4Impl.java:453)
at org.apache.jmeter.protocol.http.sampler.HTTPHC4Impl.sample(HTTPHC4Impl.java:329)
at org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy.sample(HTTPSamplerProxy.java:74)
at org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase.sample(HTTPSamplerBase.java:1146)
at org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase.sample(HTTPSamplerBase.java:1135)
at org.apache.jmeter.threads.JMeterThread.process_sampler(JMeterThread.java:434)
at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:261)
at java.lang.Thread.run(Unknown Source)
Caused by: java.io.FileNotFoundException: File 'C:JMetertestpayloadsRequest_1.json' does not exist
at org.apache.commons.io.FileUtils.openInputStream(FileUtils.java:299)
at org.apache.commons.io.FileUtils.readFileToString(FileUtils.java:1711)
at org.apache.commons.io.FileUtils.readFileToString(FileUtils.java:1734)
at org.apache.jmeter.functions.FileToString.execute(FileToString.java:102)
溶液
根据 Dmitri 的反馈,我已经从 Beanshell PostProcessor 切换到 Beanshell Assertion。经过一些调整,我让它工作,现在它只将错误(响应 != 200)写入错误.csv文件。它不是附加上一次运行中的文件,而是在每次运行时覆盖,以便仅捕获上次运行的错误。
如果有人认为我的解决方案可以改进,我很乐意收到反馈。再次感谢基里尔和德米特里。
import org.apache.jmeter.services.FileServer;
if (ResponseCode != null && ResponseCode.equals("200") == true) {
SampleResult.setResponseOK();
}
else if (!ResponseCode.equals ("200") == true ) {
Failure = true;
FailureMessage ="Creation of a new record failed. Response code " + ResponseCode + "." ; // displays in Results Tree
print ("Creation of a new record failed: Response code " + ResponseCode + "."); // goes to stdout
log.warn("Creation of a new record failed: Response code " + ResponseCode); // this goes to the JMeter log file
// Static elements or calculations
part1 = "Unable to generate a new record via POST. The response code is: "";
part2 = "". nn For response code = 'Non-HTTP ressponse', verify the payload file still exists. n For response code = 409, check the recordTypeId and recordGrpId combination for validity. n For response code = 500, verify the database and its host server are reachable. ";
// Open File(s)
FileOutputStream f = new FileOutputStream(FileServer.getFileServer().getBaseDir() + "\error.csv");
PrintStream p = new PrintStream(f);
// Write data to file
p.println( part1 + ResponseCode + part2 );
// Close File(s)
p.close();
f.close();
}
- 没有
ResponseCode
,Failure
和FailureMessage
在Beanshell PostProcessor中,切换到Beanshell Assertion。 ResponseCode.equals("200")
子句假定响应成功,错误响应通常具有响应代码> 400
请参阅如何使用 BeanShell:JMeter 最喜欢的内置组件指南,了解有关 JMeter 中 Beanshell 脚本的全面信息。
Jmeter 会覆盖您的error.csv
文件,而不是附加到该文件,因为您在每次断言调用时都会重新打开它。尝试在此之前打开它,例如在设置线程组中的单独 Beanshell 采样器中:
file = new FileOutputStream("error.csv", true);
bsh.shared.custom_log = new PrintStream(file)
然后在你的 beanshell 断言中使用它,如下所示:
if (ResponseCode.equals("200")==false) {
bsh.shared.custom_log.println( part1 + ResponseCode + part2 );
}
顺便说一句,AFAIK,你根本不需要这部分,因为默认情况下代码为 200 的 http 响应是可以的:
if (ResponseCode != null && ResponseCode.equals("200") == true) {
SampleResult.setResponseOK();
}
我没有测试代码,所以可能有错别字,但非常相似的代码对我有用。
Beanshell 共享值是在锁定的情况下访问的,因此如果您大量写入它,请注意可能的性能问题。使用这样的脚本和相当短的字符串(50-100 个字符),我每秒可以写入 ~1k,而不会对 jmeter 性能产生重大影响。