/*
* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache JMeter" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* "Apache JMeter", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.jmeter.threads;
import java.util.*;
import org.apache.jmeter.samplers.SampleListener;
import org.apache.jmeter.control.NoEntryException;
import org.apache.jmeter.timers.Timer;
import org.apache.jmeter.control.SamplerController;
import org.apache.jmeter.samplers.SampleEvent;
import org.apache.jmeter.samplers.Sampler;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.samplers.*;
import org.apache.jmeter.assertions.*;
/************************************************************
* The JMeter interface to the sampling process, allowing JMeter to see the
* timing, add listeners for sampling events and to stop the sampling process.
*
*@author $Author: mstover1 $
*@created $Date: 2002/02/16 03:21:42 $
*@version $Revision: 1.17 $
***********************************************************/
public class JMeterThread implements Runnable, java.io.Serializable
{
ThreadGroup group;
Collection listeners;
List timers;
SamplerController controller;
private boolean running;
private int threadNumberInGroup;
static Map samplers = new HashMap();
/************************************************************
* !ToDo (Constructor description)
***********************************************************/
public JMeterThread()
{
}
/************************************************************
* !ToDo (Constructor description)
*
*@param group !ToDo (Parameter description)
***********************************************************/
public JMeterThread(ThreadGroup group)
{
this.group = group;
running = true;
threadNumberInGroup = group.threadNumber();
}
/************************************************************
* !ToDo (Method description)
***********************************************************/
public void run()
{
running = true;
timers = group.getTimers();
controller = (SamplerController)group.getSamplerController().clone();
listeners = controller.getListeners();
Entry entry = null;
rampUpDelay();
while (running)
{
if (!((entry = controller.nextEntry()) instanceof CycleEntry))
{
delay(timers);
if (entry == null)
{
running = false;
break;
}
Sampler sampler = null;
try
{
sampler = getSampler(entry);
}
catch (Exception ex)
{
ex.printStackTrace();
running = false;
}
SampleResult result = sampler.sample(entry);
checkAssertions(entry, result);
notifyListeners(result);
}
}
}
/************************************************************
* !ToDo (Method description)
***********************************************************/
public void stop()
{
running = false;
}
private Sampler getSampler(Entry entry) throws Exception
{
Class sClass = entry.getSamplerClass();
Sampler sampler = (Sampler)samplers.get(sClass);
if (sampler == null)
{
sampler = (Sampler)sClass.newInstance();
samplers.put(sClass, sampler);
}
return sampler;
}
private void checkAssertions(Entry entry, SampleResult result)
{
Iterator iter = entry.getAssertions().iterator();
List resultList = new LinkedList();
while (iter.hasNext())
{
resultList.add(((Assertion)iter.next()).getResult(result));
}
result.putValue(SampleResult.ASSERTION_RESULTS, resultList);
}
private void delay(List timers)
{
int sum = 0;
Iterator iter = timers.iterator();
while (iter.hasNext())
{
sum += ((Timer)iter.next()).delay();
}
try
{
Thread.sleep(sum);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
private void notifyListeners(SampleResult result)
{
SampleEvent event = new SampleEvent(result, group.getName());
Iterator iter = listeners.iterator();
while (iter.hasNext())
{
SampleListener item = (SampleListener)iter.next();
item.sampleOccurred(event);
}
group.sampleOccurred(event);
}
/**
* Initial delay if ramp-up period is active for this group
*/
private void rampUpDelay()
{
int rampUpMs = group.getRampUp() * 1000;
int threads = group.getNumThreads();
if (rampUpMs > 0 && threads > 0)
{
int delayPerThread = rampUpMs / threads;
int initialDelay = delayPerThread * threadNumberInGroup;
try
{
Thread.sleep(initialDelay);
System.out.println("Thread #" + (threadNumberInGroup + 1) + " of " + threads + " now active.");
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}