I need to capture StartTime(Format:2021-04-28T12:09:09.000009), EndTime(Format:2021-04-28T12:09:09.000009) and test duration(milliseconds) of a JMeterLoad test. I planning to capture StartTime(Format:2021-04-28T12:09:09.000009) in SetupThreadGroup in Jsr223Sampler and EndTime(Format:2021-04-28T12:09:09.000009) & duration in tearDownThreadGroup using below code.
---SetupThread
def now = new Date()
def starttime = now.format("yyyy-MM-dd'T'HH:mm:ss.ssssss", TimeZone.getTimeZone('UTC'))
vars.put("StartTime", starttime)
--- TearDownThreadGroup
def now = new Date()
def endtime = now.format("yyyy-MM-dd'T'HH:mm:ss.ssssss", TimeZone.getTimeZone('UTC'))
vars.put("EndTime", endtime)
def diff = vars.get("StartTime")-endtime
Getting below error
2021-04-28 18:17:11,475 ERROR o.a.j.p.j.s.JSR223Sampler: Problem in JSR223 script GetEndTime_Duration, message: javax.script.ScriptException: java.lang.NullPointerException: Cannot invoke method minus() on null object
javax.script.ScriptException: java.lang.NullPointerException: Cannot invoke method minus() on null object
You have start time saved already in variable TESTSTART.MS
, you can use it in script, e.g.
long starttime = Long.valueOf(vars.get("TESTSTART.MS"))
def now = new Date()
def endtime = now.format("yyyy-MM-dd'T'HH:mm:ss.ssssss", TimeZone.getTimeZone('UTC'))
vars.put("EndTime", endtime)
def diff = now.getTime() - starttime
log.info String.valueOf(diff)