Skip to content

API

HttpUser

Bases: User

Source code in src/aiolocust/users/http.py
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
class HttpUser(User):
    session_kwargs: dict[str, Any] = {"timeout": aiohttp.ClientTimeout(60.0)}
    """
    Extra arguments to pass to aiohttp.ClientSession, e.g.
    ```
    class TimeoutUser(HttpUser):
        session_kwargs = {
            "timeout": aiohttp.ClientTimeout(0.0001),
            "skip_auto_headers": {"User-Agent"},
        }
        ...
    ```
    """

    ssl_context: None | ssl.SSLContext = None
    """
    Used to create a custom TCPConnector for LocustClientSession,
    instead of having to pass ssl=ssl_context on each request.

    For example, to use OS-managed trust you could do something like this:

    import truststore
    from aiolocust import HttpUser

    class MyUser(HttpUser):
        ssl_context = truststore.SSLContext(protocol=ssl.PROTOCOL_TLS_CLIENT)
        ...
    """

    def __init__(self, runner: Runner | None = None, base_url=None):
        super().__init__(runner)
        self.base_url = base_url or runner.host if runner else None
        self.client: LocustClientSession  # type: ignore[assignment] # always set in cm

    @asynccontextmanager
    async def cm(self):
        async with LocustClientSession(
            self.runner,
            self.base_url,
            connector=aiohttp.TCPConnector(ssl=self.ssl_context) if self.ssl_context else None,
            **self.session_kwargs,
        ) as self.client:
            yield

session_kwargs = {'timeout': aiohttp.ClientTimeout(60.0)} class-attribute instance-attribute

Extra arguments to pass to aiohttp.ClientSession, e.g.

class TimeoutUser(HttpUser):
    session_kwargs = {
        "timeout": aiohttp.ClientTimeout(0.0001),
        "skip_auto_headers": {"User-Agent"},
    }
    ...

ssl_context = None class-attribute instance-attribute

Used to create a custom TCPConnector for LocustClientSession, instead of having to pass ssl=ssl_context on each request.

For example, to use OS-managed trust you could do something like this:

import truststore from aiolocust import HttpUser

class MyUser(HttpUser): ssl_context = truststore.SSLContext(protocol=ssl.PROTOCOL_TLS_CLIENT) ...

User

Bases: ABC

Source code in src/aiolocust/__init__.py
15
16
17
18
19
20
21
22
23
24
25
26
class User(ABC):
    def __init__(self, runner: Runner | None = None, **kwargs):
        self.runner: Runner = runner  # pyright: ignore[reportAttributeAccessIssue] # always set outside of unit testing
        self.running = True

    @abstractmethod
    def run(self) -> AbcCoroutine[Any, Any, None]: ...

    @asynccontextmanager
    async def cm(self):
        """Override this method if you need an async context manager around the run method"""
        yield

cm() async

Override this method if you need an async context manager around the run method

Source code in src/aiolocust/__init__.py
23
24
25
26
@asynccontextmanager
async def cm(self):
    """Override this method if you need an async context manager around the run method"""
    yield

datatypes

SafeCounter

A thread-safe counter.

Source code in src/aiolocust/datatypes.py
55
56
57
58
59
60
61
62
63
64
65
66
67
68
class SafeCounter:
    """A thread-safe counter."""

    def __init__(self, limit: int | None = None):
        self.value = 0
        self.limit = limit if limit is not None else sys.maxsize
        self.lock = threading.Lock()

    def increment(self) -> bool:
        with self.lock:
            if self.value < self.limit:
                self.value += 1
                return False
            return True

users

http

HttpUser

Bases: User

Source code in src/aiolocust/users/http.py
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
class HttpUser(User):
    session_kwargs: dict[str, Any] = {"timeout": aiohttp.ClientTimeout(60.0)}
    """
    Extra arguments to pass to aiohttp.ClientSession, e.g.
    ```
    class TimeoutUser(HttpUser):
        session_kwargs = {
            "timeout": aiohttp.ClientTimeout(0.0001),
            "skip_auto_headers": {"User-Agent"},
        }
        ...
    ```
    """

    ssl_context: None | ssl.SSLContext = None
    """
    Used to create a custom TCPConnector for LocustClientSession,
    instead of having to pass ssl=ssl_context on each request.

    For example, to use OS-managed trust you could do something like this:

    import truststore
    from aiolocust import HttpUser

    class MyUser(HttpUser):
        ssl_context = truststore.SSLContext(protocol=ssl.PROTOCOL_TLS_CLIENT)
        ...
    """

    def __init__(self, runner: Runner | None = None, base_url=None):
        super().__init__(runner)
        self.base_url = base_url or runner.host if runner else None
        self.client: LocustClientSession  # type: ignore[assignment] # always set in cm

    @asynccontextmanager
    async def cm(self):
        async with LocustClientSession(
            self.runner,
            self.base_url,
            connector=aiohttp.TCPConnector(ssl=self.ssl_context) if self.ssl_context else None,
            **self.session_kwargs,
        ) as self.client:
            yield
session_kwargs = {'timeout': aiohttp.ClientTimeout(60.0)} class-attribute instance-attribute

Extra arguments to pass to aiohttp.ClientSession, e.g.

class TimeoutUser(HttpUser):
    session_kwargs = {
        "timeout": aiohttp.ClientTimeout(0.0001),
        "skip_auto_headers": {"User-Agent"},
    }
    ...
ssl_context = None class-attribute instance-attribute

Used to create a custom TCPConnector for LocustClientSession, instead of having to pass ssl=ssl_context on each request.

For example, to use OS-managed trust you could do something like this:

import truststore from aiolocust import HttpUser

class MyUser(HttpUser): ssl_context = truststore.SSLContext(protocol=ssl.PROTOCOL_TLS_CLIENT) ...

pw

LocustPage

A wrapper for the Playwright Page object to automatically generate OTel spans.

Source code in src/aiolocust/users/pw.py
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
class LocustPage:
    """A wrapper for the Playwright Page object to automatically generate OTel spans."""

    def __init__(self, page: Page):
        self._page = page

    async def goto(self, url: str, **kwargs):
        with tracer.start_as_current_span("playwright.goto") as span:
            span.set_attribute("browser.url", url)
            try:
                result = await self._page.goto(url, **kwargs)
                await events.request.fire(Request(url, 1, 1, None))
            except Exception as e:
                span.record_exception(e)
                await events.request.fire(Request(url, 1, 1, e))
                raise
            return result

    async def click(self, selector: str, **kwargs):
        with tracer.start_as_current_span("playwright.click") as span:
            span.set_attribute("browser.selector", selector)
            try:
                result = await self._page.click(selector, **kwargs)
                await events.request.fire(Request(selector, 1, 1, None))
            except Exception as e:
                span.record_exception(e)
                await events.request.fire(Request(selector, 1, 1, e))
                raise
            return result