我在JMeter测试计划中定义了正则表达式,并且我能够捕获user.variables中的简单变量中的值。但是我试图在Beanshell后处理器中计算两个变量之间的时差,并且我的报告中没有得到任何结果。
import javax.xml.bind.DatatypeConverter;
vars.put("old_date_submitted", "submittedDate"); // submittedDate, succeeddedDate and runningDates are regular expr. reference names
vars.put("old_date_succeeded", "succeededDate");
vars.put("old_date_running", "runningDate");
Calendar cal_s = DatatypeConverter.parseDateTime(vars.get("old_date_submitted"));
Calendar cal_c = DatatypeConverter.parseDateTime(vars.get("old_date_succeeded"));
Calendar cal_r = DatatypeConverter.parseDateTime(vars.get("old_date_running"));
Date new_date1 = cal_s.getTime(); // submitted Time
Date new_date2 = cal_c.getTime(); // succeeded Time
Date new_date3 = cal_r.getTime(); // running Time
long new_date1_ms = new_date1.getTime(); // submitted Time
long new_date2_ms = new_date2.getTime();
long new_date3_ms = new_date3.getTime();
log.info("Date in milliseconds: " + new_date1_ms);
long delta1 = new_date2_ms - new_date1_ms; //calculate the difference (succeededDate - submittedDate)
long delta2 = new_date3_ms - new_date1_ms; //calculate the difference (runningDate - submittedDate)
vars.put("delta1", String.valueOf("delta1")); // store the result into a JMeter Variable
vars.put("delta2", String.valueOf("delta2")); // store the result into a JMeter Variable
答案 0 :(得分:0)
这一位:
vars.put("old_date_submitted", "submittedDate"); // submittedDate, succeeddedDate and runningDates are regular expr. reference names
vars.put("old_date_succeeded", "succeededDate");
vars.put("old_date_running", "runningDate");
对我来说似乎很奇怪。给出:
submittedDate,succeeddedDate和runningDates是常规expr。参考名称
我的期望是你应该使用JMeter变量而不是硬编码字符串,所以你应该改变你的代码:
vars.put("old_date_submitted", vars.get("submittedDate")); // submittedDate, succeeddedDate and runningDates are regular expr. reference names
vars.put("old_date_succeeded", vars.get("succeededDate"));
vars.put("old_date_running", vars.get("runningDate"));
所以很可能你的代码在DatatypeConverter.parseDateTime
失败了。
下次您遇到Beanshell脚本的任何问题时,请考虑以下故障排除技术:
debug();
指令添加到Beanshell脚本的最开头 - 它将触发调试输出到stdout 将您的Beanshell代码放在try/catch block中,如:
try {
//your code here
}
catch (Throwable ex) {
log.error("Something went wrong", ex);
throw ex;
}
通过这种方式,您可以将更多“人性化”的堆栈跟踪打印到 jmeter.log 文件中。
有关在JMeter测试中使用Beanshell的更多信息,请参阅How to Use BeanShell: JMeter's Favorite Built-in Component指南。