Lines 19-36
Link Here
|
19 |
package org.apache.jmeter.control; |
19 |
package org.apache.jmeter.control; |
20 |
|
20 |
|
21 |
import java.io.Serializable; |
21 |
import java.io.Serializable; |
|
|
22 |
import java.util.concurrent.atomic.AtomicBoolean; |
23 |
import java.util.concurrent.locks.Lock; |
24 |
import java.util.concurrent.locks.ReadWriteLock; |
25 |
import java.util.concurrent.locks.ReentrantReadWriteLock; |
22 |
|
26 |
|
23 |
import org.apache.jmeter.samplers.Sampler; |
27 |
import org.apache.jmeter.samplers.Sampler; |
24 |
import org.apache.jmeter.testelement.TestElement; |
28 |
import org.apache.jmeter.testelement.TestElement; |
|
|
29 |
import org.apache.jmeter.testelement.TestStateListener; |
30 |
import org.apache.jmeter.testelement.property.BooleanProperty; |
25 |
import org.apache.jmeter.testelement.property.IntegerProperty; |
31 |
import org.apache.jmeter.testelement.property.IntegerProperty; |
26 |
|
32 |
|
27 |
/** |
33 |
/** |
28 |
* Alternate among each of the children controllers or samplers for each loop iteration |
34 |
* Alternate among each of the children controllers or samplers for each loop iteration |
29 |
*/ |
35 |
*/ |
30 |
public class InterleaveControl extends GenericController implements Serializable { |
36 |
public class InterleaveControl extends GenericController implements Serializable, TestStateListener { |
31 |
private static final long serialVersionUID = 233L; |
37 |
|
|
|
38 |
public static final class SafeCounter { |
39 |
private ReadWriteLock rwlock = new ReentrantReadWriteLock(); |
40 |
|
41 |
private Lock rlock = rwlock.readLock(); |
42 |
private Lock wlock = rwlock.writeLock(); |
43 |
|
44 |
private AtomicBoolean initialized = new AtomicBoolean(); |
45 |
private int maxValue; |
46 |
private int counter; |
47 |
|
48 |
public SafeCounter() { |
49 |
} |
50 |
|
51 |
public int getCounter() { |
52 |
try { |
53 |
rlock.lock(); |
54 |
return counter; |
55 |
} finally { |
56 |
rlock.unlock(); |
57 |
} |
58 |
} |
59 |
|
60 |
public int getAndIncrement() { |
61 |
int result; |
62 |
try { |
63 |
wlock.lock(); |
64 |
result = counter++; |
65 |
if(counter == maxValue) { |
66 |
counter = 0; |
67 |
} |
68 |
return result; |
69 |
} finally { |
70 |
wlock.unlock(); |
71 |
} |
72 |
} |
73 |
|
74 |
public void initIfNeeded(int maxValue) { |
75 |
try { |
76 |
wlock.lock(); |
77 |
if(initialized.compareAndSet(false, true)) { |
78 |
this.maxValue = maxValue; |
79 |
} |
80 |
} finally { |
81 |
wlock.unlock(); |
82 |
} |
83 |
} |
84 |
|
85 |
public void reset() { |
86 |
this.counter = 0; |
87 |
this.initialized.set(false); |
88 |
} |
89 |
} |
90 |
|
91 |
private static final long serialVersionUID = 234L; |
32 |
|
92 |
|
33 |
private static final String STYLE = "InterleaveControl.style";// $NON-NLS-1$ |
93 |
private static final String STYLE = "InterleaveControl.style";// $NON-NLS-1$ |
|
|
94 |
|
95 |
private static final String ACCROSS_THREADS = "InterleaveControl.accrossThreads";// $NON-NLS-1$ |
34 |
|
96 |
|
35 |
public static final int IGNORE_SUB_CONTROLLERS = 0; |
97 |
public static final int IGNORE_SUB_CONTROLLERS = 0; |
36 |
|
98 |
|
Lines 43-53
Link Here
|
43 |
private boolean currentReturnedAtLeastOne; |
105 |
private boolean currentReturnedAtLeastOne; |
44 |
|
106 |
|
45 |
private boolean stillSame = true; |
107 |
private boolean stillSame = true; |
|
|
108 |
|
109 |
private transient SafeCounter sharedCounter = new SafeCounter(); |
110 |
|
111 |
private transient boolean interleaveAccrossThreads; |
46 |
|
112 |
|
47 |
/*************************************************************************** |
113 |
/*************************************************************************** |
48 |
* Constructor for the InterleaveControl object |
114 |
* Constructor for the InterleaveControl object |
49 |
**************************************************************************/ |
115 |
**************************************************************************/ |
50 |
public InterleaveControl() { |
116 |
public InterleaveControl() { |
|
|
117 |
super(); |
51 |
} |
118 |
} |
52 |
|
119 |
|
53 |
/** |
120 |
/** |
Lines 71-76
Link Here
|
71 |
public int getStyle() { |
138 |
public int getStyle() { |
72 |
return getPropertyAsInt(STYLE); |
139 |
return getPropertyAsInt(STYLE); |
73 |
} |
140 |
} |
|
|
141 |
|
142 |
public void setInterleaveAccrossThreads(boolean accrossThreads) { |
143 |
setProperty(new BooleanProperty(ACCROSS_THREADS, accrossThreads)); |
144 |
} |
145 |
|
146 |
public boolean getInterleaveAccrossThreads() { |
147 |
return getPropertyAsBoolean(ACCROSS_THREADS, false); |
148 |
} |
74 |
|
149 |
|
75 |
/** |
150 |
/** |
76 |
* {@inheritDoc} |
151 |
* {@inheritDoc} |
Lines 172-177
Link Here
|
172 |
skipNext = true; |
247 |
skipNext = true; |
173 |
} |
248 |
} |
174 |
stillSame = false; |
249 |
stillSame = false; |
175 |
super.incrementCurrent(); |
250 |
if(!interleaveAccrossThreads) { |
|
|
251 |
current = sharedCounter.getAndIncrement(); |
252 |
} else { |
253 |
super.incrementCurrent(); |
254 |
} |
255 |
} |
256 |
|
257 |
@Override |
258 |
public Object clone() { |
259 |
InterleaveControl clone = (InterleaveControl) super.clone(); |
260 |
// Share the counter among threads |
261 |
clone.sharedCounter = sharedCounter; |
262 |
return clone; |
263 |
} |
264 |
|
265 |
/* (non-Javadoc) |
266 |
* @see org.apache.jmeter.control.GenericController#initialize() |
267 |
*/ |
268 |
@Override |
269 |
public void initialize() { |
270 |
super.initialize(); |
271 |
sharedCounter.initIfNeeded(getSubControllers().size()); |
272 |
interleaveAccrossThreads = getInterleaveAccrossThreads(); |
273 |
// get a different start index |
274 |
if(interleaveAccrossThreads) { |
275 |
this.current = sharedCounter.getAndIncrement(); |
276 |
} |
277 |
} |
278 |
|
279 |
@Override |
280 |
public void testStarted() { |
281 |
sharedCounter.reset(); |
282 |
} |
283 |
|
284 |
@Override |
285 |
public void testStarted(String host) { |
286 |
testStarted(); |
287 |
} |
288 |
|
289 |
@Override |
290 |
public void testEnded() { |
291 |
// NOOP |
292 |
} |
293 |
|
294 |
@Override |
295 |
public void testEnded(String host) { |
296 |
// NOOP |
176 |
} |
297 |
} |
177 |
} |
298 |
} |