$ {__ time}函数输出不正确

时间:2018-03-19 14:34:06

标签: jmeter jmeter-3.2

我正试图在BeanShell后处理器中使用jmeter的时间函数获取今天的日期,格式为“$ {__ time(yyyy-MM-dd)}”。但在执行Beanshell后处理器后,结果显示为“1996”。基本上,“时间”功能通过减去像“2018-03-19”= 1996这样的值来显示结果。 请帮我解决这个问题,以便获得当前的日期和时间。 Beanshell代码如下

 import org.apache.jmeter.gui.GuiPackage;
import org.apache.commons.io.FilenameUtils;

String testPlanFile = GuiPackage.getInstance().getTestPlanFile();
String testPlanFileDir = FilenameUtils.getFullPathNoEndSeparator(testPlanFile);
vars.put("testPlanFileDir", testPlanFileDir);
//create the file in test plan file dir and print current time and Date

FileOutputStream f = new FileOutputStream(testPlanFileDir+"/CurrentDate.txt", true);
PrintStream p = new PrintStream(f); 
this.interpreter.setOut(p); 
//current date and time;
print("Current date is:"+${__time(yyyy-MM-dd)});
f.close();

4 个答案:

答案 0 :(得分:2)

将时间函数调用设置为PreProcessor的参数(通过迁移到JSR223 + Groovy 的方式进行演出)

然后使用变量参数

Using Parameters in Groovy

答案 1 :(得分:0)

您应该根据JMeter Best Practices移动到JSR223后处理器:

  

从JMeter 3.1开始,我们建议从BeanShell切换到JSR223测试元素

在此之前,您可以通过引用函数调用

来修复它
  print("Current date is:"+"${__time(yyyy-MM-dd)})";

此修复程序会将函数的返回值视为字符串。 当前它将其视为数字减法:2018-3-19 = 1996然后将其转换为字符串

答案 2 :(得分:0)

  1. 效果反模式#1:如果您使用GuiPackage课程,则无法在non-GUI mode中执行考试,请考虑迁移到FileServer而不是
  2. 性能反模式#2:不推荐使用Beanshell,因为在高负载的情况下它可能成为瓶颈。自JMeter 3.1 it is recommended to use JSR223 Test Elements and Groovy language起任何形式的脚本
  3. 性能反模式#3:不要在脚本体中内联JMeter Variables or Functions它们会破坏Groovy编译的脚本缓存功能,并且可能会解决会导致意外行为的事情(比如你的情况),编译失败甚至数据丢失。
  4. 所以:

    • 添加JSR223 PostProcessor代替Beanshell
    • 将以下代码放入"脚本"面积:

      String testPlanDir = org.apache.jmeter.services.FileServer.getFileServer().getBaseDir()
      File currentDate = new File(testPlanDir + File.separator + "CurrentDate.txt") 
      currentDate << new Date().format("yyyy-MM-dd")
      

答案 3 :(得分:0)

尝试使用 JSR223 PostProcessor 和语言 Groovy 并将其放入脚本区域:

def a = new Date().format('yyyy-MM-dd')
new File('Example.txt').withWriter('utf-8') { 
         writer -> writer.writeLine 'Current date is : ' + a.toString() }

(适用于 JMeter 4.0