在使用jmeter进行性能压测过程中,经常碰到tps高、压测时间长而导致生成jtl文件过大的问题,虽然可不加“-l”参数让jmeter不生成jtl文件,但这样做弊端也很明显那就是如果出现失败的请求没有日志可以找到失败的原因进而排查问题,所以下面我们就通过修改jmeter源码实现加上“-F”参数后,让jmeter产生jtl里只记录失败的请求的效果。
首先是JMeter.java文件,依葫芦画瓢在这里加上‘-F’的定义:
private static final int SYSTEM_PROPFILE = 'S';// $NON-NLS-1$ private static final int REMOTE_STOP = 'X';// $NON-NLS-1$ private static final int JTLFAILONLY_OPT = 'F';
还有下面这些地方也相应的增加代码:
private static final CLOptionDescriptor D_REPORT_OUTPUT_FOLDER_OPT = new CLOptionDescriptor("reportoutputfolder", CLOptionDescriptor.ARGUMENT_REQUIRED, REPORT_OUTPUT_FOLDER_OPT, "output folder for report dashboard"); private static final CLOptionDescriptor D_JTLFAILONLY_OPT = new CLOptionDescriptor("jtlfailonly", CLOptionDescriptor.ARGUMENT_DISALLOWED, JTLFAILONLY_OPT, "only record the failed request log");
private static final CLOptionDescriptor[] options = new CLOptionDescriptor[] { D_OPTIONS_OPT, D_HELP_OPT, D_VERSION_OPT, D_PROPFILE_OPT, D_PROPFILE2_OPT, D_TESTFILE_OPT, D_LOGFILE_OPT, D_JTLFAILONLY_OPT,
这个地方要获取我们的参数是否为空并将参数传给startNonGui方法。
CLOption jtlfailonly = parser.getArgumentById(JTLFAILONLY_OPT); startNonGui(testFile, jtlFile, rem, reportAtEndOpt != null, jtlfailonly!=null); startOptionalServers();
在startNonGui方法中再将参数传递给runNonGui方法
driver.runNonGui(testFile, logFile, remoteStart != null, remoteHostsString, generateReportDashboard, jtlfailonly);
在runNonGui方法中修改如下,依据jtlfailonly参数是否存在调用ResultCollector的多态方法
if (logFile != null) { ResultCollector logger=null; if(jtlfailonly){ logger = new ResultCollector(summer,jtlfailonly); }else { logger = new ResultCollector(summer); }
ResultCollector方法在ResultCollector类中,修改如下: