@@ -0,0 +1,2139 @@
+%22%22%22%0AExample of a manual_report() function that can be used either as a context manager %0A(with statement), or a decorator, to manually add entries to Locust's statistics.%0A%0AUsage as a context manager:%0A%0A    with manual_report(%22stats entry name%22):%0A        # Run time of this block will be reported under a stats entry called %22stats entry name%22%0A        # do stuff here, if an Exception is raised, it'll be reported as a failure%0A    %0AUsage as a decorator:%0A%0A    @task%0A    @manual_report%0A    def my_task(self):%0A       # The run time of this task will be reported under a stats entry called %22my task%22 (type %22manual%22).%0A       # If an Exception is raised, it'll be reported as a failure%0A%0A%22%22%22%0A%0Aimport random%0Afrom contextlib import contextmanager, ContextDecorator%0Afrom time import time, sleep%0A%0Afrom locust import User, task, constant, events%0A%0A%0A@contextmanager%0Adef _manual_report(name):%0A    start_time = time()%0A    try:%0A        yield%0A    except Exception as e:%0A        events.request_failure.fire(%0A            request_type=%22manual%22,%0A            name=name,%0A            response_time=(time() - start_time) * 1000,%0A            response_length=0,%0A            exception=e,%0A        )%0A        raise%0A    else:%0A        events.request_success.fire(%0A            request_type=%22manual%22,%0A            name=name,%0A            response_time=(time() - start_time) * 1000,%0A            response_length=0,%0A        )%0A%0A%0Adef manual_report(name_or_func):%0A    if callable(name_or_func):%0A        # used as decorator without name argument specified%0A        return _manual_report(name_or_func.__name__)(name_or_func)%0A    else:%0A        return _manual_report(name_or_func)%0A%0A%0Aclass MyUser(User):%0A    wait_time = constant(1)%0A%0A    @task%0A    def successful_task(self):%0A        with manual_report(%22successful_task%22):%0A            sleep(random.random())%0A%0A    @task%0A    @manual_report%0A    def decorator_test(self):%0A        if random.random() %3E 0.5:%0A            raise Exception(%22decorator_task failed%22)%0A        sleep(random.random())%0A%0A    @task%0A    def failing_task(self):%0A        with manual_report(%22failing_task%22):%0A            sleep(random.random())%0A            raise Exception(%22Oh nooes!%22)%0A

Add example with a manual_report decorator/context manager that can be used to easily measure the time of functions/code blocks and manually report them as stats entries to the Locust statistics