1
votes

i have timestamp values in a csv file and i'm loading the file in a ThreadgGoup in jMeter. I'm using a JSR223 script to calculate the delay time between each thread but i'm having trouble doing that.

I want to launch my threads based no real life data, that's why i'm using the timestamps from a csv log file. but i don't know how to configure the equation for this. I'm reading the timestamp from the csv in my script and i know that the wait time should equal the current timestamp value - previous thread timestamp value but how can i implement this?

def long wait=0;

// this returns the currents threads timestamp value from the csv
def long ts = Double.valueOf(vars.get("timestamp")).longValue();

wait = ts - {previous timestamp in csv}

return wait;

I'm using groovy to write the scripts and in my threadgroup i have the httpRequest and in it the JSR223 Timer. Any thoughts?

3
@user7294900 i already checked that post. that guy is using a constant timer. I don't want that. I'm trying to create a timer that will change its value for each thread based on the timestamps it reads in the csv file.hashguard

3 Answers

0
votes

Instead of return the wait you should pass it to sleep method:

 sleep(wait);

For reading CSV see answers

0
votes

You code should be:

 // this returns the currents threads timestamp value from the csv
 long ts = vars["timestamp"].toLong();
 long wait = ts - {previous timestamp in csv}
 return wait;

What I don't understand is where this value comes from:

{previous timestamp in csv}

Finally you need to understand how timer work:

So if you really want to apply it between sampler, ensure you put your timer as a child of Flow Control Action:

JSR223 Timer as child of Flow Control Action

0
votes

Get used to look into jmeter.log file - normally it should contain the reason of your Groovy (or JMeter) test misbehaviour or failure. The Log Viewer window can be opened by clicking on a yellow triangle in the upper right corner of JMeter GUI

Amend your code to look like:

if (vars.get('previousTimestamp') != null) {
    long ts = vars.get('timestamp') as Long
    long wait = ts - (vars.get('previousTimestamp') as Long)
    return wait
} else {
    vars.put('previousTimestamp', vars.get('timestamp') as String)
    //return 1000
}

The logic is:

  1. If previousTimestamp variable is defined - "sleep" for delta time between the previous and the current timestamps
  2. If previousTimestamp variable is not defined - store current timestamp variable as previousTimestamp and don't sleep. You can uncomment return 1000 line to provide a "default" sleep of 1 second

Just in case check out Mathematical Functions and Converting Data Types in Groovy guide, it should give you more insight on what you're trying to implement.