|
| 1 | +from ddtrace.internal.runtime.constants import CPU_PERCENT |
1 | 2 | from ddtrace.internal.runtime.constants import GC_COUNT_GEN0 |
2 | 3 | from ddtrace.internal.runtime.constants import GC_RUNTIME_METRICS |
| 4 | +from ddtrace.internal.runtime.constants import MEM_RSS |
3 | 5 | from ddtrace.internal.runtime.constants import PSUTIL_RUNTIME_METRICS |
| 6 | +from ddtrace.internal.runtime.constants import THREAD_COUNT |
4 | 7 | from ddtrace.internal.runtime.metric_collectors import GCRuntimeMetricCollector |
5 | 8 | from ddtrace.internal.runtime.metric_collectors import PSUtilRuntimeMetricCollector |
6 | 9 | from ddtrace.internal.runtime.metric_collectors import RuntimeMetricCollector |
@@ -28,6 +31,95 @@ def test_metrics(self): |
28 | 31 | for (key, value) in collector.collect(PSUTIL_RUNTIME_METRICS): |
29 | 32 | self.assertIsNotNone(value) |
30 | 33 |
|
| 34 | + def test_static_metrics(self): |
| 35 | + import os |
| 36 | + import threading |
| 37 | + import time |
| 38 | + |
| 39 | + from ddtrace.vendor import psutil |
| 40 | + |
| 41 | + # Something to bump CPU utilization |
| 42 | + def busy_wait(duration_ms): |
| 43 | + end_time = time.time() + (duration_ms / 1000.0) |
| 44 | + while time.time() < end_time: |
| 45 | + pass |
| 46 | + |
| 47 | + def get_metrics(): |
| 48 | + # need to waste a reading of psutil because some of its reading have |
| 49 | + # memory and need a previous state |
| 50 | + collector = PSUtilRuntimeMetricCollector() |
| 51 | + collector.collect_fn(None) # wasted |
| 52 | + proc = psutil.Process(os.getpid()) |
| 53 | + proc.cpu_percent() # wasted |
| 54 | + |
| 55 | + # Create some load. If the duration is too low, then it can cause |
| 56 | + # wildly different values between readings. |
| 57 | + busy_wait(50) |
| 58 | + |
| 59 | + runtime_metrics = dict(collector.collect_fn(None)) |
| 60 | + |
| 61 | + with proc.oneshot(): |
| 62 | + psutil_metrics = { |
| 63 | + CPU_PERCENT: proc.cpu_percent(), |
| 64 | + MEM_RSS: proc.memory_info().rss, |
| 65 | + THREAD_COUNT: proc.num_threads(), |
| 66 | + } |
| 67 | + return runtime_metrics, psutil_metrics |
| 68 | + |
| 69 | + def check_metrics(runtime_metrics, psutil_metrics): |
| 70 | + def within_threshold(a, b, epsilon): |
| 71 | + return abs(a - b) <= epsilon * max(abs(a), abs(b)) |
| 72 | + |
| 73 | + # Number of threads should be precise |
| 74 | + if psutil_metrics[THREAD_COUNT] != runtime_metrics[THREAD_COUNT]: |
| 75 | + return False |
| 76 | + |
| 77 | + # CPU and RAM should be approximate. These tests are checking that the category of |
| 78 | + # the value is correct, rather than the specific value itself. |
| 79 | + epsilon = 0.25 |
| 80 | + if not within_threshold(psutil_metrics[CPU_PERCENT], runtime_metrics[CPU_PERCENT], epsilon): |
| 81 | + return False |
| 82 | + |
| 83 | + if not within_threshold(psutil_metrics[MEM_RSS], runtime_metrics[MEM_RSS], epsilon): |
| 84 | + return False |
| 85 | + |
| 86 | + return True |
| 87 | + |
| 88 | + # Sanity-check that the num_threads comparison works |
| 89 | + rt_metrics, pu_metrics = get_metrics() |
| 90 | + pu_metrics[THREAD_COUNT] += 1 |
| 91 | + self.assertFalse(check_metrics(rt_metrics, pu_metrics)) |
| 92 | + |
| 93 | + # Check that the CPU comparison works |
| 94 | + rt_metrics, pu_metrics = get_metrics() |
| 95 | + pu_metrics[CPU_PERCENT] *= 2 |
| 96 | + self.assertFalse(check_metrics(rt_metrics, pu_metrics)) |
| 97 | + |
| 98 | + # Check that the memory comparison works |
| 99 | + rt_metrics, pu_metrics = get_metrics() |
| 100 | + pu_metrics[MEM_RSS] *= 2 |
| 101 | + self.assertFalse(check_metrics(rt_metrics, pu_metrics)) |
| 102 | + |
| 103 | + # Baseline check |
| 104 | + self.assertTrue(check_metrics(*get_metrics())) |
| 105 | + |
| 106 | + # Check for threads. Rather than using a sleep() which might be brittle in CI, use an explicit |
| 107 | + # semaphore as a stop condition per thread. |
| 108 | + def thread_stopper(stop_event): |
| 109 | + stop_event.wait() |
| 110 | + |
| 111 | + stop_event = threading.Event() |
| 112 | + threads = [threading.Thread(target=thread_stopper, args=(stop_event,)) for _ in range(10)] |
| 113 | + _ = [thread.start() for thread in threads] |
| 114 | + self.assertTrue(check_metrics(*get_metrics())) |
| 115 | + stop_event.set() |
| 116 | + _ = [thread.join() for thread in threads] |
| 117 | + |
| 118 | + # Check for RSS |
| 119 | + wasted_memory = [" "] * 16 * 1024 ** 2 # 16 megs |
| 120 | + self.assertTrue(check_metrics(*get_metrics())) |
| 121 | + del wasted_memory |
| 122 | + |
31 | 123 |
|
32 | 124 | class TestGCRuntimeMetricCollector(BaseTestCase): |
33 | 125 | def test_metrics(self): |
|
0 commit comments