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