forked from starenka/mailjetv3
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathtest_perf.py
More file actions
51 lines (40 loc) · 1.97 KB
/
Copy pathtest_perf.py
File metadata and controls
51 lines (40 loc) · 1.97 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
from typing import Any, Generator
import pytest
import responses
from mailjet_rest.client import Client
# ------------------------------------------------------------------------
# FIXTURES
# ------------------------------------------------------------------------
# --- Fixture needs a Generator return type ---
@pytest.fixture
def mocked_mailjet() -> Generator[responses.RequestsMock, None, None]:
"""Intercepts Mailjet API calls at the urllib3 layer for stable benchmarks."""
with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps:
rsps.add(
responses.POST,
"https://api.mailjet.com/v3/REST/contact",
json={"Count": 1, "Data": [{"ID": 123}]},
status=201,
)
yield rsps
# ------------------------------------------------------------------------
# BENCHMARK 1: ROUTING OVERHEAD (CPU)
# ------------------------------------------------------------------------
def test_client_routing_speed(benchmark: Any) -> None:
"""Measure CPU overhead of the dynamic __getattr__ router and caching logic."""
client = Client(auth=("api", "key"))
def route_contact() -> Any:
# Tests the efficiency of the endpoint cache dictionary
return client.contact
benchmark(route_contact)
# ------------------------------------------------------------------------
# BENCHMARK 2: FULL REQUEST CYCLE (MOCKED NETWORK)
# ------------------------------------------------------------------------
def test_request_cycle_performance(benchmark: Any, mocked_mailjet: responses.RequestsMock) -> None:
"""Measure the time from method call to response (with zero network delay)."""
client = Client(auth=("api", "key"))
payload = {"Email": "perf@example.com", "Name": "Benchmark User"}
def send_request() -> Any:
return client.contact.create(data=payload)
# Use pedantic mode for higher accuracy across multiple iterations
benchmark.pedantic(send_request, rounds=50, iterations=10)