001: /*
002: * soapUI, copyright (C) 2004-2007 eviware.com
003: *
004: * soapUI is free software; you can redistribute it and/or modify it under the
005: * terms of version 2.1 of the GNU Lesser General Public License as published by
006: * the Free Software Foundation.
007: *
008: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: * See the GNU Lesser General Public License for more details at gnu.org.
011: */
012:
013: package com.eviware.soapui.impl.wsdl.loadtest.assertions;
014:
015: import org.apache.xmlbeans.XmlObject;
016:
017: import com.eviware.soapui.config.LoadTestAssertionConfig;
018: import com.eviware.soapui.impl.wsdl.loadtest.WsdlLoadTest;
019: import com.eviware.soapui.impl.wsdl.loadtest.data.LoadTestStatistics;
020: import com.eviware.soapui.impl.wsdl.loadtest.data.LoadTestStatistics.Statistic;
021: import com.eviware.soapui.impl.wsdl.support.Configurable;
022: import com.eviware.soapui.impl.wsdl.support.HelpUrls;
023: import com.eviware.soapui.model.testsuite.LoadTestRunContext;
024: import com.eviware.soapui.model.testsuite.LoadTestRunner;
025: import com.eviware.soapui.model.testsuite.TestRunContext;
026: import com.eviware.soapui.model.testsuite.TestRunner;
027: import com.eviware.soapui.model.testsuite.TestStep;
028: import com.eviware.soapui.model.testsuite.TestStepResult;
029: import com.eviware.soapui.support.UISupport;
030: import com.eviware.soapui.support.types.StringToStringMap;
031: import com.eviware.soapui.support.xml.XmlObjectConfigurationBuilder;
032: import com.eviware.soapui.support.xml.XmlObjectConfigurationReader;
033: import com.eviware.x.form.XForm;
034: import com.eviware.x.form.XFormDialog;
035: import com.eviware.x.form.XFormDialogBuilder;
036: import com.eviware.x.form.XFormFactory;
037: import com.eviware.x.form.XForm.FieldType;
038:
039: /**
040: * LoadTestAssertion for asserting the average step time
041: *
042: * @author Ole.Matzura
043: */
044:
045: public class TestStepAverageAssertion extends AbstractLoadTestAssertion
046: implements Configurable {
047: private static final String NAME_FIELD = "Name";
048: private static final String NAME_ELEMENT = "name";
049: private static final String SAMPLE_INTERVAL_ELEMENT = "sample-interval";
050: private static final String SAMPLE_INTERVAL_FIELD = "Sample Interval";
051: private static final String MAX_AVERAGE_ELEMENT = "max-average";
052: private static final String MAX_ERRORS_ELEMENT = "max-errors";
053: private static final String MAX_ERRORS_FIELD = "Max Errors";
054: private static final String MIN_REQUESTS_ELEMENT = "min-requests";
055: private static final String MAX_AVERAGE_FIELD = "Max Average";
056: private static final String MINIMUM_REQUESTS_FIELD = "Minimum Requests";
057:
058: private int minRequests;
059: private int maxAverage;
060: private int maxErrors;
061: private int sampleInterval;
062: private XFormDialog dialog;
063: public static final String STEP_AVERAGE_TYPE = "Step Average";
064:
065: public TestStepAverageAssertion(
066: LoadTestAssertionConfig assertionConfig,
067: WsdlLoadTest loadTest) {
068: super (assertionConfig, loadTest);
069:
070: init(assertionConfig);
071: initIcon("/average_loadtest_assertion.gif");
072: }
073:
074: private void init(LoadTestAssertionConfig assertionConfig) {
075: XmlObject configuration = assertionConfig.getConfiguration();
076:
077: XmlObjectConfigurationReader reader = new XmlObjectConfigurationReader(
078: configuration);
079: setName(reader.readString(NAME_ELEMENT, "Step Average"));
080: minRequests = reader.readInt(MIN_REQUESTS_ELEMENT, 100);
081: maxAverage = reader.readInt(MAX_AVERAGE_ELEMENT, 1000);
082: setTargetStep(reader.readString(TEST_STEP_ELEMENT,
083: ANY_TEST_STEP));
084: maxErrors = reader.readInt(MAX_ERRORS_ELEMENT, -1);
085: sampleInterval = reader.readInt(SAMPLE_INTERVAL_ELEMENT, 20);
086: }
087:
088: public String assertResult(LoadTestRunner loadTestRunner,
089: LoadTestRunContext context, TestStepResult result,
090: TestRunner testRunner, TestRunContext runContext) {
091: WsdlLoadTest loadTest = (WsdlLoadTest) loadTestRunner
092: .getLoadTest();
093: LoadTestStatistics statisticsModel = loadTest
094: .getStatisticsModel();
095:
096: TestStep step = result.getTestStep();
097: if (targetStepMatches(step)) {
098: int index = step.getTestCase().getIndexOfTestStep(step);
099:
100: long average = statisticsModel.getStatistic(index,
101: Statistic.AVERAGE);
102: long count = statisticsModel.getStatistic(index,
103: Statistic.AVERAGE);
104: if (count > minRequests && (count % sampleInterval == 0)
105: && average >= maxAverage) {
106: return returnErrorOrFail("Average [" + average
107: + "] exceeds limit [" + maxAverage + "]",
108: maxErrors, loadTestRunner, context);
109: }
110: } else if (ALL_TEST_STEPS.equals(getTargetStep())) {
111: long average = statisticsModel.getStatistic(
112: LoadTestStatistics.TOTAL, Statistic.AVERAGE);
113: long count = statisticsModel.getStatistic(
114: LoadTestStatistics.TOTAL, Statistic.COUNT);
115: if (count > minRequests && (count % sampleInterval == 0)
116: && average >= maxAverage) {
117: return returnErrorOrFail("Average [" + average
118: + "] exceeds limit [" + maxAverage + "]",
119: maxErrors, loadTestRunner, context);
120: }
121: }
122:
123: return null;
124: }
125:
126: public String assertResults(LoadTestRunner loadTestRunner,
127: LoadTestRunContext context, TestRunner testRunner,
128: TestRunContext runContext) {
129: return null;
130: }
131:
132: public String getDescription() {
133: return "testStep: " + getTargetStep() + ", minRequests: "
134: + minRequests + ", maxAverage: " + maxAverage
135: + ", maxErrors: " + maxErrors + ", sampleInterval: "
136: + sampleInterval;
137: }
138:
139: public boolean configure() {
140: if (dialog == null) {
141: buildDialog();
142: }
143:
144: StringToStringMap values = new StringToStringMap();
145:
146: values.put(NAME_FIELD, getName());
147: values.put(MINIMUM_REQUESTS_FIELD, String.valueOf(minRequests));
148: values.put(MAX_AVERAGE_FIELD, String.valueOf(maxAverage));
149: values.put(TEST_STEP_FIELD, getTargetStep());
150: values.put(MAX_ERRORS_FIELD, String.valueOf(maxErrors));
151: values.put(SAMPLE_INTERVAL_FIELD, String
152: .valueOf(sampleInterval));
153:
154: dialog.setOptions(TEST_STEP_FIELD, getTargetStepOptions(true));
155: values = dialog.show(values);
156:
157: if (dialog.getReturnValue() == XFormDialog.OK_OPTION) {
158: try {
159: minRequests = Integer.parseInt(values
160: .get(MINIMUM_REQUESTS_FIELD));
161: maxAverage = Integer.parseInt(values
162: .get(MAX_AVERAGE_FIELD));
163: maxErrors = Integer.parseInt(values
164: .get(MAX_ERRORS_FIELD));
165: sampleInterval = Integer.parseInt(values
166: .get(SAMPLE_INTERVAL_FIELD));
167: setName(values.get(NAME_FIELD));
168: setTargetStep(values.get(TEST_STEP_FIELD));
169: } catch (Exception e) {
170: UISupport.showErrorMessage(e.getMessage());
171: }
172:
173: updateConfiguration();
174:
175: return true;
176: }
177:
178: return false;
179: }
180:
181: protected void updateConfiguration() {
182: XmlObjectConfigurationBuilder builder = new XmlObjectConfigurationBuilder();
183:
184: builder.add(NAME_ELEMENT, getName());
185: builder.add(MIN_REQUESTS_ELEMENT, minRequests);
186: builder.add(MAX_AVERAGE_ELEMENT, maxAverage);
187: builder.add(TEST_STEP_ELEMENT, getTargetStep());
188: builder.add(MAX_ERRORS_ELEMENT, maxErrors);
189: builder.add(SAMPLE_INTERVAL_ELEMENT, sampleInterval);
190:
191: setConfiguration(builder.finish());
192: }
193:
194: private void buildDialog() {
195: XFormDialogBuilder builder = XFormFactory
196: .createDialogBuilder("TestStep Average Assertion");
197: XForm form = builder.createForm("Basic");
198:
199: form.addTextField(NAME_FIELD, "Name of this assertion",
200: FieldType.TEXT);
201: form.addTextField(MINIMUM_REQUESTS_FIELD,
202: "Minimum number of steps before asserting",
203: FieldType.TEXT);
204: form.addTextField(MAX_AVERAGE_FIELD,
205: "Maximum allowed average step time", FieldType.TEXT);
206: form
207: .addTextField(
208: MAX_ERRORS_FIELD,
209: "Maximum number of allowed errors before failing loadtest (-1 = unlimited)",
210: FieldType.TEXT);
211: form.addTextField(SAMPLE_INTERVAL_FIELD,
212: "Step count interval between sampling", FieldType.TEXT);
213: form.addComboBox(TEST_STEP_FIELD, new String[0],
214: "TestStep to assert");
215:
216: dialog = builder
217: .buildDialog(
218: builder
219: .buildOkCancelHelpActions(HelpUrls.STEP_AVERAGE_LOAD_TEST_ASSERTION_HELP_URL),
220: "Specify options for this TestStep Average Assertion",
221: UISupport.OPTIONS_ICON);
222: }
223: }
|