-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathtest_benchmarks.py
97 lines (73 loc) · 2.89 KB
/
test_benchmarks.py
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import builtins
import json
import os
import pytest
import ddtrace
from datadog_lambda import metric
from datadog_lambda import tag_object
from datadog_lambda import tracing
from datadog_lambda import trigger
from datadog_lambda import xray
from datadog_lambda.constants import XrayDaemon, XraySubsegment
from tests.utils import get_mock_context, reset_xray_connection
event_samples_dir = "tests/event_samples"
event_samples = [f[:-5] for f in os.listdir(event_samples_dir) if f.endswith(".json")]
def test_metric_write_metric_point_to_stdout(benchmark, monkeypatch):
monkeypatch.setattr(builtins, "print", lambda *a, **k: None)
benchmark(
metric.write_metric_point_to_stdout,
"metric_name",
1,
tags=[
"tag1:value1",
"tag2:value2",
"tag3:value3",
],
)
@pytest.mark.parametrize("event", event_samples)
def test_tag_object_tag_object(event, benchmark):
with open(f"{event_samples_dir}/{event}.json") as f:
event = json.load(f)
span = ddtrace.tracer.start_span("test")
benchmark(tag_object.tag_object, span, "function.request", event)
@pytest.mark.parametrize("event", event_samples)
def test_tracing_create_inferred_span(event, benchmark):
with open(f"{event_samples_dir}/{event}.json") as f:
event = json.load(f)
context = get_mock_context()
benchmark(tracing.create_inferred_span, event, context)
@pytest.mark.parametrize("event", event_samples)
def test_tracing_extract_dd_trace_context(event, benchmark):
with open(f"{event_samples_dir}/{event}.json") as f:
event = json.load(f)
context = get_mock_context()
benchmark(tracing.extract_dd_trace_context, event, context)
@pytest.mark.parametrize("event", event_samples)
def test_trigger_parse_event_source(event, benchmark):
with open(f"{event_samples_dir}/{event}.json") as f:
event = json.load(f)
benchmark(trigger.parse_event_source, event)
@pytest.mark.parametrize("event", event_samples)
def test_trigger_extract_trigger_tags(event, benchmark):
with open(f"{event_samples_dir}/{event}.json") as f:
event = json.load(f)
context = get_mock_context()
benchmark(trigger.extract_trigger_tags, event, context)
def test_xray_send_segment(benchmark, monkeypatch):
reset_xray_connection()
monkeypatch.setenv(XrayDaemon.XRAY_DAEMON_ADDRESS, "localhost:9000")
monkeypatch.setenv(
XrayDaemon.XRAY_TRACE_ID_HEADER_NAME,
"Root=1-5e272390-8c398be037738dc042009320;Parent=94ae789b969f1cc5;Sampled=1;Lineage=c6c5b1b9:0",
)
def socket_send(*a, **k):
sends.append(True)
sends = []
monkeypatch.setattr("socket.socket.send", socket_send)
key = {
"trace-id": "12345678901234567890123456789012",
"parent-id": "1234567890123456",
"sampling-priority": "1",
}
benchmark(xray.send_segment, XraySubsegment.TRACE_KEY, key)
assert sends