Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(profiling): more tests for libdd and stack_v2 #11679

Merged
merged 35 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
032363e
copy test_scheduler
taegyunkim Dec 11, 2024
994672f
test_profiler
taegyunkim Dec 11, 2024
d00e82a
test_main
taegyunkim Dec 11, 2024
511192e
Merge branch 'main' into taegyunkim/prof-10736-copy-tests
taegyunkim Dec 11, 2024
4456429
test_accuracy with libdd
taegyunkim Dec 11, 2024
1dcf6cc
test_accuracy with v2
taegyunkim Dec 11, 2024
25ca1ca
Merge branch 'main' into taegyunkim/prof-10736-copy-tests
taegyunkim Dec 11, 2024
51231c2
test_main with stackv2
taegyunkim Dec 11, 2024
2db4da2
test collect_truncate
taegyunkim Dec 11, 2024
5316839
test_ignore_profiler_gevent_task
taegyunkim Dec 11, 2024
99abe90
typo
taegyunkim Dec 11, 2024
490a2b0
fix test
taegyunkim Dec 11, 2024
d5814c6
Merge branch 'main' into taegyunkim/prof-10736-copy-tests
taegyunkim Dec 11, 2024
12f56f7
pass env directly instead of using monkeypatch
taegyunkim Dec 11, 2024
96b207c
Merge branch 'main' into taegyunkim/prof-10736-copy-tests
taegyunkim Dec 12, 2024
79009bf
skip 3.7 for stack_v2_enabled
taegyunkim Dec 12, 2024
47fbb41
skip 3.7 for stack_v2_enabled
taegyunkim Dec 12, 2024
1eaf699
skip 3.7 for stack_v2_enabled
taegyunkim Dec 12, 2024
222d3e7
proper skipif
taegyunkim Dec 12, 2024
6d7556b
proper skipif
taegyunkim Dec 12, 2024
2d2cf94
Merge branch 'main' into taegyunkim/prof-10736-copy-tests
taegyunkim Dec 17, 2024
3e68211
update almost_equal
taegyunkim Dec 19, 2024
017f545
Merge branch 'main' into taegyunkim/prof-10736-copy-tests
taegyunkim Dec 19, 2024
968abc1
Merge branch 'main' into taegyunkim/prof-10736-copy-tests
taegyunkim Dec 19, 2024
252f1d9
Merge branch 'main' into taegyunkim/prof-10736-copy-tests
taegyunkim Dec 19, 2024
6c61aa0
Merge branch 'main' into taegyunkim/prof-10736-copy-tests
taegyunkim Dec 19, 2024
bd1de30
use 5% tolerance
taegyunkim Dec 19, 2024
f9e7f36
remove import
taegyunkim Dec 19, 2024
3efc185
revert to monotonic_ns
taegyunkim Dec 19, 2024
ab7769b
process_time_ns
taegyunkim Dec 19, 2024
f5842b3
mark to skip
taegyunkim Dec 19, 2024
1cfc03d
adjust tolerance
taegyunkim Dec 19, 2024
819910b
Merge branch 'main' into taegyunkim/prof-10736-copy-tests
taegyunkim Dec 20, 2024
fdb2c30
fix failing test
taegyunkim Dec 20, 2024
e6750cc
increase tolerance
taegyunkim Dec 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions tests/profiling/test_accuracy.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,5 @@ def test_accuracy():
else:
assert almost_equal(total_time(time_spent_ns, "spend_cpu_2"), 2e9)
assert almost_equal(total_time(time_spent_ns, "spend_cpu_3"), 3e9)
assert almost_equal(total_time(time_spent_ns, "spend_cpu_2"), 2e9, CPU_TOLERANCE)
assert almost_equal(total_time(time_spent_ns, "spend_cpu_3"), 3e9, CPU_TOLERANCE)
assert almost_equal(total_time(cpu_spent_ns, "spend_cpu_2"), 2e9, CPU_TOLERANCE)
assert almost_equal(total_time(cpu_spent_ns, "spend_cpu_3"), 3e9, CPU_TOLERANCE)
31 changes: 31 additions & 0 deletions tests/profiling_v2/simple_program.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env python
import os
import sys
import time

from ddtrace.internal import service
from ddtrace.profiling import bootstrap
from ddtrace.profiling.collector import stack


for running_collector in bootstrap.profiler._profiler._collectors:
if isinstance(running_collector, stack.StackCollector):
break
else:
raise AssertionError("Unable to find stack collector")


print("hello world")
assert running_collector.status == service.ServiceStatus.RUNNING
print(running_collector.interval)

t0 = time.time()
while time.time() - t0 < (running_collector.interval * 10):
pass

# Do some serious memory allocations!
for _ in range(5000000):
object()

print(os.getpid())
sys.exit(42)
32 changes: 32 additions & 0 deletions tests/profiling_v2/simple_program_fork.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import os
import sys
import threading

from ddtrace.internal import service
import ddtrace.profiling.auto
import ddtrace.profiling.bootstrap
import ddtrace.profiling.profiler


lock = threading.Lock()
lock.acquire()


assert ddtrace.profiling.bootstrap.profiler.status == service.ServiceStatus.RUNNING


child_pid = os.fork()
if child_pid == 0:
# Release it
lock.release()

