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 -c
Loading extension '../molotov/tests/example6.py'
Preparing 1 worker...
OK
[W:0] Starting
[W:0] Setting up session
[W:0] Running scenarios

Average response time 16ms
**** Molotov v2.6. Happy breaking! ****
SUCCESSES: 10 | FAILURES: 0
*** Bye ***