|
1 | 1 | import importlib
|
2 |
| -import time |
3 |
| -from contextlib import contextmanager |
4 | 2 | from types import ModuleType
|
5 |
| -from typing import Generator, Tuple |
| 3 | +from typing import Tuple |
6 | 4 |
|
7 | 5 | import pytest
|
8 | 6 |
|
9 | 7 | LOGGER_INIT_SLA: float = 0.005
|
10 | 8 | METRICS_INIT_SLA: float = 0.005
|
11 | 9 | TRACER_INIT_SLA: float = 0.5
|
12 | 10 | IMPORT_INIT_SLA: float = 0.035
|
| 11 | +PARENT_PACKAGE = "aws_lambda_powertools" |
| 12 | +TRACING_PACKAGE = "aws_lambda_powertools.tracing" |
| 13 | +LOGGING_PACKAGE = "aws_lambda_powertools.logging" |
| 14 | +METRICS_PACKAGE = "aws_lambda_powertools.metrics" |
13 | 15 |
|
14 | 16 |
|
15 |
| -@contextmanager |
16 |
| -def timing() -> Generator: |
17 |
| - """ "Generator to quickly time operations. It can add 5ms so take that into account in elapsed time |
| 17 | +def import_core_utilities() -> Tuple[ModuleType, ModuleType, ModuleType]: |
| 18 | + """Dynamically imports and return Tracing, Logging, and Metrics modules""" |
| 19 | + return ( |
| 20 | + importlib.import_module(TRACING_PACKAGE), |
| 21 | + importlib.import_module(LOGGING_PACKAGE), |
| 22 | + importlib.import_module(METRICS_PACKAGE), |
| 23 | + ) |
18 | 24 |
|
19 |
| - Examples |
20 |
| - -------- |
21 | 25 |
|
22 |
| - with timing() as t: |
23 |
| - print("something") |
24 |
| - elapsed = t() |
25 |
| - """ |
26 |
| - start = time.perf_counter() |
27 |
| - yield lambda: time.perf_counter() - start # gen as lambda to calculate elapsed time |
| 26 | +@pytest.fixture(autouse=True) |
| 27 | +def clear_cache(): |
| 28 | + importlib.invalidate_caches() |
28 | 29 |
|
29 | 30 |
|
30 |
| -def core_utilities() -> Tuple[ModuleType, ModuleType, ModuleType]: |
31 |
| - """Return Tracing, Logging, and Metrics module""" |
32 |
| - tracing = importlib.import_module("aws_lambda_powertools.tracing") |
33 |
| - logging = importlib.import_module("aws_lambda_powertools.logging") |
34 |
| - metrics = importlib.import_module("aws_lambda_powertools.metrics") |
| 31 | +def import_init_tracer(): |
| 32 | + tracing = importlib.import_module(TRACING_PACKAGE) |
| 33 | + tracing.Tracer(disabled=True) |
35 | 34 |
|
36 |
| - return tracing, logging, metrics |
| 35 | + |
| 36 | +def import_init_metrics(): |
| 37 | + metrics = importlib.import_module(METRICS_PACKAGE) |
| 38 | + metrics.Metrics() |
| 39 | + |
| 40 | + |
| 41 | +def import_init_logger(): |
| 42 | + logging = importlib.import_module(LOGGING_PACKAGE) |
| 43 | + logging.Logger() |
37 | 44 |
|
38 | 45 |
|
39 | 46 | @pytest.mark.perf
|
40 |
| -def test_import_times_ceiling(): |
| 47 | +@pytest.mark.benchmark(group="core", disable_gc=True, warmup=False) |
| 48 | +def test_import_times_ceiling(benchmark): |
41 | 49 | # GIVEN Core utilities are imported
|
42 | 50 | # WHEN none are used
|
43 | 51 | # THEN import and any global initialization perf should be below 30ms
|
44 | 52 | # though we adjust to 35ms to take into account different CI machines, etc.
|
45 | 53 | # instead of re-running tests which can lead to false positives
|
46 |
| - with timing() as t: |
47 |
| - core_utilities() |
48 |
| - |
49 |
| - elapsed = t() |
50 |
| - if elapsed > IMPORT_INIT_SLA: |
51 |
| - pytest.fail(f"High level imports should be below ${IMPORT_INIT_SLA}s: {elapsed}") |
| 54 | + benchmark.pedantic(import_core_utilities) |
| 55 | + stat = benchmark.stats.stats.max |
| 56 | + if stat > IMPORT_INIT_SLA: |
| 57 | + pytest.fail(f"High level imports should be below {IMPORT_INIT_SLA}s: {stat}") |
52 | 58 |
|
53 | 59 |
|
54 | 60 | @pytest.mark.perf
|
55 |
| -def test_tracer_init(): |
| 61 | +@pytest.mark.benchmark(group="core", disable_gc=True, warmup=False) |
| 62 | +def test_tracer_init(benchmark): |
56 | 63 | # GIVEN Tracer is initialized
|
57 | 64 | # WHEN default options are used
|
58 | 65 | # THEN initialization X-Ray SDK perf should be below 450ms
|
59 | 66 | # though we adjust to 500ms to take into account different CI machines, etc.
|
60 | 67 | # instead of re-running tests which can lead to false positives
|
61 |
| - with timing() as t: |
62 |
| - tracing, _, _ = core_utilities() |
63 |
| - tracing.Tracer(disabled=True) # boto3 takes ~200ms, and remaining is X-Ray SDK init |
64 |
| - |
65 |
| - elapsed = t() |
66 |
| - if elapsed > TRACER_INIT_SLA: |
67 |
| - pytest.fail(f"High level imports should be below ${TRACER_INIT_SLA}s: {elapsed}") |
| 68 | + benchmark.pedantic(import_init_tracer) |
| 69 | + stat = benchmark.stats.stats.max |
| 70 | + if stat > TRACER_INIT_SLA: |
| 71 | + pytest.fail(f"High level imports should be below {TRACER_INIT_SLA}s: {stat}") |
68 | 72 |
|
69 | 73 |
|
70 | 74 | @pytest.mark.perf
|
71 |
| -def test_metrics_init(): |
| 75 | +@pytest.mark.benchmark(group="core", disable_gc=True, warmup=False) |
| 76 | +def test_metrics_init(benchmark): |
72 | 77 | # GIVEN Metrics is initialized
|
73 | 78 | # WHEN default options are used
|
74 | 79 | # THEN initialization perf should be below 5ms
|
75 |
| - with timing() as t: |
76 |
| - _, _, metrics = core_utilities() |
77 |
| - metrics.Metrics() |
78 |
| - |
79 |
| - elapsed = t() |
80 |
| - if elapsed > METRICS_INIT_SLA: |
81 |
| - pytest.fail(f"High level imports should be below ${METRICS_INIT_SLA}s: {elapsed}") |
| 80 | + benchmark.pedantic(import_init_metrics) |
| 81 | + stat = benchmark.stats.stats.max |
| 82 | + if stat > METRICS_INIT_SLA: |
| 83 | + pytest.fail(f"High level imports should be below ${METRICS_INIT_SLA}s: {stat}") |
82 | 84 |
|
83 | 85 |
|
84 | 86 | @pytest.mark.perf
|
85 |
| -def test_logger_init(): |
| 87 | +@pytest.mark.benchmark(group="core", disable_gc=True, warmup=False) |
| 88 | +def test_logger_init(benchmark): |
86 | 89 | # GIVEN Logger is initialized
|
87 | 90 | # WHEN default options are used
|
88 | 91 | # THEN initialization perf should be below 5ms
|
89 |
| - with timing() as t: |
90 |
| - _, logging, _ = core_utilities() |
91 |
| - logging.Logger() |
92 |
| - |
93 |
| - elapsed = t() |
94 |
| - if elapsed > LOGGER_INIT_SLA: |
95 |
| - pytest.fail(f"High level imports should be below ${LOGGER_INIT_SLA}s: {elapsed}") |
| 92 | + benchmark.pedantic(import_init_logger) |
| 93 | + stat = benchmark.stats.stats.max |
| 94 | + if stat > LOGGER_INIT_SLA: |
| 95 | + pytest.fail(f"High level imports should be below ${LOGGER_INIT_SLA}s: {stat}") |
0 commit comments