API Reference

Workload Generator

class wg.WorkerThread(parent, sleep_time=2)[source]

This constructor should always be called with keyword arguments. Arguments are:

group should be None; reserved for future extension when a ThreadGroup class is implemented.

target is the callable object to be invoked by the run() method. Defaults to None, meaning nothing is called.

name is the thread name. By default, a unique name is constructed of the form “Thread-N” where N is a small decimal number.

args is the argument tuple for the target invocation. Defaults to ().

kwargs is a dictionary of keyword arguments for the target invocation. Defaults to {}.

If a subclass overrides the constructor, it must make sure to invoke the base class constructor (Thread.__init__()) before doing anything else to the thread.

run()[source]

Method representing the thread’s activity.

You may override this method in a subclass. The standard run() method invokes the callable object passed to the object’s constructor as the target argument, if any, with sequential and keyword arguments taken from the args and kwargs arguments, respectively.

class wg.WorkloadGenerator(worker_func, rps=0.16666666666666666, delay_func=None, worker_thread_count=10, *args, **kwargs)[source]

WorkloadGenerator is the class responsible for generating the desired workload using the delay function provided, to achieve the target requests per second.

Returns

an instance of the WorkloadGenerator class

Return type

object

__init__ for WorkloadGenerator class.

Parameters
  • worker_func (function) – the worker function that will be called by the worker threads, it shouldn’t have any arguments and should return a dict.

  • rps (float, optional) – desired requests per second to be achieved by the workload generator, defaults to 10/60

  • delay_func (function, optional) – the function that generates a draw from inter-arrival time given rps as an argument, defaults to exponential distribution

  • worker_thread_count (int, optional) – number of worker threads, defaults to 10

fire()[source]

fire causes one of the worker threads to call worker_func once

fire_wait()[source]

fire_wait fires a request, generates an inter-arrival delay using delay_finc, then waits for that amount of time.

get_stats()[source]

get_stats gathers the values generated by calling the workload function throughout the test.

Returns

stats

Return type

array of dicts

prepare_test()[source]

prepare_test resets the timer that will be used to time the requests.

reset_stats()[source]

reset_stats resets the info gathered from worker threads.

Returns

sucess

Return type

boolean

set_rps(new_rps)[source]

set_rps sets the number of requests per second that will be made by the workers.

Parameters

new_rps (float) – the new rps

Returns

success

Return type

boolean

start_workers()[source]

start_workers starts up the worker pool

stop_workers()[source]

stop_workers stops all workers and waits until the threads are all shut down.

Returns

success

Return type

boolean

wg.get_random_wait_time(rps)[source]

get_random_wait_time generates random exponential inter-arrival times corresponding to Poisson process.

Parameters

rps (float) – rps or requests per second is the target number of requests per second

Returns

a draw from the resulting exponential distribution for inter-arrival time

Return type

float

Timer

class timer.TimerClass[source]

TimerClass is an object that can help with timing different components of the execution.

Returns

TimerClass object

Return type

object

tic()[source]

tic resets the starting time fo the timer. toc() will get seconds past since calling tic().

toc()[source]
toc calculates the number of seconds passed since tic() has been called, or object has been created,

whichever is more recent.

Returns

elapsed time since time reference

Return type

float

toc_print()[source]

toc_print prints the value returned by toc_str

Returns

number of seconds elapsed with 2 digits of precision.

Return type

string

toc_str()[source]

toc_str returns the time elapsed in string with 2 digits of precision. This is for user printing mainly.

Returns

number of seconds elapsed with 2 digits of precision.

Return type

string

timer.get_time_in_secs(s)[source]

get_time_in_secs converts the string given to seconds, e.g. 1h is converted to 3600.

Parameters

s (string) – input string of the format “Nu” where N is a number and u is a unit (s/m/h/d/w).

Returns

Number of seconds

Return type

float