JMeter Inner nested loop counter not resetting for subsequent loops

Viewed 20

I basically have this structure - How to implement nested loop in jmeter?.

The outer loop works as it should. In my case, the outer loop executes 3 times and executes a SQL query each time.

The inner loop executions are based on the # of rows a query returns, so if the first query returns 10 rows, the inner loop execute 10 times. This works great for the 1st outer and inner loop execution. The problem is that the JMeter Counter object in the inner loop does not reset for the second query.

In other words, if the second query in the outer loop returned 5 rows of data, the Counter is still going to count from 1 to 10 based on the first query. Even worse, when it gets to the inner loop of the 3rd query, it's going to start at 6 and continue counting up to 10, then start all over again at 1.

The JMeter inner Loop Controller picks up the new count value and executes the correct number of times, but the query result data is written to a new DB based on an index # (i.e. 1 to #_of_records) so without this index #, I can't properly write out this data. Where p_cnt_tq below is the index # I referred to -

INSERT INTO TopQuery (Id,Database_name,Schema_name,sqltext,total,avg_duration,max_duration,avg_reads,max_reads,avg_writes,max_writes,avg_CPU_time,max_CPU_time) VALUES ("${p_max_id_1}","${_V(P_DbName${p_cnt_tq})}","${_V(P_SchemaName${p_cnt_tq})}","${_V(P_SqlText${p_cnt_tq})}","${_V(P_ExecCount${p_cnt_tq})}","${_V(P_AvgDuration${p_cnt_tq})}","${_V(P_MaxDuration${p_cnt_tq})}","${_V(P_AvgReads${p_cnt_tq})}","${_V(P_MaxReads${p_cnt_tq})}","${_V(P_AvgWrites${p_cnt_tq})}","${_V(P_MaxWrites${p_cnt_tq})}","${_V(P_AvgCpuTime${p_cnt_tq})}","${_V(P_MaxCpuTime${p_cnt_tq})}");

While this seems like it would be ridiculously easy to fix, I have not found a way to create a counter inside the inner loop that counts from 1 to the #_of_rows returned from each outer loop. Has anyone else run across and solved this puzzle?

enter image description here

1 Answers

Why do you call it "puzzle" and why do you expect the counter to "reset"? Have you tried to read the documentation?

Allows the user to create a counter that can be referenced anywhere in the Thread Group. The counter config lets the user configure a starting point, a maximum, and the increment. The counter will loop from the start to the max, and then start over with the start, continuing on like that until the test is ended.

You ticked the box to Reset counter on each Thread Group Iteration but "Outer" Loop Controller's iteration is not a Thread Group iteration


In general I fail to see why would you need to have this Counter at all, Loop Controller exposes a special JMeter Variable which returns the current iteration number.

JMeter will expose the looping index as a variable named __jm__<Name of your element>__idx. So for example, if your Loop Controller is named LC, then you can access the looping index through ${__jm__LC__idx}. Index starts at 0

It's zero-based but you can easily add 1 to it using __intSum() function

enter image description here

Related