View Javadoc

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