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