1

I'm trying to get today's date using time function of jmeter with the format "${__time(yyyy-MM-dd)}" in BeanShell postprocessor. But after executing the Beanshell postprocessor the result shows as "1996". Basically the "time" function is displaying result by subtracting the values like "2018-03-19"=1996. Please help me to resolve this issue so that i can get current date and time. Beanshell code as below

 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();
Praveen PS
  • 127
  • 2
  • 5
  • 17

4 Answers4

3

Set time function call as parameter of your PreProcessor (by the way migrate to JSR223 + Groovy for performances)

And use it then with variable Parameters:

Using Parameters in Groovy

UBIK LOAD PACK
  • 33,980
  • 5
  • 71
  • 116
1

Try this with JSR223 PostProcessor and language Groovy and put this into script area:

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

(It works on JMeter 4.0)

John Smith
  • 21
  • 4
0

You should move to JSR223 postprocessor according to JMeter Best Practices:

Since JMeter 3.1, we advise switching from BeanShell to JSR223 Test Elements

Until then you can fix it by quoting the function call as

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

This fix will treat the function's return value as string. Currenty it treat it as a numeric substraction: 2018-3-19=1996 and then convert it to string

Ori Marko
  • 56,308
  • 23
  • 131
  • 233
0
  1. Performance anti-pattern #1: given you use GuiPackage class you won't be able to execute your test in non-GUI mode, consider migrating to FileServer instead
  2. Performance anti-pattern #2: using Beanshell is not recommended as in case of high loads it might become the bottleneck. Since JMeter 3.1 it is recommended to use JSR223 Test Elements and Groovy language for any form of scripting
  3. Performance anti-pattern #3: don't inline JMeter Variables or Functions in script body they break Groovy compiled scripts caching function and might resolve into something which will cause unexpected behaviour (like in your case), compilation failure or even data loss.

So:

  • Add JSR223 PostProcessor instead of the Beanshell one
  • Put the following code into "Script" area:

    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")
    
Dmitri T
  • 159,985
  • 5
  • 83
  • 133