# We track this one though
lock = threading.Lock()
lock.acquire()
lock.release()
else:
lock.release()
assert ddtrace.profiling.bootstrap.profiler.status == service.ServiceStatus.RUNNING
print(child_pid)
pid, status = os.waitpid(child_pid, 0)
sys.exit(os.WEXITSTATUS(status))
34 changes: 34 additions & 0 deletions tests/profiling_v2/simple_program_gevent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Import from ddtrace before monkey patching to ensure that we grab all the
# necessary references to the unpatched modules.
import ddtrace.auto # noqa: F401, I001
import ddtrace.profiling.auto # noqa:F401


import gevent.monkey # noqa:F402

gevent.monkey.patch_all()

import threading # noqa: E402, F402, I001
import time # noqa: E402, F402


def fibonacci(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return fibonacci(n - 1) + fibonacci(n - 2)


i = 1
for _ in range(20):
threads = []
for _ in range(10):
t = threading.Thread(target=fibonacci, args=(i,))
t.start()
threads.append(t)
i += 1
for t in threads:
t.join()
time.sleep(0.1)
99 changes: 99 additions & 0 deletions tests/profiling_v2/test_accuracy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# -*- encoding: utf-8 -*-
import pytest


@pytest.mark.subprocess(
env=dict(DD_PROFILING_MAX_TIME_USAGE_PCT="100", DD_PROFILING_OUTPUT_PPROF="/tmp/test_accuracy_libdd.pprof")
)
def test_accuracy_libdd():
import collections
import os

from ddtrace.profiling import profiler
from tests.profiling.collector import pprof_utils
from tests.profiling.test_accuracy import CPU_TOLERANCE
from tests.profiling.test_accuracy import almost_equal
from tests.profiling.test_accuracy import spend_16

# Set this to 100 so we don't sleep too often and mess with the precision.
p = profiler.Profiler()
p.start()
spend_16()
p.stop()
wall_times = collections.defaultdict(lambda: 0)
cpu_times = collections.defaultdict(lambda: 0)
profile = pprof_utils.parse_profile(os.environ["DD_PROFILING_OUTPUT_PPROF"] + "." + str(os.getpid()))

for sample in profile.sample:
wall_time_index = pprof_utils.get_sample_type_index(profile, "wall-time")

wall_time_spent_ns = sample.value[wall_time_index]
cpu_time_index = pprof_utils.get_sample_type_index(profile, "cpu-time")
cpu_time_spent_ns = sample.value[cpu_time_index]

for location_id in sample.location_id:
location = pprof_utils.get_location_with_id(profile, location_id)
line = location.line[0]
function = pprof_utils.get_function_with_id(profile, line.function_id)
function_name = profile.string_table[function.name]
wall_times[function_name] += wall_time_spent_ns
cpu_times[function_name] += cpu_time_spent_ns

assert almost_equal(wall_times["spend_3"], 9e9)
assert almost_equal(wall_times["spend_1"], 2e9)
assert almost_equal(wall_times["spend_4"], 4e9)
assert almost_equal(wall_times["spend_16"], 16e9)
assert almost_equal(wall_times["spend_7"], 7e9)

assert almost_equal(wall_times["spend_cpu_2"], 2e9)
assert almost_equal(wall_times["spend_cpu_3"], 3e9)
assert almost_equal(cpu_times["spend_cpu_2"], 2e9, CPU_TOLERANCE)
taegyunkim marked this conversation as resolved.
Show resolved Hide resolved
assert almost_equal(cpu_times["spend_cpu_3"], 3e9, CPU_TOLERANCE)


@pytest.mark.subprocess(
env=dict(DD_PROFILING_STACK_V2_ENABLED="1", DD_PROFILING_OUTPUT_PPROF="/tmp/test_accuracy_stack_v2.pprof")
)
def test_accuracy_stack_v2():
import collections
import os

from ddtrace.profiling import profiler
from tests.profiling.collector import pprof_utils
from tests.profiling.test_accuracy import almost_equal
from tests.profiling.test_accuracy import spend_16

# Set this to 100 so we don't sleep too often and mess with the precision.
p = profiler.Profiler()
p.start()
spend_16()
p.stop()
wall_times = collections.defaultdict(lambda: 0)
cpu_times = collections.defaultdict(lambda: 0)
profile = pprof_utils.parse_profile(os.environ["DD_PROFILING_OUTPUT_PPROF"] + "." + str(os.getpid()))

for sample in profile.sample:
wall_time_index = pprof_utils.get_sample_type_index(profile, "wall-time")

wall_time_spent_ns = sample.value[wall_time_index]
cpu_time_index = pprof_utils.get_sample_type_index(profile, "cpu-time")
cpu_time_spent_ns = sample.value[cpu_time_index]

for location_id in sample.location_id:
location = pprof_utils.get_location_with_id(profile, location_id)
line = location.line[0]
function = pprof_utils.get_function_with_id(profile, line.function_id)
function_name = profile.string_table[function.name]
wall_times[function_name] += wall_time_spent_ns
cpu_times[function_name] += cpu_time_spent_ns

assert almost_equal(wall_times["spend_3"], 9e9)
assert almost_equal(wall_times["spend_1"], 2e9)
assert almost_equal(wall_times["spend_4"], 4e9)
assert almost_equal(wall_times["spend_16"], 16e9)
assert almost_equal(wall_times["spend_7"], 7e9)

assert almost_equal(wall_times["spend_cpu_2"], 2e9)
assert almost_equal(wall_times["spend_cpu_3"], 3e9)
assert almost_equal(cpu_times["spend_cpu_2"], 2e9)
assert almost_equal(cpu_times["spend_cpu_3"], 3e9)
Loading
Loading