Jmeter多线程数运行BeanShell断言报错

使用JMeter 写了一个BeanShell断言判断返回的参数,用单个线程运行的时候 可以正常运行,但是用2个线程数运行 就会报错

这是BeanShell断言内容

import java.text.SimpleDateFormat;
import java.util.Date;
//获取定位数据的当前戳
String posTime = vars.get("posTime");
String Lastpostime = vars.get("Lastpostime");
//获取定位模式数据值
String posMode1 = vars.get("posMode");
//将定位模式值截取第一位
posMode2 = posMode1.substring(0, 1);
//将str转化为int
int posMode3 = Integer.parseInt(posMode2);
//判断定位模式值是否大于0
if(posMode3>0) { 
    Date d1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").parse("${Lastpostime}");
    Date d2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").parse("${posTime}");
    if(d1.before(d2)){
        //Failure为false代表断言成功,且结果树不显示该断言
        Failure =  false;
        FailureMessage = "当前位置数据的时间为:"+posTime+",上一次请求获取时间为:"+Lastpostime;
        log.info(FailureMessage);
        vars.put("Lastpostime", "${posTime}");
        log.info(Lastpostime);
    }else{
        Failure =  true;
        FailureMessage = "\n\t\t当前位置实时数据时间戳在绑定时间之前,该数据没有更新!\n\t\t当前位置数据的时间为:"+posTime+",上一次请求获取时间为:"+Lastpostime;
    }
}else{
    Failure = true;
    FailureMessage = "当前定位模式值为:"+posMode1+"定位数据异常!!";
    log.info(FailureMessage);
}


这是报错内容

img

SimpleDateFormat是线程不安全的,所以多线程下就有问题,换成使用DateTimeFormatter试试,DateTimeFormatter是线程安全的

这篇文章:【JMeter】BeanShell断言之数据处理 也许能够解决你的问题,你可以看下

代码整体上try-catch一下,看看错误日志或许就明白了

try {
    //script logic here
}catch (Throwable ex) {
    log.error("Failed to do this or that", ex);
}