SoapUI: forcing unique counters on load tests

When you configure a load test to use several threads, each thread will be created and set up with its own copy of the environment's variables. So, if you use a counter to uniquely identify a payload, chances are you're going to see repeated values being used. Actually, you don't need several threads to get into this tangle - having more than one thread is enough.

To avoid this, you must synchronize the code block that increments the counter (at the TestSuite level), and then populate a "local" variable (to the running thread) with the new value. Here's a very basic example of a Groovy script that deals with the problem:

def ts = testRunner.testCase.testSuite
int counter

	synchronized(ts) {
		 * If the variable doesn't exist, create and initialize it
		if (null == ts.getPropertyValue("counter")) {
		   ts.setPropertyValue("counter", "1")
		   counter = 1;
		} else {
		   counter = ts.getPropertyValue("counter").toInteger()

		ts.setPropertyValue("counter", counter.toString())

		// Save the new value to the thread's context to avoid interference between each other
		context.put("localCounter", counter.toString())
} catch(Exception e) { }

And that's it. To inject the variable on the payload, just declare it as this: ${localCounter}.


Posted by Fabio Mazanatti at Jan 21, 2014 - 15:51
Tags: ,

Comments:  leave one