aboutsummaryrefslogtreecommitdiffstats
path: root/src/scenarios/base.py
blob: f15a82f9968edbe59ce54b86aa6c7a2950b38247 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python3

# scenarios/base.py - base scenario class

# inherit from BaseScenario and override the `run()` method
# to define your own load test behavior. that's it.

import logging
import time

logger = logging.getLogger("loadtest.scenario")


class BaseScenario:
    """
    base class for all load test scenarios.

    subclass this and implement `run(page)` to define what
    each virtual user does during the load test. the `page`
    argument is a playwright Page object — you can navigate,
    click, scrape, fill forms, whatever you want.

    optionally override `setup(page)` and `teardown(page)`
    for per-iteration init and cleanup.
    """

    name = "base"

    def setup(self, page):
        """
        called BEFORE each iteration of `run()`.
        override this if you need to do login, seed data, etc.
        """
        pass

    def run(self, page):
        """
        the main scenario logic. this is what each virtual
        user will execute on every iteration.

        override this method in your subclass.

        Args:
            page: a playwright Page object.
        """
        raise NotImplementedError(
            "you need to implement the run() method in your scenario. "
            "check out scenarios/example.py for reference."
        )

    def teardown(self, page):
        """
        called AFTER each iteration of `run()`.
        override this for cleanup, logging out, etc.
        """
        pass

    def on_response(self, response):
        """
        optional hook — called on every HTTP response the page
        receives during the scenario. useful for logging API
        calls, checking status codes, etc.

        override if you want response-level visibility.

        Args:
            response: a playwright Response object.
        """
        pass

    def on_request(self, request):
        """
        optional hook — called on every HTTP request the page
        makes during the scenario. useful for logging outgoing
        requests.

        override if you want request-level visibility.

        Args:
            request: a playwright Request object.
        """
        pass