跨线程组的JMeter BeanShell属性设置

时间:2016-06-03 21:31:48

标签: java jmeter beanshell

我尝试使用BeanShell PostProcessors计算JMeter中两个事件之间的时间。

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

以下是两个BeanShell代码:

FIRST POSTPROCESSOR:

//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
}

ERROR LOG的相关部分:

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 

1 个答案:

答案 0 :(得分:1)

您无法使用String.valueOf()方法将long转换为String,有以下选项:

  1. 如果你仍然想要String,只需将long转换为String,使其变为:

    props.put("time_upload", Objects.toString(time_upload,null)); 
    
  2. 摆脱漫长 - &gt;字符串,反之亦然转换,props是一个通常的java.util.Properties类实例,因此它存储Objects

    在第一个PostProcessor中:

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

    在第二个PostProcessor中:

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

    在第一个PostProcessor中:

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

    在第二个PostProcessor中:

    long time_upload = bsh.shared.time_upload
    
  4. 如果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