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 public JComponent getConfigurationPanel()
119 {
120 if( configPanel == null )
121 {
122 ButtonBarBuilder builder = new ButtonBarBuilder();
123
124 infoLabel = new JLabel();
125 delayField = new JTextField( 4 );
126 UISupport.setPreferredHeight( delayField, 18 );
127 delayField.setHorizontalAlignment( JTextField.RIGHT );
128 delayField.setText( String.valueOf( burstDelay / 1000 ) );
129 delayField.setToolTipText( "Sets the delay before each burst run in seconds" );
130 delayField.getDocument().addDocumentListener( new DocumentListenerAdapter()
131 {
132
133 public void update( Document doc )
134 {
135 try
136 {
137 burstDelay = Integer.parseInt( delayField.getText() ) * 1000;
138 notifyConfigurationChanged();
139 }
140 catch( NumberFormatException e )
141 {
142 }
143 }
144 } );
145
146 builder.addFixed( new JLabel( "Burst Delay" ) );
147 builder.addRelatedGap();
148
149 builder.addFixed( delayField );
150 builder.addRelatedGap();
151
152 durationField = new JTextField( 4 );
153 UISupport.setPreferredHeight( durationField, 18 );
154 durationField.setHorizontalAlignment( JTextField.RIGHT );
155 durationField.setText( String.valueOf( burstDuration / 1000 ) );
156 durationField.setToolTipText( "Specifies the duration of a burst run in seconds" );
157 durationField.getDocument().addDocumentListener( new DocumentListenerAdapter()
158 {
159
160 public void update( Document doc )
161 {
162 try
163 {
164 burstDuration = Integer.parseInt( durationField.getText() ) * 1000;
165 notifyConfigurationChanged();
166 }
167 catch( NumberFormatException e )
168 {
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 {
187 XmlObjectConfigurationBuilder builder = new XmlObjectConfigurationBuilder();
188 builder.add( BURST_DELAY_ELEMENT, burstDelay );
189 builder.add( BURST_DURATION_ELEMENT, burstDuration );
190 return builder.finish();
191 }
192
193 @Override
194 public boolean allowThreadCountChangeDuringRun()
195 {
196 return false;
197 }
198
199 /***
200 * Factory for BurstLoadStrategy class
201 *
202 * @author Ole.Matzura
203 */
204
205 public static class Factory implements LoadStrategyFactory
206 {
207 public String getType()
208 {
209 return STRATEGY_TYPE;
210 }
211
212 public LoadStrategy build( XmlObject config, WsdlLoadTest loadTest )
213 {
214 return new BurstLoadStrategy( config, loadTest );
215 }
216
217 public LoadStrategy create( WsdlLoadTest loadTest )
218 {
219 return new BurstLoadStrategy( loadTest );
220 }
221 }
222 }