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.JTextField;
019: import javax.swing.text.Document;
020:
021: import org.apache.xmlbeans.XmlObject;
022:
023: import com.eviware.soapui.SoapUI;
024: import com.eviware.soapui.impl.wsdl.loadtest.WsdlLoadTestRunner;
025: import com.eviware.soapui.model.testsuite.LoadTestRunContext;
026: import com.eviware.soapui.model.testsuite.LoadTestRunner;
027: import com.eviware.soapui.model.testsuite.TestRunContext;
028: import com.eviware.soapui.model.testsuite.TestRunner;
029: import com.eviware.soapui.support.DocumentListenerAdapter;
030: import com.eviware.soapui.support.UISupport;
031: import com.eviware.soapui.support.xml.XmlObjectConfigurationBuilder;
032: import com.eviware.soapui.support.xml.XmlObjectConfigurationReader;
033: import com.jgoodies.forms.builder.ButtonBarBuilder;
034:
035: /**
036: * Burst LoadStrategy that pauses for a certain amount of time
037: *
038: * @author Ole.Matzura
039: */
040:
041: public class BurstLoadStrategy extends AbstractLoadStrategy {
042: private static final String BURST_DURATION_ELEMENT = "burstDuration";
043: private static final String BURST_DELAY_ELEMENT = "burstDelay";
044: private static final int DEFAULT_BURST_DURATION = 10000;
045: private static final int DEFAULT_BURST_DELAY = 60000;
046: private static final int SLEEP_DELAY = 500;
047: public static final String STRATEGY_TYPE = "Burst";
048: private JPanel configPanel;
049:
050: private int burstDelay = DEFAULT_BURST_DELAY;
051: private int burstDuration = DEFAULT_BURST_DURATION;
052: private long startTime;
053: private JTextField delayField;
054: private JTextField durationField;
055: private JLabel infoLabel = new JLabel();
056:
057: public BurstLoadStrategy() {
058: super (STRATEGY_TYPE);
059:
060: burstDelay = DEFAULT_BURST_DELAY;
061: burstDuration = DEFAULT_BURST_DURATION;
062: }
063:
064: public BurstLoadStrategy(XmlObject config) {
065: super (STRATEGY_TYPE);
066:
067: XmlObjectConfigurationReader reader = new XmlObjectConfigurationReader(
068: config);
069: burstDelay = reader.readInt(BURST_DELAY_ELEMENT,
070: DEFAULT_BURST_DELAY);
071: burstDuration = reader.readInt(BURST_DURATION_ELEMENT,
072: DEFAULT_BURST_DURATION);
073: }
074:
075: public void beforeLoadTest(LoadTestRunner loadTestRunner,
076: LoadTestRunContext context) {
077: startTime = System.currentTimeMillis();
078: infoLabel.setText("starting..");
079: }
080:
081: public void beforeTestCase(LoadTestRunner loadTestRunner,
082: LoadTestRunContext context, TestRunner testRunner,
083: TestRunContext runContext) {
084: // get time passed since start of test
085: long timePassed = System.currentTimeMillis() - startTime;
086:
087: // wait during burstDelay period
088: long mod = timePassed % (burstDelay + burstDuration);
089: while ((mod < burstDelay)
090: && (testRunner.getStatus() == TestRunner.Status.RUNNING || testRunner
091: .getStatus() == TestRunner.Status.INITIALIZED)) {
092: try {
093: String label = (burstDelay - mod) / 1000
094: + "s delay left";
095: if (!infoLabel.getText().equals(label))
096: infoLabel.setText(label);
097:
098: Thread.sleep(SLEEP_DELAY);
099: if (((WsdlLoadTestRunner) loadTestRunner).getProgress() >= 1) {
100: infoLabel.setText("");
101: return;
102: }
103:
104: timePassed = System.currentTimeMillis() - startTime;
105: mod = timePassed % (burstDelay + burstDuration);
106: } catch (InterruptedException e) {
107: SoapUI.logError(e);
108: }
109: }
110:
111: if (testRunner.getStatus() == TestRunner.Status.RUNNING) {
112: String label = ((burstDelay + burstDuration) - mod) / 1000
113: + "s burst left";
114: if (!infoLabel.getText().equals(label))
115: infoLabel.setText(label);
116: }
117: }
118:
119: public void afterLoadTest(LoadTestRunner loadTestRunner,
120: LoadTestRunContext context) {
121: infoLabel.setText("");
122: }
123:
124: public JComponent getConfigurationPanel() {
125: if (configPanel == null) {
126: ButtonBarBuilder builder = new ButtonBarBuilder();
127:
128: delayField = new JTextField(4);
129: UISupport.setPreferredHeight(delayField, 18);
130: delayField.setHorizontalAlignment(JTextField.RIGHT);
131: delayField.setText(String.valueOf(burstDelay / 1000));
132: delayField
133: .setToolTipText("Sets the delay before each burst run in seconds");
134: delayField.getDocument().addDocumentListener(
135: new DocumentListenerAdapter() {
136:
137: public void update(Document doc) {
138: try {
139: burstDelay = Integer
140: .parseInt(delayField.getText()) * 1000;
141: notifyConfigurationChanged();
142: } catch (NumberFormatException e) {
143: }
144: }
145: });
146:
147: builder.addFixed(new JLabel("Burst Delay"));
148: builder.addRelatedGap();
149:
150: builder.addFixed(delayField);
151: builder.addRelatedGap();
152:
153: durationField = new JTextField(4);
154: UISupport.setPreferredHeight(durationField, 18);
155: durationField.setHorizontalAlignment(JTextField.RIGHT);
156: durationField.setText(String.valueOf(burstDuration / 1000));
157: durationField
158: .setToolTipText("Specifies the duration of a burst run in seconds");
159: durationField.getDocument().addDocumentListener(
160: new DocumentListenerAdapter() {
161:
162: public void update(Document doc) {
163: try {
164: burstDuration = Integer
165: .parseInt(durationField
166: .getText()) * 1000;
167: notifyConfigurationChanged();
168: } catch (NumberFormatException e) {
169: }
170: }
171: });
172:
173: builder.addFixed(new JLabel("Burst Duration"));
174: builder.addRelatedGap();
175: builder.addFixed(durationField);
176: builder.addRelatedGap();
177: builder.addFixed(infoLabel);
178:
179: configPanel = builder.getPanel();
180: }
181:
182: return configPanel;
183: }
184:
185: public XmlObject getConfig() {
186: XmlObjectConfigurationBuilder builder = new XmlObjectConfigurationBuilder();
187: builder.add(BURST_DELAY_ELEMENT, burstDelay);
188: builder.add(BURST_DURATION_ELEMENT, burstDuration);
189: return builder.finish();
190: }
191:
192: /**
193: * Factory for BurstLoadStrategy class
194: *
195: * @author Ole.Matzura
196: */
197:
198: public static class Factory implements LoadStrategyFactory {
199: public String getType() {
200: return STRATEGY_TYPE;
201: }
202:
203: public LoadStrategy build(XmlObject config) {
204: return new BurstLoadStrategy(config);
205: }
206:
207: public LoadStrategy create() {
208: return new BurstLoadStrategy();
209: }
210: }
211: }
|