1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.impl.wsdl.loadtest.strategy;
14
15
16 import javax.swing.JComponent;
17 import javax.swing.JLabel;
18 import javax.swing.JPanel;
19 import javax.swing.JTextField;
20 import javax.swing.text.Document;
21
22 import org.apache.xmlbeans.XmlObject;
23
24 import com.eviware.soapui.SoapUI;
25 import com.eviware.soapui.impl.wsdl.loadtest.WsdlLoadTest;
26 import com.eviware.soapui.model.testsuite.LoadTestRunContext;
27 import com.eviware.soapui.model.testsuite.LoadTestRunner;
28 import com.eviware.soapui.model.testsuite.TestRunContext;
29 import com.eviware.soapui.model.testsuite.TestRunner;
30 import com.eviware.soapui.support.DocumentListenerAdapter;
31 import com.eviware.soapui.support.UISupport;
32 import com.eviware.soapui.support.xml.XmlObjectConfigurationBuilder;
33 import com.eviware.soapui.support.xml.XmlObjectConfigurationReader;
34 import com.jgoodies.forms.builder.ButtonBarBuilder;
35
36 /***
37 * LoadStrategy allowing maximum runs and request delays
38 *
39 * @author Ole.Matzura
40 */
41
42 public class SimpleLoadStrategy extends AbstractLoadStrategy
43 {
44 private static final int DEFAULT_TEST_DELAY = 1000;
45 private static final float DEFAULT_RANDOM_FACTOR = 0.5F;
46 public static final String STRATEGY_TYPE = "Simple";
47
48 private int testDelay = DEFAULT_TEST_DELAY;
49 private float randomFactor = DEFAULT_RANDOM_FACTOR;
50
51 private JPanel configPanel;
52 private JTextField testDelayField;
53 private JTextField randomFactorField;
54
55 public SimpleLoadStrategy(XmlObject config, WsdlLoadTest loadTest)
56 {
57 super( STRATEGY_TYPE, loadTest );
58
59 if( config != null )
60 {
61 XmlObjectConfigurationReader reader = new XmlObjectConfigurationReader( config );
62 testDelay = reader.readInt( "testDelay", DEFAULT_TEST_DELAY );
63 randomFactor = reader.readFloat( "randomFactor", DEFAULT_RANDOM_FACTOR );
64 }
65 }
66
67 public XmlObject getConfig()
68 {
69 XmlObjectConfigurationBuilder builder = new XmlObjectConfigurationBuilder();
70 builder.add( "testDelay", testDelay );
71 builder.add( "randomFactor", randomFactor );
72 return builder.finish();
73 }
74
75 public void beforeTestCase( LoadTestRunner loadTestRunner, LoadTestRunContext context, TestRunner testRunner, TestRunContext runContext)
76 {
77 int delay = calculateDelay( testDelay);
78 if( delay == 0 ) return;
79 try
80 {
81 Thread.sleep( delay );
82 }
83 catch (InterruptedException e)
84 {
85 SoapUI.logError( e );
86 }
87 }
88
89 public int calculateDelay( int delay )
90 {
91 if( delay == 0 || randomFactor == 0 )
92 return delay;
93
94 int fixDelay = (int) ((float)delay*(1-randomFactor));
95 int randDelay = (int) (randomFactor == 0 ? 0 : (float)(delay-fixDelay)*Math.random());
96 return fixDelay + randDelay;
97 }
98
99 public JComponent getConfigurationPanel()
100 {
101 if( configPanel == null )
102 {
103 ButtonBarBuilder builder = new ButtonBarBuilder();
104
105 testDelayField = new JTextField( 5 );
106 UISupport.setPreferredHeight( testDelayField, 18 );
107 testDelayField.setHorizontalAlignment( JTextField.RIGHT );
108 testDelayField.setText( String.valueOf( testDelay ));
109 testDelayField.setToolTipText( "Sets the delay between each test run in milliseconds" );
110 testDelayField.getDocument().addDocumentListener( new ConfigDocumentListener());
111
112 builder.addFixed( new JLabel( "Test Delay" ));
113 builder.addRelatedGap();
114
115 builder.addFixed( testDelayField );
116 builder.addRelatedGap();
117
118 randomFactorField = new JTextField( 4 );
119 UISupport.setPreferredHeight( randomFactorField, 18 );
120 randomFactorField.setHorizontalAlignment( JTextField.RIGHT );
121 randomFactorField.setText( String.valueOf( randomFactor ));
122 randomFactorField.setToolTipText( "Specifies the relative amount of randomization for delay (0 = no random, 1 = all random)" );
123 randomFactorField.getDocument().addDocumentListener( new ConfigDocumentListener());
124
125 builder.addFixed( new JLabel( "Random" ));
126 builder.addRelatedGap();
127 builder.addFixed( randomFactorField);
128
129 configPanel = builder.getPanel();
130 }
131
132 return configPanel;
133 }
134
135 private final class ConfigDocumentListener extends DocumentListenerAdapter
136 {
137 public void update(Document document)
138 {
139 try
140 {
141 if (document == testDelayField.getDocument())
142 testDelay = Integer.parseInt(testDelayField.getText());
143 if (document == randomFactorField.getDocument())
144 randomFactor = Float.parseFloat(randomFactorField.getText().replace( ',', '.' ));
145
146 notifyConfigurationChanged();
147 }
148 catch (NumberFormatException e)
149 {
150 }
151 }
152 }
153
154 public int getTestDelay()
155 {
156 return testDelay;
157 }
158
159 public float getRandomFactor()
160 {
161 return randomFactor;
162 }
163
164 /***
165 * Factory for SimpleLoadStrategy class
166 *
167 * @author Ole.Matzura
168 */
169
170 public static class Factory implements LoadStrategyFactory
171 {
172 public String getType()
173 {
174 return STRATEGY_TYPE;
175 }
176
177 public LoadStrategy build(XmlObject config, WsdlLoadTest loadTest)
178 {
179 return new SimpleLoadStrategy( config, loadTest );
180 }
181
182 public LoadStrategy create(WsdlLoadTest loadTest)
183 {
184 return new SimpleLoadStrategy( null, loadTest );
185 }
186 }
187 }