001: /*
002: * soapUI, copyright (C) 2004-2007 eviware.com
003: *
004: * soapUI is free software; you can redistribute it and/or modify it under the
005: * terms of version 2.1 of the GNU Lesser General Public License as published by
006: * the Free Software Foundation.
007: *
008: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: * See the GNU Lesser General Public License for more details at gnu.org.
011: */
012:
013: package com.eviware.soapui.impl.wsdl.loadtest.strategy;
014:
015: import javax.swing.JComponent;
016: import javax.swing.JLabel;
017: import javax.swing.JPanel;
018: import javax.swing.JSpinner;
019: import javax.swing.SpinnerNumberModel;
020: import javax.swing.event.ChangeEvent;
021: import javax.swing.event.ChangeListener;
022:
023: import org.apache.log4j.Logger;
024: import org.apache.xmlbeans.XmlObject;
025:
026: import com.eviware.soapui.impl.wsdl.loadtest.WsdlLoadTest;
027: import com.eviware.soapui.impl.wsdl.loadtest.WsdlLoadTestRunner;
028: import com.eviware.soapui.model.testsuite.LoadTestRunContext;
029: import com.eviware.soapui.model.testsuite.LoadTestRunner;
030: import com.eviware.soapui.model.testsuite.TestRunContext;
031: import com.eviware.soapui.model.testsuite.TestRunner;
032: import com.eviware.soapui.support.UISupport;
033: import com.eviware.soapui.support.swing.ComponentBag;
034: import com.eviware.soapui.support.xml.XmlObjectConfigurationBuilder;
035: import com.eviware.soapui.support.xml.XmlObjectConfigurationReader;
036: import com.jgoodies.forms.builder.ButtonBarBuilder;
037:
038: /**
039: * LoadStrategy allowing maximum runs and request delays
040: *
041: * @author Ole.Matzura
042: */
043:
044: public class ThreadCountChangeLoadStrategy extends AbstractLoadStrategy {
045: private final static Logger log = Logger
046: .getLogger(ThreadCountChangeLoadStrategy.class);
047:
048: private static final int DEFAULT_END_THREAD_COUNT = 10;
049: private static final int DEFAULT_START_THREAD_COUNT = 1;
050: public static final String STRATEGY_TYPE = "Thread";
051:
052: private int startThreadCount = DEFAULT_START_THREAD_COUNT;
053: private int endThreadCount = DEFAULT_END_THREAD_COUNT;
054:
055: private JPanel configPanel;
056: private ComponentBag stateDependantComponents = new ComponentBag();
057:
058: private SpinnerNumberModel startThreadCountSpinnerNumberModel;
059: private JSpinner startThreadCountSpinner;
060: private SpinnerNumberModel endThreadCountSpinnerNumberModel;
061: private JSpinner endThreadCountSpinner;
062:
063: public ThreadCountChangeLoadStrategy(XmlObject config) {
064: super (STRATEGY_TYPE);
065:
066: if (config != null) {
067: XmlObjectConfigurationReader reader = new XmlObjectConfigurationReader(
068: config);
069: startThreadCount = reader.readInt("startThreadCount",
070: DEFAULT_START_THREAD_COUNT);
071: endThreadCount = reader.readInt("endThreadCount",
072: DEFAULT_END_THREAD_COUNT);
073: }
074: }
075:
076: public XmlObject getConfig() {
077: XmlObjectConfigurationBuilder builder = new XmlObjectConfigurationBuilder();
078: builder.add("startThreadCount", startThreadCount);
079: builder.add("endThreadCount", endThreadCount);
080: return builder.finish();
081: }
082:
083: public void beforeLoadTest(LoadTestRunner loadTestRunner,
084: LoadTestRunContext context) {
085: stateDependantComponents.setEnabled(false);
086:
087: WsdlLoadTest wsdlLoadTest = ((WsdlLoadTest) loadTestRunner
088: .getLoadTest());
089: wsdlLoadTest.setThreadCount(startThreadCount);
090: }
091:
092: public void afterLoadTest(LoadTestRunner loadTestRunner,
093: LoadTestRunContext context) {
094: stateDependantComponents.setEnabled(true);
095: }
096:
097: public boolean allowThreadCountChangeDuringRun() {
098: return false;
099: }
100:
101: public void beforeTestCase(LoadTestRunner loadTestRunner,
102: LoadTestRunContext context, TestRunner testRunner,
103: TestRunContext runContext) {
104: // calculate thread count
105: WsdlLoadTestRunner runner = (WsdlLoadTestRunner) loadTestRunner;
106: float progress = runner.getProgress();
107: if ((int) progress != -1) {
108: WsdlLoadTest wsdlLoadTest = ((WsdlLoadTest) loadTestRunner
109: .getLoadTest());
110: synchronized (wsdlLoadTest) {
111: int newThreadCount = (int) (startThreadCount + (progress
112: * (endThreadCount - startThreadCount) + 0.5));
113: if (newThreadCount != wsdlLoadTest.getThreadCount()) {
114: log.debug("Changing threadcount to "
115: + newThreadCount + ", progress = "
116: + progress);
117: wsdlLoadTest.setThreadCount(newThreadCount);
118: }
119: }
120: }
121: }
122:
123: public JComponent getConfigurationPanel() {
124: if (configPanel == null) {
125: ButtonBarBuilder builder = new ButtonBarBuilder();
126:
127: startThreadCountSpinnerNumberModel = new SpinnerNumberModel(
128: startThreadCount, 1, 10000, 1);
129: startThreadCountSpinner = new JSpinner(
130: startThreadCountSpinnerNumberModel);
131: UISupport.setPreferredHeight(startThreadCountSpinner, 18);
132: startThreadCountSpinner
133: .setToolTipText("Sets the initial thread-count");
134: startThreadCountSpinnerNumberModel
135: .addChangeListener(new ChangeListener() {
136:
137: public void stateChanged(ChangeEvent e) {
138: startThreadCount = startThreadCountSpinnerNumberModel
139: .getNumber().intValue();
140: notifyConfigurationChanged();
141: }
142: });
143:
144: builder.addFixed(new JLabel("Start Threads"));
145: builder.addRelatedGap();
146:
147: builder.addFixed(startThreadCountSpinner);
148: builder.addRelatedGap();
149:
150: endThreadCountSpinnerNumberModel = new SpinnerNumberModel(
151: endThreadCount, 1, 10000, 1);
152: endThreadCountSpinner = new JSpinner(
153: endThreadCountSpinnerNumberModel);
154: UISupport.setPreferredHeight(endThreadCountSpinner, 18);
155: endThreadCountSpinner
156: .setToolTipText("Sets the final thread-count");
157: endThreadCountSpinnerNumberModel
158: .addChangeListener(new ChangeListener() {
159:
160: public void stateChanged(ChangeEvent e) {
161: endThreadCount = endThreadCountSpinnerNumberModel
162: .getNumber().intValue();
163: notifyConfigurationChanged();
164: }
165: });
166:
167: builder.addFixed(new JLabel("End Threads"));
168: builder.addRelatedGap();
169: builder.addFixed(endThreadCountSpinner);
170:
171: configPanel = builder.getPanel();
172:
173: stateDependantComponents.add(startThreadCountSpinner);
174: stateDependantComponents.add(endThreadCountSpinner);
175: }
176:
177: return configPanel;
178: }
179:
180: /**
181: * Factory for ThreadCountChangeLoadStrategy class
182: *
183: * @author Ole.Matzura
184: */
185:
186: public static class Factory implements LoadStrategyFactory {
187: public String getType() {
188: return STRATEGY_TYPE;
189: }
190:
191: public LoadStrategy build(XmlObject config) {
192: return new ThreadCountChangeLoadStrategy(config);
193: }
194:
195: public LoadStrategy create() {
196: return new ThreadCountChangeLoadStrategy(null);
197: }
198: }
199: }
|