View | Details | Raw Unified | Return to bug 60081
Collapse All | Expand All

(-)src/components/org/apache/jmeter/control/InterleaveControl.java (-3 / +124 lines)
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
}
(-)src/components/org/apache/jmeter/control/gui/InterleaveControlGui.java (-1 / +11 lines)
Lines 30-35 Link Here
30
    private static final long serialVersionUID = 240L;
30
    private static final long serialVersionUID = 240L;
31
31
32
    private JCheckBox style;
32
    private JCheckBox style;
33
    
34
    private JCheckBox accrossThreads;
33
35
34
    public InterleaveControlGui() {
36
    public InterleaveControlGui() {
35
        init();
37
        init();
Lines 38-48 Link Here
38
    @Override
40
    @Override
39
    public void configure(TestElement el) {
41
    public void configure(TestElement el) {
40
        super.configure(el);
42
        super.configure(el);
41
        if (((InterleaveControl) el).getStyle() == InterleaveControl.IGNORE_SUB_CONTROLLERS) {
43
        InterleaveControl controller = (InterleaveControl) el;
44
        if (controller.getStyle() == InterleaveControl.IGNORE_SUB_CONTROLLERS) {
42
            style.setSelected(true);
45
            style.setSelected(true);
43
        } else {
46
        } else {
44
            style.setSelected(false);
47
            style.setSelected(false);
45
        }
48
        }
49
        accrossThreads.setSelected(controller.getInterleaveAccrossThreads());
46
    }
50
    }
47
51
48
    @Override
52
    @Override
Lines 65-70 Link Here
65
        } else {
69
        } else {
66
            ((InterleaveControl) ic).setStyle(InterleaveControl.USE_SUB_CONTROLLERS);
70
            ((InterleaveControl) ic).setStyle(InterleaveControl.USE_SUB_CONTROLLERS);
67
        }
71
        }
72
        
73
        ((InterleaveControl) ic).setInterleaveAccrossThreads(accrossThreads.isSelected());
68
    }
74
    }
69
75
70
    /**
76
    /**
Lines 74-79 Link Here
74
    public void clearGui() {
80
    public void clearGui() {
75
        super.clearGui();
81
        super.clearGui();
76
        style.setSelected(false);
82
        style.setSelected(false);
83
        accrossThreads.setSelected(false);
77
    }
84
    }
78
85
79
    @Override
86
    @Override
Lines 89-93 Link Here
89
96
90
        style = new JCheckBox(JMeterUtils.getResString("ignore_subcontrollers")); // $NON-NLS-1$
97
        style = new JCheckBox(JMeterUtils.getResString("ignore_subcontrollers")); // $NON-NLS-1$
91
        add(CheckBoxPanel.wrap(style));
98
        add(CheckBoxPanel.wrap(style));
99
        
100
        accrossThreads = new JCheckBox(JMeterUtils.getResString("interleave_accross_threads")); // $NON-NLS-1$
101
        add(CheckBoxPanel.wrap(accrossThreads));
92
    }
102
    }
93
}
103
}

Return to bug 60081