Extending Molotov

Molotov has a –use-extension option that can be used to load one or several arbitrary Python modules that contains some fixtures or event listeners.

Using extensions is useful when you want to implement a behavior that can be reused with arbitrary load tests.

In the example below record_time() is used to calculate the average response time of the load test:

"""

This Molotov script show how you can print
the average response time.

"""
import molotov
import time


_T = {}


def _now():
    return time.time() * 1000


@molotov.events()
async def record_time(event, **info):
    req = info.get('request')
    if event == 'sending_request':
        _T[req] = _now()
    elif event == 'response_received':
        _T[req] = _now() - _T[req]


@molotov.global_teardown()
def display_average():
    average = sum(_T.values()) / len(_T)
    print("\nAverage response time %dms" % average)

When a Molotov test uses this extension, the function will collect execution times and print out the average response time of all requests made by Molotov:

$ molotov --use-extension molotov/tests/example6.py --max-runs 10 loadtest.py
**** Molotov v1.4. Happy breaking! ****
Loading extension 'molotov/tests/example6.py'
Preparing 50 workers...
OK
SUCCESSES: 447 | FAILURES: 45 | WORKERS: 15
Average response time 19ms
SUCCESSES: 451 | FAILURES: 49
*** Bye ***