简体   繁体   中英

Getting, setting, and modifying timestamp in JMeter

In JMeter, I have a variable,

current_time = ${__time()}   

in the User Defined Variables.

My goal is to pass along a time that is 24 hours behind current time thru a HTTP Request.

I am able to cut off the milliseconds from the current time with

current_epoch_time = ${__groovy(vars.get("current_time").substring(0\,10))}

However, I am running into a wall of (1)trying to convert "current_epoch_time" into an integer and (2)subtract 24 hours from it.

I am still pretty novice in using JMeter and new to using groovy in general.

If you are already using groovy (or any other programming language), you don't need a variable, or a ${__time()} function, you can

${__groovy(System.currentTimeMillis()-24*60*60*1000)}

or

${__groovy(System.currentTimeMillis()-8400000)}

and if you want to be even more specific, you can use System.nanoTime()

The only reason to use variable is if you want the value to be the same for all places that use it, so you can add it in User Defined Variables as

Name: yourname
Value `${__groovy(System.currentTimeMillis()-8400000)}`

And then use it as ${yourname} .

If you still want to subtract it within the script, then this should work:

 ${__groovy(try {Long.parseLong(vars.get("current_time"))-8400000} catch(Exception e){})}

Since JMeter version 3.3 you have __timeShift() function allowing addition/substsraction of arbitrary amounts of time periods to either current or specific date.

So if you need to substract 24 hours from the current timestamp the relevant __timeShift() function configuration would be something like:

${__timeShift(,,-P1D,,)}

Use __timeShift() and __jexl3() functions combination

with __timeShift() you will get the date in past/future in milliseconds since start of the Unix epoch timeshift and with __jexl3() you can simply divide the resulting value by 1000 to get the value in seconds jexl3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM