generated from amazon-archives/__template_MIT-0
-
Notifications
You must be signed in to change notification settings - Fork 468
Expand file tree
/
Copy pathtest_parser_performance.py
More file actions
71 lines (50 loc) · 1.8 KB
/
test_parser_performance.py
File metadata and controls
71 lines (50 loc) · 1.8 KB
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
import time
from contextlib import contextmanager
from typing import Generator, Literal, Union
import pytest
from pydantic import BaseModel, Field
from typing_extensions import Annotated
from aws_lambda_powertools.utilities.parser import parse
# adjusted for slower machines in CI too
PARSER_VALIDATION_SLA: float = 0.010
@contextmanager
def timing() -> Generator:
""" "Generator to quickly time operations. It can add 5ms so take that into account in elapsed time
Examples
--------
with timing() as t:
print("something")
elapsed = t()
"""
start = time.perf_counter()
yield lambda: time.perf_counter() - start # gen as lambda to calculate elapsed time
class SuccessfulCallback(BaseModel):
status: Literal["succeeded"]
name: str
breed: Literal["Husky", "Labrador"]
class FailedCallback(BaseModel):
status: Literal["failed"]
error: str
class TemporaryErrorCallback(BaseModel):
status: Literal["temporary_error"]
error: str
class PartisalSuccessCallback(BaseModel):
status: Literal["partial_success"]
name: str
breed: Literal["Husky", "Labrador"]
DogCallback = Annotated[
Union[SuccessfulCallback, FailedCallback, PartisalSuccessCallback, TemporaryErrorCallback],
Field(discriminator="status"),
]
@pytest.mark.perf
@pytest.mark.benchmark(group="core", disable_gc=True, warmup=False)
def test_parser_with_cache():
event = {"status": "temporary_error", "error": "X"}
# WHEN we call parser 999 times
with timing() as t:
for _ in range(999):
parse(event=event, model=DogCallback)
# THEN completion time should be below our validation SLA
elapsed = t()
if elapsed > PARSER_VALIDATION_SLA:
pytest.fail(f"Parser validation should be below {PARSER_VALIDATION_SLA}s: {elapsed}")