View Javadoc

    1   /*
    2    *  soapUI, copyright (C) 2004-2007 eviware.com
    3    *
    4    *  soapUI is free software; you can redistribute it and/or modify it under the
    5    *  terms of version 2.1 of the GNU Lesser General Public License as published by
    6    *  the Free Software Foundation.
    7    *
    8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
    9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    10   *  See the GNU Lesser General Public License for more details at gnu.org.
    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.TestRunContext;
    26  import com.eviware.soapui.model.testsuite.TestRunner;
    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, TestRunner testRunner, TestRunContext runContext)
    87  	{
    88  		WsdlLoadTest loadTest = (WsdlLoadTest) loadTestRunner.getLoadTest();
    89  		LoadTestStatistics statisticsModel = loadTest.getStatisticsModel();
    90
    91  		TestStep step = result.getTestStep();
    92  		if( targetStepMatches( step ))
    93  		{
    94  			int index = step.getTestCase().getIndexOfTestStep( step );
    95
    96  			long average = statisticsModel.getStatistic( index, Statistic.AVERAGE );
    97  			long count = statisticsModel.getStatistic( index, Statistic.AVERAGE );
    98  			if( count > minRequests && (count % sampleInterval == 0) && average >= maxAverage )
    99  			{
    100 				return returnErrorOrFail( "Average [" + average + "] exceeds limit [" + maxAverage + "]", maxErrors,
    101 						loadTestRunner, context );
    102 			}
    103 		}
    104 		else if( ALL_TEST_STEPS.equals( getTargetStep() ) )
    105 		{
    106 			long average = statisticsModel.getStatistic( LoadTestStatistics.TOTAL, Statistic.AVERAGE );
    107 			long count = statisticsModel.getStatistic( LoadTestStatistics.TOTAL, Statistic.COUNT );
    108 			if( count > minRequests && (count % sampleInterval == 0) && average >= maxAverage )
    109 			{
    110 				return returnErrorOrFail( "Average [" + average + "] exceeds limit [" + maxAverage + "]", maxErrors,
    111 						loadTestRunner, context );
    112 			}
    113 		}
    114
    115 		return null;
    116 	}
    117
    118 	public String assertResults(LoadTestRunner loadTestRunner, LoadTestRunContext context,TestRunner testRunner, TestRunContext runContext)
    119 	{
    120 		return null;
    121 	}
    122
    123 	public String getDescription()
    124 	{
    125 		return "testStep: " + getTargetStep() + ", minRequests: " + minRequests + ", maxAverage: " + maxAverage +
    126 				", maxErrors: " + maxErrors + ", sampleInterval: " + sampleInterval;
    127 	}
    128
    129 	public boolean configure()
    130 	{
    131 		if( dialog == null )
    132 		{
    133 			buildDialog();
    134 		}
    135
    136 		StringToStringMap values = new StringToStringMap();
    137
    138 		values.put( NAME_FIELD, getName() );
    139 		values.put( MINIMUM_REQUESTS_FIELD, String.valueOf( minRequests ));
    140 		values.put( MAX_AVERAGE_FIELD, String.valueOf( maxAverage ));
    141 		values.put( TEST_STEP_FIELD, getTargetStep() );
    142 		values.put( MAX_ERRORS_FIELD, String.valueOf( maxErrors ));
    143 		values.put( SAMPLE_INTERVAL_FIELD, String.valueOf( sampleInterval ));
    144
    145 		dialog.setOptions( TEST_STEP_FIELD, getTargetStepOptions( true ) );
    146 		values = dialog.show( values );
    147
    148 		if( dialog.getReturnValue() == XFormDialog.OK_OPTION )
    149 		{
    150 			try
    151 			{
    152 				minRequests = Integer.parseInt( values.get( MINIMUM_REQUESTS_FIELD ));
    153 				maxAverage = Integer.parseInt( values.get( MAX_AVERAGE_FIELD ));
    154 				maxErrors = Integer.parseInt( values.get( MAX_ERRORS_FIELD ));
    155 				sampleInterval = Integer.parseInt( values.get( SAMPLE_INTERVAL_FIELD ));
    156 				setName( values.get( NAME_FIELD ));
    157 				setTargetStep( values.get( TEST_STEP_FIELD ));
    158 			}
    159 			catch( Exception e )
    160 			{
    161 				UISupport.showErrorMessage( e.getMessage() );
    162 			}
    163
    164 			updateConfiguration();
    165
    166 			return true;
    167 		}
    168
    169 		return false;
    170 	}
    171
    172 	protected void updateConfiguration()
    173 	{
    174 		XmlObjectConfigurationBuilder builder = new XmlObjectConfigurationBuilder();
    175
    176 		builder.add( NAME_ELEMENT, getName() );
    177 		builder.add( MIN_REQUESTS_ELEMENT, minRequests );
    178 		builder.add( MAX_AVERAGE_ELEMENT, maxAverage );
    179 		builder.add( TEST_STEP_ELEMENT, getTargetStep() );
    180 		builder.add( MAX_ERRORS_ELEMENT, maxErrors );
    181 		builder.add( SAMPLE_INTERVAL_ELEMENT, sampleInterval );
    182
    183 		setConfiguration( builder.finish() );
    184 	}
    185
    186 	private void buildDialog()
    187 	{
    188 		XFormDialogBuilder builder = XFormFactory.createDialogBuilder( "TestStep Average Assertion" );
    189 	   XForm form = builder.createForm( "Basic" );
    190
    191 	   form.addTextField( NAME_FIELD, "Name of this assertion", FieldType.TEXT );
    192 	   form.addTextField( MINIMUM_REQUESTS_FIELD, "Minimum number of steps before asserting", FieldType.TEXT );
    193 	   form.addTextField( MAX_AVERAGE_FIELD, "Maximum allowed average step time", FieldType.TEXT );
    194 	   form.addTextField( MAX_ERRORS_FIELD, "Maximum number of allowed errors before failing loadtest (-1 = unlimited)", FieldType.TEXT );
    195 	   form.addTextField( SAMPLE_INTERVAL_FIELD, "Step count interval between sampling", FieldType.TEXT );
    196 	   form.addComboBox( TEST_STEP_FIELD, new String[0], "TestStep to assert" );
    197
    198 	   dialog = builder.buildDialog( builder.buildOkCancelHelpActions( HelpUrls.STEP_AVERAGE_LOAD_TEST_ASSERTION_HELP_URL),
    199 			"Specify options for this TestStep Average Assertion", UISupport.OPTIONS_ICON );
    200 	}
    201 }