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.WsdlLoadTest;
24 import com.eviware.soapui.model.testsuite.LoadTestRunContext;
25 import com.eviware.soapui.model.testsuite.LoadTestRunner;
26 import com.eviware.soapui.model.testsuite.TestRunner.Status;
27 import com.eviware.soapui.support.DocumentListenerAdapter;
28 import com.eviware.soapui.support.UISupport;
29 import com.eviware.soapui.support.xml.XmlObjectConfigurationBuilder;
30 import com.eviware.soapui.support.xml.XmlObjectConfigurationReader;
31 import com.jgoodies.forms.builder.ButtonBarBuilder;
32
33 /***
34 * Burst LoadStrategy that pauses for a certain amount of time
35 *
36 * @author Ole.Matzura
37 */
38
39 public class BurstLoadStrategy extends AbstractLoadStrategy
40 {
41 private static final String BURST_DURATION_ELEMENT = "burstDuration";
42 private static final String BURST_DELAY_ELEMENT = "burstDelay";
43 private static final int DEFAULT_BURST_DURATION = 10000;
44 private static final int DEFAULT_BURST_DELAY = 60000;
45 public static final String STRATEGY_TYPE = "Burst";
46 private JPanel configPanel;
47
48 private int burstDelay = DEFAULT_BURST_DELAY;
49 private int burstDuration = DEFAULT_BURST_DURATION;
50 private long startTime;
51 private JTextField delayField;
52 private JTextField durationField;
53 private JLabel infoLabel;
54 private long threadCount;
55
56 public BurstLoadStrategy( WsdlLoadTest loadTest )
57 {
58 super( STRATEGY_TYPE, loadTest );
59
60 burstDelay = DEFAULT_BURST_DELAY;
61 burstDuration = DEFAULT_BURST_DURATION;
62 }
63
64 public BurstLoadStrategy( XmlObject config, WsdlLoadTest loadTest )
65 {
66 super( STRATEGY_TYPE, loadTest );
67
68 XmlObjectConfigurationReader reader = new XmlObjectConfigurationReader( config );
69 burstDelay = reader.readInt( BURST_DELAY_ELEMENT, DEFAULT_BURST_DELAY );
70 burstDuration = reader.readInt( BURST_DURATION_ELEMENT, DEFAULT_BURST_DURATION );
71 }
72
73 public void beforeLoadTest( LoadTestRunner loadTestRunner, LoadTestRunContext context )
74 {
75 super.beforeLoadTest( loadTestRunner, context );
76 startTime = System.currentTimeMillis();
77 if( infoLabel != null )
78 infoLabel.setText( "starting.." );
79
80 WsdlLoadTest wsdlLoadTest = ( WsdlLoadTest )loadTestRunner.getLoadTest();
81 threadCount = wsdlLoadTest.getThreadCount();
82 wsdlLoadTest.setThreadCount( 0 );
83 }
84
85 public void recalculate( LoadTestRunner loadTestRunner, LoadTestRunContext context )
86 {
87
88 long timePassed = System.currentTimeMillis() - startTime;
89
90 if( loadTestRunner.getStatus() == Status.RUNNING )
91 {
92 WsdlLoadTest wsdlLoadTest = ( WsdlLoadTest )loadTestRunner.getLoadTest();
93 String label = null;
94
95 long mod = timePassed % ( burstDelay + burstDuration );
96 if( mod < burstDelay )
97 {
98 wsdlLoadTest.setThreadCount( 0 );
99 label = ( burstDelay - mod ) / 1000 + "s delay left";
100 }
101 else
102 {
103 wsdlLoadTest.setThreadCount( threadCount );
104 label = ( ( burstDelay + burstDuration ) - mod ) / 1000 + "s burst left";
105 }
106
107 if( infoLabel != null && !infoLabel.getText().equals( label ) )
108 infoLabel.setText( label );
109 }
110 }
111
112 public void afterLoadTest( LoadTestRunner loadTestRunner, LoadTestRunContext context )
113 {
114 if( infoLabel != null )
115 infoLabel.setText( "" );
116
117
118 WsdlLoadTest wsdlLoadTest = ( WsdlLoadTest )loadTestRunner.getLoadTest();
119 wsdlLoadTest.setThreadCount( threadCount );
120 }
121
122 public JComponent getConfigurationPanel()
123 {
124 if( configPanel == null )
125 {
126 ButtonBarBuilder builder = new ButtonBarBuilder();
127
128 infoLabel = new JLabel();
129 delayField = new JTextField( 4 );
130 UISupport.setPreferredHeight( delayField, 18 );
131 delayField.setHorizontalAlignment( JTextField.RIGHT );
132 delayField.setText( String.valueOf( burstDelay / 1000 ) );
133 delayField.setToolTipText( "Sets the delay before each burst run in seconds" );
134 delayField.getDocument().addDocumentListener( new DocumentListenerAdapter()
135 {
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
164 public void update( Document doc )
165 {
166 try
167 {
168 burstDuration = Integer.parseInt( durationField.getText() ) * 1000;
169 notifyConfigurationChanged();
170 }
171 catch( NumberFormatException e )
172 {
173 }
174 }
175 } );
176
177 builder.addFixed( new JLabel( "Burst Duration" ) );
178 builder.addRelatedGap();
179 builder.addFixed( durationField );
180 builder.addRelatedGap();
181 builder.addFixed( infoLabel );
182
183 configPanel = builder.getPanel();
184 }
185
186 return configPanel;
187 }
188
189 public XmlObject getConfig()
190 {
191 XmlObjectConfigurationBuilder builder = new XmlObjectConfigurationBuilder();
192 builder.add( BURST_DELAY_ELEMENT, burstDelay );
193 builder.add( BURST_DURATION_ELEMENT, burstDuration );
194 return builder.finish();
195 }
196
197 @Override
198 public boolean allowThreadCountChangeDuringRun()
199 {
200 return false;
201 }
202
203 /***
204 * Factory for BurstLoadStrategy class
205 *
206 * @author Ole.Matzura
207 */
208
209 public static class Factory implements LoadStrategyFactory
210 {
211 public String getType()
212 {
213 return STRATEGY_TYPE;
214 }
215
216 public LoadStrategy build( XmlObject config, WsdlLoadTest loadTest )
217 {
218 return new BurstLoadStrategy( config, loadTest );
219 }
220
221 public LoadStrategy create( WsdlLoadTest loadTest )
222 {
223 return new BurstLoadStrategy( loadTest );
224 }
225 }
226 }