|
| 1 | +import concurrent.futures |
| 2 | +import random |
| 3 | +from typing import Callable |
| 4 | +from typing import Generator |
| 5 | +from typing import List |
| 6 | +from typing import Optional |
| 7 | + |
| 8 | +import bm |
| 9 | + |
| 10 | +from ddtrace.internal.writer import TraceWriter |
| 11 | +from ddtrace.span import Span |
| 12 | +from ddtrace.tracer import Tracer |
| 13 | + |
| 14 | + |
| 15 | +class NoopWriter(TraceWriter): |
| 16 | + def recreate(self): |
| 17 | + # type: () -> TraceWriter |
| 18 | + return NoopWriter() |
| 19 | + |
| 20 | + def stop(self, timeout=None): |
| 21 | + # type: (Optional[float]) -> None |
| 22 | + pass |
| 23 | + |
| 24 | + def write(self, spans=None): |
| 25 | + # type: (Optional[List[Span]]) -> None |
| 26 | + pass |
| 27 | + |
| 28 | + |
| 29 | +@bm.register |
| 30 | +class Threading(bm.Scenario): |
| 31 | + nthreads = bm.var(type=int) |
| 32 | + ntraces = bm.var(type=int) |
| 33 | + nspans = bm.var(type=int) |
| 34 | + |
| 35 | + def create_trace(self, tracer): |
| 36 | + # type: (Tracer) -> None |
| 37 | + with tracer.trace("root"): |
| 38 | + for _ in range(self.nspans - 1): |
| 39 | + with tracer.trace("child"): |
| 40 | + # Simulate work in each child |
| 41 | + random.random() |
| 42 | + |
| 43 | + def run(self): |
| 44 | + # type: () -> Generator[Callable[[int], None], None, None] |
| 45 | + from ddtrace import tracer |
| 46 | + |
| 47 | + # configure global tracer to drop traces rather |
| 48 | + tracer.configure(writer=NoopWriter()) |
| 49 | + |
| 50 | + def _(loops): |
| 51 | + # type: (int) -> None |
| 52 | + for _ in range(loops): |
| 53 | + with concurrent.futures.ThreadPoolExecutor(max_workers=self.nthreads) as executor: |
| 54 | + tasks = {executor.submit(self.create_trace, tracer) for i in range(self.ntraces)} |
| 55 | + for task in concurrent.futures.as_completed(tasks): |
| 56 | + task.result() |
| 57 | + |
| 58 | + yield _ |
0 commit comments