View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2009 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  import javax.swing.JComponent;
16  import javax.swing.JLabel;
17  import javax.swing.JPanel;
18  import javax.swing.JTextField;
19  import javax.swing.text.Document;
20  
21  import org.apache.xmlbeans.XmlObject;
22  
23  import com.eviware.soapui.SoapUI;
24  import com.eviware.soapui.impl.wsdl.loadtest.WsdlLoadTest;
25  import com.eviware.soapui.model.testsuite.LoadTestRunContext;
26  import com.eviware.soapui.model.testsuite.LoadTestRunner;
27  import com.eviware.soapui.model.testsuite.TestCaseRunContext;
28  import com.eviware.soapui.model.testsuite.TestCaseRunner;
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  	private static final float DEFAULT_RANDOM_FACTOR = 0.5F;
45  	public static final String STRATEGY_TYPE = "Simple";
46  
47  	private int testDelay = DEFAULT_TEST_DELAY;
48  	private float randomFactor = DEFAULT_RANDOM_FACTOR;
49  
50  	private JPanel configPanel;
51  	private JTextField testDelayField;
52  	private JTextField randomFactorField;
53  
54  	public SimpleLoadStrategy( XmlObject config, WsdlLoadTest loadTest )
55  	{
56  		super( STRATEGY_TYPE, loadTest );
57  
58  		if( config != null )
59  		{
60  			XmlObjectConfigurationReader reader = new XmlObjectConfigurationReader( config );
61  			testDelay = reader.readInt( "testDelay", DEFAULT_TEST_DELAY );
62  			randomFactor = reader.readFloat( "randomFactor", DEFAULT_RANDOM_FACTOR );
63  		}
64  	}
65  
66  	public XmlObject getConfig()
67  	{
68  		XmlObjectConfigurationBuilder builder = new XmlObjectConfigurationBuilder();
69  		builder.add( "testDelay", testDelay );
70  		builder.add( "randomFactor", randomFactor );
71  		return builder.finish();
72  	}
73  
74  	public void beforeTestCase( LoadTestRunner loadTestRunner, LoadTestRunContext context, TestCaseRunner testRunner,
75  			TestCaseRunContext runContext )
76  	{
77  		int delay = calculateDelay( testDelay );
78  		if( delay == 0 )
79  			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
124 					.setToolTipText( "Specifies the relative amount of randomization for delay (0 = no random, 1 = all random)" );
125 			randomFactorField.getDocument().addDocumentListener( new ConfigDocumentListener() );
126 
127 			builder.addFixed( new JLabel( "Random" ) );
128 			builder.addRelatedGap();
129 			builder.addFixed( randomFactorField );
130 
131 			configPanel = builder.getPanel();
132 		}
133 
134 		return configPanel;
135 	}
136 
137 	private final class ConfigDocumentListener extends DocumentListenerAdapter
138 	{
139 		public void update( Document document )
140 		{
141 			try
142 			{
143 				if( document == testDelayField.getDocument() )
144 					testDelay = Integer.parseInt( testDelayField.getText() );
145 				if( document == randomFactorField.getDocument() )
146 					randomFactor = Float.parseFloat( randomFactorField.getText().replace( ',', '.' ) );
147 
148 				notifyConfigurationChanged();
149 			}
150 			catch( NumberFormatException e )
151 			{
152 			}
153 		}
154 	}
155 
156 	public int getTestDelay()
157 	{
158 		return testDelay;
159 	}
160 
161 	public float getRandomFactor()
162 	{
163 		return randomFactor;
164 	}
165 
166 	/***
167 	 * Factory for SimpleLoadStrategy class
168 	 * 
169 	 * @author Ole.Matzura
170 	 */
171 
172 	public static class Factory implements LoadStrategyFactory
173 	{
174 		public String getType()
175 		{
176 			return STRATEGY_TYPE;
177 		}
178 
179 		public LoadStrategy build( XmlObject config, WsdlLoadTest loadTest )
180 		{
181 			return new SimpleLoadStrategy( config, loadTest );
182 		}
183 
184 		public LoadStrategy create( WsdlLoadTest loadTest )
185 		{
186 			return new SimpleLoadStrategy( null, loadTest );
187 		}
188 	}
189 }