1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.impl.wsdl.loadtest.assertions;
14
15 import org.apache.xmlbeans.XmlObject;
16
17 import com.eviware.soapui.config.LoadTestAssertionConfig;
18 import com.eviware.soapui.impl.wsdl.loadtest.WsdlLoadTest;
19 import com.eviware.soapui.impl.wsdl.loadtest.data.LoadTestStatistics;
20 import com.eviware.soapui.impl.wsdl.loadtest.data.LoadTestStatistics.Statistic;
21 import com.eviware.soapui.impl.wsdl.support.Configurable;
22 import com.eviware.soapui.impl.wsdl.support.HelpUrls;
23 import com.eviware.soapui.model.testsuite.LoadTestRunContext;
24 import com.eviware.soapui.model.testsuite.LoadTestRunner;
25 import com.eviware.soapui.model.testsuite.TestCaseRunContext;
26 import com.eviware.soapui.model.testsuite.TestCaseRunner;
27 import com.eviware.soapui.model.testsuite.TestStep;
28 import com.eviware.soapui.model.testsuite.TestStepResult;
29 import com.eviware.soapui.support.UISupport;
30 import com.eviware.soapui.support.types.StringToStringMap;
31 import com.eviware.soapui.support.xml.XmlObjectConfigurationBuilder;
32 import com.eviware.soapui.support.xml.XmlObjectConfigurationReader;
33 import com.eviware.x.form.XForm;
34 import com.eviware.x.form.XFormDialog;
35 import com.eviware.x.form.XFormDialogBuilder;
36 import com.eviware.x.form.XFormFactory;
37 import com.eviware.x.form.XForm.FieldType;
38
39 /***
40 * LoadTestAssertion for asserting the average step time
41 *
42 * @author Ole.Matzura
43 */
44
45 public class TestStepAverageAssertion extends AbstractLoadTestAssertion implements Configurable
46 {
47 private static final String NAME_FIELD = "Name";
48 private static final String NAME_ELEMENT = "name";
49 private static final String SAMPLE_INTERVAL_ELEMENT = "sample-interval";
50 private static final String SAMPLE_INTERVAL_FIELD = "Sample Interval";
51 private static final String MAX_AVERAGE_ELEMENT = "max-average";
52 private static final String MAX_ERRORS_ELEMENT = "max-errors";
53 private static final String MAX_ERRORS_FIELD = "Max Errors";
54 private static final String MIN_REQUESTS_ELEMENT = "min-requests";
55 private static final String MAX_AVERAGE_FIELD = "Max Average";
56 private static final String MINIMUM_REQUESTS_FIELD = "Minimum Requests";
57
58 private int minRequests;
59 private int maxAverage;
60 private int maxErrors;
61 private int sampleInterval;
62 private XFormDialog dialog;
63 public static final String STEP_AVERAGE_TYPE = "Step Average";
64
65 public TestStepAverageAssertion( LoadTestAssertionConfig assertionConfig, WsdlLoadTest loadTest )
66 {
67 super( assertionConfig, loadTest );
68
69 init( assertionConfig );
70 initIcon( "/average_loadtest_assertion.gif" );
71 }
72
73 private void init( LoadTestAssertionConfig assertionConfig )
74 {
75 XmlObject configuration = assertionConfig.getConfiguration();
76
77 XmlObjectConfigurationReader reader = new XmlObjectConfigurationReader( configuration );
78 setName( reader.readString( NAME_ELEMENT, "Step Average" ) );
79 minRequests = reader.readInt( MIN_REQUESTS_ELEMENT, 100 );
80 maxAverage = reader.readInt( MAX_AVERAGE_ELEMENT, 1000 );
81 setTargetStep( reader.readString( TEST_STEP_ELEMENT, ANY_TEST_STEP ) );
82 maxErrors = reader.readInt( MAX_ERRORS_ELEMENT, -1 );
83 sampleInterval = reader.readInt( SAMPLE_INTERVAL_ELEMENT, 20 );
84 }
85
86 public String assertResult( LoadTestRunner loadTestRunner, LoadTestRunContext context, TestStepResult result,
87 TestCaseRunner testRunner, TestCaseRunContext runContext )
88 {
89 WsdlLoadTest loadTest = ( WsdlLoadTest )loadTestRunner.getLoadTest();
90 LoadTestStatistics statisticsModel = loadTest.getStatisticsModel();
91
92 TestStep step = result.getTestStep();
93 if( targetStepMatches( step ) )
94 {
95 int index = step.getTestCase().getIndexOfTestStep( step );
96
97 long average = statisticsModel.getStatistic( index, Statistic.AVERAGE );
98 long count = statisticsModel.getStatistic( index, Statistic.AVERAGE );
99 if( count > minRequests && ( count % sampleInterval == 0 ) && average >= maxAverage )
100 {
101 return returnErrorOrFail( "Average [" + average + "] exceeds limit [" + maxAverage + "]", maxErrors,
102 loadTestRunner, context );
103 }
104 }
105 else if( ALL_TEST_STEPS.equals( getTargetStep() ) )
106 {
107 long average = statisticsModel.getStatistic( LoadTestStatistics.TOTAL, Statistic.AVERAGE );
108 long count = statisticsModel.getStatistic( LoadTestStatistics.TOTAL, Statistic.COUNT );
109 if( count > minRequests && ( count % sampleInterval == 0 ) && average >= maxAverage )
110 {
111 return returnErrorOrFail( "Average [" + average + "] exceeds limit [" + maxAverage + "]", maxErrors,
112 loadTestRunner, context );
113 }
114 }
115
116 return null;
117 }
118
119 public String assertResults( LoadTestRunner loadTestRunner, LoadTestRunContext context, TestCaseRunner testRunner,
120 TestCaseRunContext runContext )
121 {
122 return null;
123 }
124
125 public String getDescription()
126 {
127 return "testStep: " + getTargetStep() + ", minRequests: " + minRequests + ", maxAverage: " + maxAverage
128 + ", maxErrors: " + maxErrors + ", sampleInterval: " + sampleInterval;
129 }
130
131 public boolean configure()
132 {
133 if( dialog == null )
134 {
135 buildDialog();
136 }
137
138 StringToStringMap values = new StringToStringMap();
139
140 values.put( NAME_FIELD, getName() );
141 values.put( MINIMUM_REQUESTS_FIELD, String.valueOf( minRequests ) );
142 values.put( MAX_AVERAGE_FIELD, String.valueOf( maxAverage ) );
143 values.put( TEST_STEP_FIELD, getTargetStep() );
144 values.put( MAX_ERRORS_FIELD, String.valueOf( maxErrors ) );
145 values.put( SAMPLE_INTERVAL_FIELD, String.valueOf( sampleInterval ) );
146
147 dialog.setOptions( TEST_STEP_FIELD, getTargetStepOptions( true ) );
148 values = dialog.show( values );
149
150 if( dialog.getReturnValue() == XFormDialog.OK_OPTION )
151 {
152 try
153 {
154 minRequests = Integer.parseInt( values.get( MINIMUM_REQUESTS_FIELD ) );
155 maxAverage = Integer.parseInt( values.get( MAX_AVERAGE_FIELD ) );
156 maxErrors = Integer.parseInt( values.get( MAX_ERRORS_FIELD ) );
157 sampleInterval = Integer.parseInt( values.get( SAMPLE_INTERVAL_FIELD ) );
158 setName( values.get( NAME_FIELD ) );
159 setTargetStep( values.get( TEST_STEP_FIELD ) );
160 }
161 catch( Exception e )
162 {
163 UISupport.showErrorMessage( e.getMessage() );
164 }
165
166 updateConfiguration();
167
168 return true;
169 }
170
171 return false;
172 }
173
174 protected void updateConfiguration()
175 {
176 XmlObjectConfigurationBuilder builder = new XmlObjectConfigurationBuilder();
177
178 builder.add( NAME_ELEMENT, getName() );
179 builder.add( MIN_REQUESTS_ELEMENT, minRequests );
180 builder.add( MAX_AVERAGE_ELEMENT, maxAverage );
181 builder.add( TEST_STEP_ELEMENT, getTargetStep() );
182 builder.add( MAX_ERRORS_ELEMENT, maxErrors );
183 builder.add( SAMPLE_INTERVAL_ELEMENT, sampleInterval );
184
185 setConfiguration( builder.finish() );
186 }
187
188 private void buildDialog()
189 {
190 XFormDialogBuilder builder = XFormFactory.createDialogBuilder( "Step Average Assertion" );
191 XForm form = builder.createForm( "Basic" );
192
193 form.addTextField( NAME_FIELD, "Name of this assertion", FieldType.TEXT );
194 form.addTextField( MINIMUM_REQUESTS_FIELD, "Minimum number of steps before asserting", FieldType.TEXT );
195 form.addTextField( MAX_AVERAGE_FIELD, "Maximum allowed average step time", FieldType.TEXT );
196 form.addTextField( MAX_ERRORS_FIELD, "Maximum number of allowed errors before failing loadtest (-1 = unlimited)",
197 FieldType.TEXT );
198 form.addTextField( SAMPLE_INTERVAL_FIELD, "Step count interval between sampling", FieldType.TEXT );
199 form.addComboBox( TEST_STEP_FIELD, new String[0], "TestStep to assert" );
200
201 dialog = builder.buildDialog( builder
202 .buildOkCancelHelpActions( HelpUrls.STEP_AVERAGE_LOAD_TEST_ASSERTION_HELP_URL ),
203 "Specify options for this Step Average Assertion", UISupport.OPTIONS_ICON );
204 }
205 }