JMeter BeanShell 属性 跨线程组设置

JMeter BeanShell Property Setting Across Thread Group

我正在尝试使用 BeanShell 后处理器计算 JMeter 中两个事件之间的时间。

在第一个块中,我获取时间并将其存储为 属性。这是在一个线程组中。 然后在另一个线程组中,我有第二个 BeanShell 块。我收到一个我无法弄清楚的错误。我把错误贴在这里了。非常感谢您的提示和建议!

下面是两段BeanShell代码:

第一个后处理器:

//Set the current time to the time_upload variable
long time_upload = prev.getTime(); // get POST Time

props.put("time_upload",(String.valueof(time_upload))); 
log.info("Time for Upload is: " + time_upload); // print difference to jmeter.log file

第二个后处理器:

String no_saved_carts = vars.get("no_saved_carts");
String no_saved_carts_trimmed = no_saved_carts.trim();

String temp_description = vars.get("description");
String temp_description_no_space = temp_description.trim();

String time_upload_local = props.get("time_upload");


if(temp_description_no_space.equals("</") || no_saved_carts_trimmed.equals("No Saved Carts Found")){
    vars.put("description","true");
} else{
    vars.put("description","false");
    //set the time to time_processing based on time_upload
    long time_processing_done = prev.getTime(); // get time 
    long time_upload_long = Long.parseLong(time_upload_local); // get HTTP Sampler 1 execution time from variable
    long delta = (time_processing_done - time_upload); // calculate difference
    log.info("Time difference is: " + delta + " ms"); // print difference to jmeter.log file
}

错误日志的相关部分:

2016/06/03 17:21:22 ERROR - jmeter.util.BeanShellInterpreter: Error invoking bsh method: eval   Sourced file: inline evaluation of: ``//Set the current time to the time_upload variable long time_upload = prev.getTi . . . '' : Error in method invocation: Static method valueof( long ) not found in class'java.lang.String' 
2016/06/03 17:21:22 WARN  - jmeter.extractor.BeanShellPostProcessor: Problem in BeanShell script org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval   Sourced file: inline evaluation of: ``//Set the current time to the time_upload variable long time_upload = prev.getTi . . . '' : Error in method invocation: Static method valueof( long ) not found in class'java.lang.String' 
2016/06/03 17:21:22 INFO  - jmeter.threads.JMeterThread: Thread is done: Upload Saved Cart Thread Group 1-1 
2016/06/03 17:21:22 INFO  - jmeter.threads.JMeterThread: Thread finished: Upload Saved Cart Thread Group 1-1 
2016/06/03 17:21:22 ERROR - jmeter.util.BeanShellInterpreter: Error invoking bsh method: eval   Sourced file: inline evaluation of: ``String temp_description = vars.get("description"); String no_saved_carts = vars. . . . '' : Typed variable declaration : Method Invocation Long.parseLong 
2016/06/03 17:21:22 WARN  - jmeter.extractor.BeanShellPostProcessor: Problem in BeanShell script org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval   Sourced file: inline evaluation of: ``String temp_description = vars.get("description"); String no_saved_carts = vars. . . . '' : Typed variable declaration : Method Invocation Long.parseLong 
2016/06/03 17:21:26 INFO  - jmeter.threads.JMeterThread: Thread is done: Check Upload Status 2-1 

不能使用String.valueOf()方法将long转String,有以下选项:

  1. 如果您仍然想要 String,只需更改将 long 转换为 String 的行,如下所示:

    props.put("time_upload", Objects.toString(time_upload,null)); 
    
  2. 去掉 long -> String 和反之转换,props 是通常的 java.util.Properties class instance so it stores Objects

    在第一个后处理器中:

    long time_upload = prev.getTime();
    props.put("time_upload", time_upload);
    

    在第二个后处理器中:

    long time_upload_long = props.get("time_upload"); // no need to cast from String
    
  3. 您可以使用 bsh.shared namespace 来保留任何对象 - 所有线程组都可以访问它

    在第一个后处理器中:

    bsh.shared.time_upload = prev.getTime();
    

    在第二个后处理器中:

    long time_upload = bsh.shared.time_upload
    

在 Beanshell 脚本错误的情况下,您可以在 jmeter.log 文件中获得更多信息性错误消息,方法是将您的代码用 try/catch 块包围,例如:

try {
    //your code here
}
catch (Throwable ex) {
    log.error("Something wrong", ex);
    throw ex;
}

有关 JMeter 和 Beanshell 的更多提示和技巧,请参阅 How to Use BeanShell: JMeter's Favorite Built-in Component