|
1 | | -import timeit |
2 | | - |
3 | 1 | from ddtrace import Tracer |
| 2 | +import pytest |
4 | 3 |
|
5 | 4 | from .test_tracer import DummyWriter |
6 | | -from os import getpid |
7 | | - |
8 | 5 |
|
9 | | -REPEAT = 10 |
10 | | -NUMBER = 10000 |
11 | 6 |
|
| 7 | +@pytest.fixture |
| 8 | +def tracer(): |
| 9 | + tracer = Tracer() |
| 10 | + tracer.writer = DummyWriter() |
| 11 | + return tracer |
12 | 12 |
|
13 | | -def trace_error(tracer): |
14 | | - # explicit vars |
15 | | - with tracer.trace('a', service='s', resource='r', span_type='t'): |
16 | | - 1 / 0 |
17 | 13 |
|
| 14 | +def test_tracer_context(benchmark, tracer): |
| 15 | + def func(tracer): |
| 16 | + with tracer.trace('a', service='s', resource='r', span_type='t'): |
| 17 | + pass |
18 | 18 |
|
19 | | -def benchmark_tracer_trace(): |
20 | | - tracer = Tracer() |
21 | | - tracer.writer = DummyWriter() |
| 19 | + benchmark(func, tracer) |
22 | 20 |
|
23 | | - # testcase |
24 | | - def trace(tracer): |
25 | | - # explicit vars |
26 | | - with tracer.trace('a', service='s', resource='r', span_type='t') as s: |
27 | | - s.set_tag('a', 'b') |
28 | | - s.set_tag('b', 1) |
29 | | - with tracer.trace('another.thing'): |
30 | | - pass |
31 | | - with tracer.trace('another.thing'): |
32 | | - pass |
33 | | - |
34 | | - # benchmark |
35 | | - print('## tracer.trace() benchmark: {} loops ##'.format(NUMBER)) |
36 | | - timer = timeit.Timer(lambda: trace(tracer)) |
37 | | - result = timer.repeat(repeat=REPEAT, number=NUMBER) |
38 | | - print('- trace execution time: {:8.6f}'.format(min(result))) |
39 | | - |
40 | | - |
41 | | -def benchmark_tracer_wrap(): |
42 | | - tracer = Tracer() |
43 | | - tracer.writer = DummyWriter() |
44 | 21 |
|
45 | | - # testcase |
| 22 | +def test_tracer_wrap_staticmethod(benchmark, tracer): |
46 | 23 | class Foo(object): |
47 | 24 | @staticmethod |
48 | 25 | @tracer.wrap() |
49 | | - def s(): |
| 26 | + def func(): |
50 | 27 | return 0 |
51 | 28 |
|
| 29 | + f = Foo() |
| 30 | + benchmark(f.func) |
| 31 | + |
| 32 | + |
| 33 | +def test_tracer_wrap_classmethod(benchmark, tracer): |
| 34 | + class Foo(object): |
52 | 35 | @classmethod |
53 | 36 | @tracer.wrap() |
54 | | - def c(cls): |
| 37 | + def func(cls): |
55 | 38 | return 0 |
56 | 39 |
|
| 40 | + f = Foo() |
| 41 | + benchmark(f.func) |
| 42 | + |
| 43 | + |
| 44 | +def test_tracer_wrap_instancemethod(benchmark, tracer): |
| 45 | + class Foo(object): |
57 | 46 | @tracer.wrap() |
58 | | - def m(self): |
| 47 | + def func(self): |
59 | 48 | return 0 |
60 | 49 |
|
61 | 50 | f = Foo() |
| 51 | + benchmark(f.func) |
| 52 | + |
| 53 | + |
| 54 | +def test_tracer_start_span(benchmark, tracer): |
| 55 | + benchmark(tracer.start_span, 'benchmark') |
| 56 | + |
| 57 | + |
| 58 | +def test_tracer_start_finish_span(benchmark, tracer): |
| 59 | + def func(tracer): |
| 60 | + s = tracer.start_span('benchmark') |
| 61 | + s.finish() |
| 62 | + |
| 63 | + benchmark(func, tracer) |
| 64 | + |
| 65 | + |
| 66 | +def test_trace_simple_trace(benchmark, tracer): |
| 67 | + def func(tracer): |
| 68 | + with tracer.trace('parent'): |
| 69 | + for i in range(5): |
| 70 | + with tracer.trace('child') as c: |
| 71 | + c.set_tag('i', i) |
| 72 | + |
| 73 | + benchmark(func, tracer) |
| 74 | + |
| 75 | + |
| 76 | +def test_tracer_large_trace(benchmark, tracer): |
| 77 | + import random |
| 78 | + |
| 79 | + # generate trace with 1024 spans |
| 80 | + @tracer.wrap() |
| 81 | + def func(tracer, level=0): |
| 82 | + span = tracer.current_span() |
| 83 | + |
| 84 | + # do some work |
| 85 | + num = random.randint(1, 10) |
| 86 | + span.set_tag('num', num) |
| 87 | + |
| 88 | + if level < 10: |
| 89 | + func(tracer, level+1) |
| 90 | + func(tracer, level+1) |
62 | 91 |
|
63 | | - # benchmark |
64 | | - print('## tracer.trace() wrapper benchmark: {} loops ##'.format(NUMBER)) |
65 | | - timer = timeit.Timer(f.s) |
66 | | - result = timer.repeat(repeat=REPEAT, number=NUMBER) |
67 | | - print('- staticmethod execution time: {:8.6f}'.format(min(result))) |
68 | | - timer = timeit.Timer(f.c) |
69 | | - result = timer.repeat(repeat=REPEAT, number=NUMBER) |
70 | | - print('- classmethod execution time: {:8.6f}'.format(min(result))) |
71 | | - timer = timeit.Timer(f.m) |
72 | | - result = timer.repeat(repeat=REPEAT, number=NUMBER) |
73 | | - print('- method execution time: {:8.6f}'.format(min(result))) |
74 | | - |
75 | | - |
76 | | -def benchmark_getpid(): |
77 | | - timer = timeit.Timer(getpid) |
78 | | - result = timer.repeat(repeat=REPEAT, number=NUMBER) |
79 | | - print('## getpid wrapper benchmark: {} loops ##'.format(NUMBER)) |
80 | | - print('- getpid execution time: {:8.6f}'.format(min(result))) |
81 | | - |
82 | | - |
83 | | -if __name__ == '__main__': |
84 | | - benchmark_tracer_wrap() |
85 | | - benchmark_tracer_trace() |
86 | | - benchmark_getpid() |
| 92 | + benchmark(func, tracer) |
0 commit comments