net.grinder.script
Interface Grinder.ScriptContext

All Known Subinterfaces:
InternalScriptContext
Enclosing class:
Grinder

public static interface Grinder.ScriptContext

Scripts can get contextual information and access services through a global net.grinder.script.Grinder.grinder object that supports this interface.

Author:
Philip Aston

Method Summary
 Barrier barrier(String name)
          Create a Barrier to coordinate worker thread actions across running worker processes.
 int getAgentNumber()
          Return the agent number.
 int getFirstProcessNumber()
          Return the process number of the first worker process.
 org.slf4j.Logger getLogger()
          Get a Logger.
 String getProcessName()
          Get a unique name for this worker process.
 int getProcessNumber()
          Return the process number.
 GrinderProperties getProperties()
          Get the global properties for this agent/worker process set.
 int getRunNumber()
          Return the current run number, or -1 if not called from a worker thread.
 SSLControl getSSLControl()
          Get an SSLControl.
 Statistics getStatistics()
          Get a Statistics object that allows statistics to be queried and updated.
 int getThreadNumber()
          Return the thread number, or -1 if not called from a worker thread.
 void sleep(long meanTime)
          Sleep for a time based on the meanTime parameter.
 void sleep(long meanTime, long sigma)
          Sleep for a time based on the meanTime parameter.
 int startWorkerThread()
          Start a new worker thread.
 int startWorkerThread(Object testRunner)
          Start a new worker thread, specifying a test runner instance.
 void stopThisWorkerThread()
          Stop this worker thread immediately and cleanly.
 boolean stopWorkerThread(int threadNumber)
          Request a specific worker thread to stop.
 

Method Detail

getAgentNumber

int getAgentNumber()
Return the agent number. The console allocates a unique number to that each connected agent, and the agent passes this on to the worker process.

The lowest possible number is allocated. When an agent disconnects, its number will be reused. Script authors can assume that the agent number lies between 0 and the number of currently connected agents.

Returns:
The agent number, or -1 if not launched from the console.
See Also:
getProcessNumber(), getThreadNumber()

getProcessNumber

int getProcessNumber()
Return the process number. The agent allocates a unique number to that each worker process it starts.

Returns:
The process number.
See Also:
getAgentNumber(), getThreadNumber(), getFirstProcessNumber()

getProcessName

String getProcessName()
Get a unique name for this worker process.

Returns:
The process name.
See Also:
getProcessNumber()

getFirstProcessNumber

int getFirstProcessNumber()
Return the process number of the first worker process. The first process is one our agent started first upon receiving a start signal from the console.

Process numbers are assigned incrementally, so the expression (grinder.getProcessNumber() - grinder.getFirstProcessNumber()) can be used to as a zero based index for this process against an array of test data.

Returns:
The first process number.
See Also:
getProcessNumber()

getThreadNumber

int getThreadNumber()
Return the thread number, or -1 if not called from a worker thread.

Returns:
The thread number.
See Also:
getAgentNumber(), getProcessNumber()

getRunNumber

int getRunNumber()
Return the current run number, or -1 if not called from a worker thread.

Returns:
An int value.

getLogger

org.slf4j.Logger getLogger()
Get a Logger.

Returns:
A Logger.

sleep

void sleep(long meanTime)
           throws GrinderException
Sleep for a time based on the meanTime parameter. The actual time may be greater or less than meanTime, and is distributed according to a pseudo normal distribution.

Parameters:
meanTime - Mean time in milliseconds.
Throws:
GrinderException - If the sleep failed.

sleep

void sleep(long meanTime,
           long sigma)
           throws GrinderException
Sleep for a time based on the meanTime parameter. The actual time may be greater or less than meanTime, and is distributed according to a pseudo normal distribution.

Parameters:
meanTime - Mean time in milliseconds.
sigma - The standard deviation, in milliseconds.
Throws:
GrinderException - If the sleep failed.

startWorkerThread

int startWorkerThread()
                      throws GrinderException
Start a new worker thread. The script's TestRunner class will be used to create new test runner instance for the worker thread.

Returns:
The thread number of the new worker thread.
Throws:
InvalidContextException - If the main thread has not yet initialised the script engine, or all other threads have shut down. Typically, you should only call startWorkerThread() from another worker thread.
GrinderException - If the new worker thread could not be started.

startWorkerThread

int startWorkerThread(Object testRunner)
                      throws GrinderException
Start a new worker thread, specifying a test runner instance.

This is a more advanced version of startWorkerThread() that allows a different test runner to be specified. The test runner should be a function or a callable object.

Parameters:
testRunner - A function, or some other callable object.
Returns:
The thread number of the new worker thread.
Throws:
InvalidContextException - If the main thread has not yet initialised the script engine, or all other threads have shut down. Typically, you should only call startWorkerThread() from another worker thread.
GrinderException - If the new worker thread could not be started.

stopThisWorkerThread

void stopThisWorkerThread()
                          throws InvalidContextException
Stop this worker thread immediately and cleanly.

This method works by throwing a special unchecked exception. If the caller catches this exception, rather than letting it propagate, the thread will not stop.

Throws:
InvalidContextException - If called from a non-worker thread.

stopWorkerThread

boolean stopWorkerThread(int threadNumber)
Request a specific worker thread to stop. If the thread was running, it will shut down be the next time it enters code instrumented by a Test.

Parameters:
threadNumber - The thread number of the worker thread to stop.
Returns:
true if a thread existed with the given threadNumber, otherwise false.

getProperties

GrinderProperties getProperties()
Get the global properties for this agent/worker process set.

Returns:
The properties.

getStatistics

Statistics getStatistics()
Get a Statistics object that allows statistics to be queried and updated.

Returns:
The statistics.

getSSLControl

SSLControl getSSLControl()
Get an SSLControl. This can be used to create secure sockets or to set the certificates that a worker thread should use.

Returns:
The SSL control.

barrier

Barrier barrier(String name)
                throws GrinderException
Create a Barrier to coordinate worker thread actions across running worker processes.

Example script:

 from net.grinder.script.Grinder import grinder

 class TestRunner:
   def __init__(self):
     # Each worker thread joins the barrier.
     self.phase1CompleteBarrier = grinder.barrier("Phase 1")

   def __call__(self):

     # ... Phase 1 actions.

     # Wait for all worker threads to reach this point before proceeding.
     self.phase1CompleteBarrier.await()

     # ... Further actions.
 

Parameters:
name - The barrier name.
Returns:
The barrier.
Throws:
GrinderException - If the barrier could not be created due to a network problem.
Since:
3.6
See Also:
Barrier


Copyright © 2000-2013. All Rights Reserved.