1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.impl.wsdl.loadtest.strategy;
14
15 import javax.swing.JComponent;
16 import javax.swing.JLabel;
17 import javax.swing.JPanel;
18 import javax.swing.JTextField;
19 import javax.swing.text.Document;
20
21 import org.apache.xmlbeans.XmlObject;
22
23 import com.eviware.soapui.impl.wsdl.loadtest.WsdlLoadTestRunner;
24 import com.eviware.soapui.model.testsuite.LoadTestRunContext;
25 import com.eviware.soapui.model.testsuite.LoadTestRunner;
26 import com.eviware.soapui.model.testsuite.TestRunContext;
27 import com.eviware.soapui.model.testsuite.TestRunner;
28 import com.eviware.soapui.support.DocumentListenerAdapter;
29 import com.eviware.soapui.support.UISupport;
30 import com.eviware.soapui.support.xml.XmlObjectConfigurationBuilder;
31 import com.eviware.soapui.support.xml.XmlObjectConfigurationReader;
32 import com.jgoodies.forms.builder.ButtonBarBuilder;
33
34 /***
35 * Burst LoadStrategy that pauses for a certain amount of time
36 *
37 * @author Ole.Matzura
38 */
39
40 public class BurstLoadStrategy extends AbstractLoadStrategy
41 {
42 private static final String BURST_DURATION_ELEMENT = "burstDuration";
43 private static final String BURST_DELAY_ELEMENT = "burstDelay";
44 private static final int DEFAULT_BURST_DURATION = 10000;
45 private static final int DEFAULT_BURST_DELAY = 60000;
46 private static final int SLEEP_DELAY = 500;
47 public static final String STRATEGY_TYPE = "Burst";
48 private JPanel configPanel;
49
50 private int burstDelay = DEFAULT_BURST_DELAY;
51 private int burstDuration = DEFAULT_BURST_DURATION;
52 private long startTime;
53 private JTextField delayField;
54 private JTextField durationField;
55 private JLabel infoLabel = new JLabel();
56
57 public BurstLoadStrategy()
58 {
59 super( STRATEGY_TYPE );
60
61 burstDelay = DEFAULT_BURST_DELAY;
62 burstDuration = DEFAULT_BURST_DURATION;
63 }
64
65 public BurstLoadStrategy(XmlObject config)
66 {
67 super( STRATEGY_TYPE );
68
69 XmlObjectConfigurationReader reader = new XmlObjectConfigurationReader( config );
70 burstDelay = reader.readInt( BURST_DELAY_ELEMENT, DEFAULT_BURST_DELAY );
71 burstDuration = reader.readInt( BURST_DURATION_ELEMENT, DEFAULT_BURST_DURATION );
72 }
73
74 public void beforeLoadTest(LoadTestRunner loadTestRunner, LoadTestRunContext context)
75 {
76 startTime = System.currentTimeMillis();
77 infoLabel.setText( "starting.." );
78 }
79
80 public void beforeTestCase( LoadTestRunner loadTestRunner, LoadTestRunContext context, TestRunner testRunner, TestRunContext runContext)
81 {
82
83 long timePassed = System.currentTimeMillis() - startTime;
84
85
86 long mod = timePassed % (burstDelay + burstDuration);
87 while( (mod < burstDelay) && (testRunner.getStatus() == TestRunner.Status.RUNNING || testRunner.getStatus() == TestRunner.Status.INITIALIZED ))
88 {
89 try
90 {
91 String label = (burstDelay-mod)/1000 + "s delay left";
92 if( !infoLabel.getText().equals( label ))
93 infoLabel.setText( label );
94
95 Thread.sleep( SLEEP_DELAY );
96 if( ((WsdlLoadTestRunner)loadTestRunner).getProgress() >= 1 )
97 {
98 infoLabel.setText( "" );
99 return;
100 }
101
102 timePassed = System.currentTimeMillis() - startTime;
103 mod = timePassed % (burstDelay + burstDuration);
104 }
105 catch (InterruptedException e)
106 {
107 e.printStackTrace();
108 }
109 }
110
111 if( testRunner.getStatus() == TestRunner.Status.RUNNING )
112 {
113 String label = ((burstDelay + burstDuration) - mod)/1000 + "s burst left";
114 if( !infoLabel.getText().equals( label ))
115 infoLabel.setText( label );
116 }
117 }
118
119 public void afterLoadTest(LoadTestRunner loadTestRunner, LoadTestRunContext context)
120 {
121 infoLabel.setText( "" );
122 }
123
124 public JComponent getConfigurationPanel()
125 {
126 if( configPanel == null )
127 {
128 ButtonBarBuilder builder = new ButtonBarBuilder();
129
130 delayField = new JTextField( 4 );
131 UISupport.setPreferredHeight( delayField, 18 );
132 delayField.setHorizontalAlignment( JTextField.RIGHT );
133 delayField.setText( String.valueOf( burstDelay/1000 ));
134 delayField.setToolTipText( "Sets the delay before each burst run in seconds" );
135 delayField.getDocument().addDocumentListener( new DocumentListenerAdapter(){
136
137 public void update( Document doc )
138 {
139 try
140 {
141 burstDelay = Integer.parseInt(delayField.getText())*1000;
142 notifyConfigurationChanged();
143 }
144 catch (NumberFormatException e)
145 {
146 }
147 }}
148 );
149
150 builder.addFixed( new JLabel( "Burst Delay" ));
151 builder.addRelatedGap();
152
153 builder.addFixed( delayField );
154 builder.addRelatedGap();
155
156 durationField = new JTextField(4);
157 UISupport.setPreferredHeight( durationField, 18 );
158 durationField.setHorizontalAlignment( JTextField.RIGHT );
159 durationField.setText( String.valueOf( burstDuration/1000 ));
160 durationField.setToolTipText( "Specifies the duration of a burst run in seconds" );
161 durationField.getDocument().addDocumentListener( new DocumentListenerAdapter(){
162
163 public void update( Document doc )
164 {
165 try
166 {
167 burstDuration = Integer.parseInt(durationField.getText())*1000;
168 notifyConfigurationChanged();
169 }
170 catch (NumberFormatException e)
171 {
172 }
173 }}
174 );
175
176 builder.addFixed( new JLabel( "Burst Duration" ));
177 builder.addRelatedGap();
178 builder.addFixed( durationField);
179 builder.addRelatedGap();
180 builder.addFixed( infoLabel );
181
182 configPanel = builder.getPanel();
183 }
184
185 return configPanel;
186 }
187
188 public XmlObject getConfig()
189 {
190 XmlObjectConfigurationBuilder builder = new XmlObjectConfigurationBuilder();
191 builder.add( BURST_DELAY_ELEMENT, burstDelay );
192 builder.add( BURST_DURATION_ELEMENT, burstDuration );
193 return builder.finish();
194 }
195
196 /***
197 * Factory for BurstLoadStrategy class
198 *
199 * @author Ole.Matzura
200 */
201
202 public static class Factory implements LoadStrategyFactory
203 {
204 public String getType()
205 {
206 return STRATEGY_TYPE;
207 }
208
209 public LoadStrategy build(XmlObject config)
210 {
211 return new BurstLoadStrategy( config );
212 }
213
214 public LoadStrategy create()
215 {
216 return new BurstLoadStrategy();
217 }
218 }
219 }