You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jmeter-dev@jakarta.apache.org by th...@apache.org on 2004/04/08 20:40:08 UTC

cvs commit: jakarta-jmeter/src/core/org/apache/jmeter/engine StandardJMeterEngine.java

thads       2004/04/08 11:40:08

  Modified:    src/core/org/apache/jmeter/threads JMeterThread.java
               src/core/org/apache/jmeter/engine StandardJMeterEngine.java
  Added:       src/components/org/apache/jmeter/sampler/gui
                        TestActionGui.java
               src/components/org/apache/jmeter/sampler TestAction.java
  Removed:     src/core/org/apache/jmeter/threads JMeterContext.java
               src/core/org/apache/jmeter/resources messages.properties
  Log:
  TestAction is a new test component (implemented as a sampler...for now)
  that gives the ability to pause or stop the current running thread
  or pause/stop the entire test.
  
  Revision  Changes    Path
  1.49      +13 -2     jakarta-jmeter/src/core/org/apache/jmeter/threads/JMeterThread.java
  
  Index: JMeterThread.java
  ===================================================================
  RCS file: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/threads/JMeterThread.java,v
  retrieving revision 1.48
  retrieving revision 1.49
  diff -u -r1.48 -r1.49
  --- JMeterThread.java	27 Feb 2004 00:17:06 -0000	1.48
  +++ JMeterThread.java	8 Apr 2004 18:40:08 -0000	1.49
  @@ -216,6 +216,7 @@
               threadContext = JMeterContextService.getContext();
               threadContext.setVariables(threadVars);
               threadContext.setThreadNum(getThreadNum());
  +            threadContext.setThread(this);
               testTree.traverse(compiler);
               running = true;
               //listeners = controller.getListeners();
  @@ -354,12 +355,21 @@
   		//engine.stopTest();
   		if (engine != null ) engine.askThreadsToStop();
   	}
  +
   	private void stopThread()
   	{
   		running = false;
   		log.info("Stop Thread detected by thread " + threadName);
   	}
   
  +	public void pauseThread(int milis)
  +	{
  +		try
  +		{
  +			Thread.sleep(milis);
  +		}
  +		catch (InterruptedException e) {}
  +	}
   
       private void checkAssertions(List assertions, SampleResult result)
       {
  @@ -493,6 +503,7 @@
               notifyTestListeners();
           }
       }
  +    
       /**
        * Save the engine instance for access to the stop methods
        * 
  
  
  
  1.1                  jakarta-jmeter/src/components/org/apache/jmeter/sampler/gui/TestActionGui.java
  
  Index: TestActionGui.java
  ===================================================================
  /*
   * Copyright 2003-2004 The Apache Software Foundation.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *   http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   * 
  */
  
  package org.apache.jmeter.sampler.gui;
  
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.awt.event.FocusEvent;
  import java.awt.event.FocusListener;
  
  import javax.swing.ButtonGroup;
  import javax.swing.DefaultComboBoxModel;
  import javax.swing.JComboBox;
  import javax.swing.JLabel;
  import javax.swing.JRadioButton;
  import javax.swing.JTextField;
  import javax.swing.event.ChangeEvent;
  import javax.swing.event.ChangeListener;
  
  import org.apache.jmeter.gui.util.HorizontalPanel;
  import org.apache.jmeter.sampler.TestAction;
  import org.apache.jmeter.samplers.gui.AbstractSamplerGui;
  import org.apache.jmeter.testelement.TestElement;
  import org.apache.jmeter.util.JMeterUtils;
  import org.apache.jorphan.gui.layout.VerticalLayout;
  
  /**
   * @version $Revision: 1.1 $
   */
  public class TestActionGui extends AbstractSamplerGui
  {
  	//Gui components
  	private JComboBox targetBox;
  //	private ButtonGroup actionButtons;
  	private JRadioButton pauseButton;
  	private JRadioButton stopButton;
  	private JTextField durationField;
  	
  	//State variables
  	private int target;
  	private int action;
  	private int duration;
  
  	//String in the panel
  	private static final String title = JMeterUtils.getResString("test_action_title");
  	private static final String targetLabel = JMeterUtils.getResString("test_action_target");
  	private static final String threadTarget = JMeterUtils.getResString("test_action_target_thread");
  	private static final String testTarget = JMeterUtils.getResString("test_action_target_test");
  	private static final String actionLabel = JMeterUtils.getResString("test_action_action");
  	private static final String pauseAction = JMeterUtils.getResString("test_action_pause");
  	private static final String stopAction = JMeterUtils.getResString("test_action_stop");
  	private static final String durationLabel = JMeterUtils.getResString("test_action_duration");
  	
      public TestActionGui()
      {
          super();
          target = TestAction.THREAD;
          action = TestAction.PAUSE;
          init();
      }
  
      /**
       * @see org.apache.jmeter.gui.JMeterGUIComponent#getStaticLabel()
       */
      public String getStaticLabel()
      {
          return title;
      }
      
      public void configure(TestElement element)
      {
          super.configure(element);
          TestAction ta = (TestAction)element;
          
          target=ta.getTarget();
          if (target == TestAction.THREAD)
          	targetBox.setSelectedItem(threadTarget);
          else 
  			targetBox.setSelectedItem(testTarget);
  
  		action = ta.getAction();		
  		if (action == TestAction.PAUSE)
  			pauseButton.setSelected(true);
  		else
  			stopButton.setSelected(true);
  		
  		duration=ta.getDuration();
  		durationField.setText(Integer.toString(duration));
      }
  
      /**
       * @see org.apache.jmeter.gui.JMeterGUIComponent#createTestElement()
       */
      public TestElement createTestElement()
      {
          TestAction ta = new TestAction();
          modifyTestElement(ta);
          return ta;
      }
  
      /**
       * Modifies a given TestElement to mirror the data in the gui components.
       * @see org.apache.jmeter.gui.JMeterGUIComponent#modifyTestElement(TestElement)
       */
      public void modifyTestElement(TestElement element)
      {
          super.configureTestElement(element);
  		TestAction ta = (TestAction)element;
  		ta.setAction(action);
  		ta.setTarget(target);
  		ta.setDuration(duration);
      }
      
      private void init()
      {
  		setLayout(
  			new VerticalLayout(5, VerticalLayout.LEFT, VerticalLayout.TOP));
  		setBorder(makeBorder());
  		add(makeTitlePanel());
  		
  		//Target
  		HorizontalPanel targetPanel = new HorizontalPanel();
  		targetPanel.add(new JLabel(targetLabel));
  		DefaultComboBoxModel targetModel = new DefaultComboBoxModel();
  		targetModel.addElement(threadTarget);
  		targetModel.addElement(testTarget);
  		targetBox = new JComboBox(targetModel);
  		targetBox.addActionListener(new ActionListener()
  		{
  			public void actionPerformed(ActionEvent e)
  			{
  				if (((String)targetBox.getSelectedItem())
  					.equals(threadTarget))
  				{
  					target = TestAction.THREAD;
  				}
  				else
  				{
  					target = TestAction.TEST;
  				}
  			}
  		});
  		targetPanel.add(targetBox);
  		add(targetPanel);
  		
  		//Action
  		HorizontalPanel actionPanel = new HorizontalPanel();
  		ButtonGroup actionButtons = new ButtonGroup();
  		pauseButton = new JRadioButton(pauseAction,true);
  		pauseButton.addChangeListener(new ChangeListener()
  		{
  			public void stateChanged(ChangeEvent e)
  			{
  				if(pauseButton.isSelected())
  				{
  					action = TestAction.PAUSE;
  					durationField.setEnabled(true);
  				}
  					
  			}
  		});
  		stopButton = new JRadioButton(stopAction,false);
  		stopButton.addChangeListener(new ChangeListener()
  		{
  			public void stateChanged(ChangeEvent e)
  			{
  				if(stopButton.isSelected())
  				{
  					action = TestAction.STOP;
  					durationField.setEnabled(false);
  				}
  			}
  		});
  		actionButtons.add(pauseButton);
  		actionButtons.add(stopButton);
  		actionPanel.add(new JLabel(actionLabel));
  		actionPanel.add(pauseButton);
  		actionPanel.add(stopButton);
  		add(actionPanel);
  
  		//Duration
  		HorizontalPanel durationPanel = new HorizontalPanel();
  		durationField = new JTextField(5);
  		durationField.setText(Integer.toString(duration));
  		durationField.addFocusListener(new FocusListener()
  		{
  			public void focusLost(FocusEvent e)
  			{
  				try 
  				{ 
  					duration = Integer.parseInt(durationField.getText());
  				}
  				catch (NumberFormatException nfe)
  				{
  					duration = 0;
  					//alert
  //					durationField.grabFocus();
  				}
  			}
  			
  			public void focusGained(FocusEvent e) {}
  		});
  		durationPanel.add(new JLabel("Duration"));
  		durationPanel.add(durationField);
  		add(durationPanel);
      }
      
  
  }
  
  
  
  1.1                  jakarta-jmeter/src/components/org/apache/jmeter/sampler/TestAction.java
  
  Index: TestAction.java
  ===================================================================
  /*
   * Copyright 2001-2004 The Apache Software Foundation.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *   http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   * 
  */
  package org.apache.jmeter.sampler;
  
  import org.apache.jmeter.samplers.AbstractSampler;
  import org.apache.jmeter.samplers.Entry;
  import org.apache.jmeter.samplers.SampleResult;
  import org.apache.jmeter.testelement.property.IntegerProperty;
  import org.apache.jmeter.threads.JMeterContext;
  import org.apache.jmeter.threads.JMeterContextService;
  import org.apache.jmeter.threads.JMeterThread;
  
  /**
   * @author Administrator
   *
   * To change the template for this generated type comment go to
   * Window>Preferences>Java>Code Generation>Code and Comments
   */
  public class TestAction
  	extends AbstractSampler 
  {
  	//Actions
  	public final static int STOP = 0;
  	public final static int PAUSE = 1;
  
  	//Action target
  	public final static int THREAD = 0;
  //	public final static int THREAD_GROUP = 1;
  	public final static int TEST = 2;
  	
  	//Identifiers
  	private final static String TARGET = "ActionProcessor.target";
  	private final static String ACTION = "ActionProcessor.action";
  	private final static String DURATION = "ActionProcessor.duration";
  
  	public TestAction()
  	{
  		super();
  		setTarget(THREAD);
  		setAction(PAUSE);
  		setDuration(0);
  	}
  
  	/* (non-Javadoc)
  	 * @see org.apache.jmeter.samplers.Sampler#sample(org.apache.jmeter.samplers.Entry)
  	 */
  	public SampleResult sample(Entry e) 
  	{
  //		SampleResult res = new SampleResult();
  		JMeterContext context = JMeterContextService.getContext();
  //		StringBuffer response = new StringBuffer();
  //				
  //		res.setSampleLabel(getTitle());
  //		res.setSuccessful(true);
  //		res.setResponseMessage("OK");
  //		res.sampleStart();
  
  		int target = getTarget();
  		int action = getAction();
  		if (target==THREAD)
  		{
  			JMeterThread thread = context.getThread();
  			if (action==PAUSE)
  			{
  //				res.setSamplerData("Pause Thread "+thread.getThreadNum());
  				thread.pauseThread(getDuration());
  //				response.append("Thread ");
  //				response.append(thread.getThreadNum());
  //				response.append(" paused for ");
  //				response.append(getDuration());
  //				response.append(" miliseconds");
  			}
  			else if (action==STOP)
  			{
  //				res.setSamplerData("Stop Thread"+thread.getThreadNum());
  				context.getThread().stop();
  //				response.append("Thread ");
  //				response.append(thread.getThreadNum());
  //				response.append(" stopped");
  			}
  		}
  //Not yet implemented
  //		else if (target==THREAD_GROUP)
  //		{
  //			if (action==PAUSE)
  //			{
  //			}
  //			else if (action==STOP)
  //			{
  //			}
  //		}
  		else if(target==TEST)
  		{
  			if (action==PAUSE)
  			{
  				context.getEngine().pauseTest(getDuration());
  			}
  			else if (action==STOP)
  			{
  				context.getEngine().askThreadsToStop();
  			}
  		}
  
  //		res.setResponseData(response.toString().getBytes());
  //		res.sampleEnd();
  //		return res;
  		return null;
  	}
  	
  	public void setTarget(int target)
  	{
  		setProperty(new IntegerProperty(TARGET,target));
  	}
  
  	public int getTarget()
  	{
  		return getPropertyAsInt(TARGET);
  	}
  	
  	public void setAction(int action)
  	{
  		setProperty(new IntegerProperty(ACTION,action));
  	}
  	
  	public int getAction()
  	{
  		return getPropertyAsInt(ACTION);
  	}
  	
  	public void setDuration(int duration)
  	{
  		setProperty(new IntegerProperty(DURATION,duration));
  	}
  	
  	public int getDuration()
  	{
  		return getPropertyAsInt(DURATION);
  	}
  	
  	private String getTitle()
  	{
  		return this.getName();
  	}
  }
  
  
  
  1.44      +20 -2     jakarta-jmeter/src/core/org/apache/jmeter/engine/StandardJMeterEngine.java
  
  Index: StandardJMeterEngine.java
  ===================================================================
  RCS file: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/engine/StandardJMeterEngine.java,v
  retrieving revision 1.43
  retrieving revision 1.44
  diff -u -r1.43 -r1.44
  --- StandardJMeterEngine.java	18 Feb 2004 23:23:58 -0000	1.43
  +++ StandardJMeterEngine.java	8 Apr 2004 18:40:08 -0000	1.44
  @@ -373,6 +373,24 @@
               thread.setScheduled(true);
           }
       }
  +    
  +    public synchronized void pauseTest(int milis)
  +    {
  +		Iterator iter = new HashSet(allThreads.keySet()).iterator();
  +		while (iter.hasNext())
  +		{
  +			Thread t = (Thread) allThreads.get(iter.next());
  +			if (t != null && t.isAlive())
  +			{
  +				try
  +				{
  +					t.sleep(milis);
  +				}
  +				catch (InterruptedException e)
  +				{}
  +			}
  +		}  	
  +    }
   
       private void verifyThreadsStopped()
       {
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: jmeter-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: jmeter-dev-help@jakarta.apache.org