View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 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.impl.wsdl.loadtest.WsdlLoadTestRunner;
26  import com.eviware.soapui.model.testsuite.LoadTestRunContext;
27  import com.eviware.soapui.model.testsuite.LoadTestRunner;
28  import com.eviware.soapui.model.testsuite.LoadTestRunner.Status;
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   * Burst LoadStrategy that pauses for a certain amount of time
37   * 
38   * @author Ole.Matzura
39   */
40  
41  public class BurstLoadStrategy extends AbstractLoadStrategy
42  {
43  	private static final String BURST_DURATION_ELEMENT = "burstDuration";
44  	private static final String BURST_DELAY_ELEMENT = "burstDelay";
45  	private static final int DEFAULT_BURST_DURATION = 10000;
46  	private static final int DEFAULT_BURST_DELAY = 60000;
47  	private static final int SLEEP_DELAY = 500;
48  	public static final String STRATEGY_TYPE = "Burst";
49  	private JPanel configPanel;	
50  	
51  	private int burstDelay = DEFAULT_BURST_DELAY;
52  	private int burstDuration = DEFAULT_BURST_DURATION;
53  	private long startTime;
54  	private JTextField delayField;
55  	private JTextField durationField;
56  	private JLabel infoLabel;
57  
58  	public BurstLoadStrategy( WsdlLoadTest loadTest )
59  	{
60  		super( STRATEGY_TYPE, loadTest );
61  		
62  		burstDelay = DEFAULT_BURST_DELAY;
63  		burstDuration = DEFAULT_BURST_DURATION;
64  	}
65  
66  	public BurstLoadStrategy(XmlObject config, WsdlLoadTest loadTest)
67  	{
68  		super( STRATEGY_TYPE, loadTest );
69  		
70  		XmlObjectConfigurationReader reader = new XmlObjectConfigurationReader( config );
71  		burstDelay = reader.readInt( BURST_DELAY_ELEMENT, DEFAULT_BURST_DELAY );
72  		burstDuration = reader.readInt( BURST_DURATION_ELEMENT, DEFAULT_BURST_DURATION );
73  	}
74  
75  	public void beforeLoadTest(LoadTestRunner loadTestRunner, LoadTestRunContext context)
76  	{
77  		startTime = System.currentTimeMillis();
78  		if( infoLabel != null )
79  			infoLabel.setText( "starting.." );
80  	}
81  
82  	public void recalculate( LoadTestRunner loadTestRunner, LoadTestRunContext context )
83  	{
84  		// get time passed since start of test
85  		long timePassed = System.currentTimeMillis() - startTime;
86  		
87  		// wait during burstDelay period
88  		long mod = timePassed % (burstDelay + burstDuration);
89  		while( (mod < burstDelay) && (loadTestRunner.getStatus() == Status.RUNNING || loadTestRunner.getStatus() == Status.INITIALIZED ))
90  		{
91  			try
92  			{
93  				String label = (burstDelay-mod)/1000 + "s delay left";
94  				if( infoLabel != null && !infoLabel.getText().equals( label ))
95  					infoLabel.setText( label );
96  				
97  				Thread.sleep( SLEEP_DELAY );
98  				if( ((WsdlLoadTestRunner)loadTestRunner).getProgress() >= 1 && infoLabel != null )
99  				{
100 					infoLabel.setText( "" );
101 					return;
102 				}
103 				
104 				timePassed = System.currentTimeMillis() - startTime;
105 				mod = timePassed % (burstDelay + burstDuration);
106 			}
107 			catch (InterruptedException e)
108 			{
109 				SoapUI.logError( e );
110 			}
111 		}
112 		
113 		if( loadTestRunner.getStatus() == Status.RUNNING )
114 		{
115 			String label = ((burstDelay + burstDuration) - mod)/1000 + "s burst left";
116 			if( infoLabel != null && !infoLabel.getText().equals( label ))
117 				infoLabel.setText( label );
118 		}
119 	}
120 
121 	public void afterLoadTest(LoadTestRunner loadTestRunner, LoadTestRunContext context)
122 	{
123 		if( infoLabel != null )
124 			infoLabel.setText( "" );
125 	}
126 
127 	public JComponent getConfigurationPanel()
128 	{
129 		if( configPanel == null )
130 		{
131 			ButtonBarBuilder builder = new ButtonBarBuilder();
132 			
133 			infoLabel = new JLabel();
134 			delayField = new JTextField( 4 );
135 			UISupport.setPreferredHeight( delayField, 18 );
136 			delayField.setHorizontalAlignment( JTextField.RIGHT );
137 			delayField.setText( String.valueOf( burstDelay/1000 ));
138 			delayField.setToolTipText( "Sets the delay before each burst run in seconds" );
139 			delayField.getDocument().addDocumentListener( new DocumentListenerAdapter(){
140 
141 				public void update( Document doc )
142 				{
143 					try
144 					{
145 						burstDelay = Integer.parseInt(delayField.getText())*1000;
146 						notifyConfigurationChanged();
147 					}
148 					catch (NumberFormatException e)
149 					{
150 					}
151 				}}
152 				);
153 			
154 			builder.addFixed( new JLabel( "Burst Delay" ));
155 			builder.addRelatedGap();
156 			
157 			builder.addFixed( delayField );
158 			builder.addRelatedGap();
159 
160 			durationField = new JTextField(4);
161 			UISupport.setPreferredHeight( durationField, 18 );
162 			durationField.setHorizontalAlignment( JTextField.RIGHT );
163 			durationField.setText( String.valueOf( burstDuration/1000 ));
164 			durationField.setToolTipText( "Specifies the duration of a burst run in seconds" );
165 			durationField.getDocument().addDocumentListener( new DocumentListenerAdapter(){
166 
167 				public void update( Document doc )
168 				{
169 					try
170 					{
171 						burstDuration = Integer.parseInt(durationField.getText())*1000;
172 						notifyConfigurationChanged();
173 					}
174 					catch (NumberFormatException e)
175 					{
176 					}
177 				}}
178 				);
179 			
180 			builder.addFixed( new JLabel( "Burst Duration" ));
181 			builder.addRelatedGap();
182 			builder.addFixed( durationField);
183 			builder.addRelatedGap();
184 			builder.addFixed( infoLabel );
185 			
186 			configPanel = builder.getPanel();
187 		}
188 
189 		return configPanel;
190 	}
191 
192 	public XmlObject getConfig()
193 	{
194 		XmlObjectConfigurationBuilder builder = new XmlObjectConfigurationBuilder();
195 		builder.add( BURST_DELAY_ELEMENT, burstDelay );
196       builder.add( BURST_DURATION_ELEMENT, burstDuration );
197       return builder.finish();
198 	}
199 	
200 	/***
201 	 * Factory for BurstLoadStrategy class
202 	 * 
203 	 * @author Ole.Matzura
204 	 */
205 
206 	public static class Factory implements LoadStrategyFactory
207 	{
208 		public String getType()
209 		{
210 			return STRATEGY_TYPE;
211 		}
212 
213 		public LoadStrategy build(XmlObject config, WsdlLoadTest loadTest)
214 		{
215 			return new BurstLoadStrategy( config, loadTest );
216 		}
217 
218 		public LoadStrategy create(WsdlLoadTest loadTest)
219 		{
220 			return new BurstLoadStrategy( loadTest );
221 		}
222 	}
223 }