2121
2222from newrelic .core .config import global_settings
2323from newrelic .packages import six
24+ from newrelic .samplers .cpu_usage import cpu_usage_data_source
2425from newrelic .samplers .gc_data import garbage_collector_data_source
26+ from newrelic .samplers .memory_usage import memory_usage_data_source
2527
2628settings = global_settings ()
2729
2830
2931@pytest .fixture
30- def data_source ():
32+ def gc_data_source ():
3133 sampler = garbage_collector_data_source (settings = ())["factory" ](environ = ())
3234 sampler .start ()
3335 yield sampler
3436 sampler .stop ()
3537
3638
39+ @pytest .fixture
40+ def cpu_data_source ():
41+ sampler = cpu_usage_data_source (settings = ())["factory" ](environ = ())
42+ sampler .start ()
43+ yield sampler
44+ sampler .stop ()
45+
46+
47+ @pytest .fixture
48+ def memory_data_source ():
49+ sampler = memory_usage_data_source (settings = ())["factory" ](environ = ())
50+ yield sampler
51+
52+
3753PID = os .getpid ()
3854
3955if six .PY2 :
40- EXPECTED_METRICS = (
56+ EXPECTED_GC_METRICS = (
4157 "GC/objects/%d/all" % PID ,
4258 "GC/objects/%d/generation/0" % PID ,
4359 "GC/objects/%d/generation/1" % PID ,
4460 "GC/objects/%d/generation/2" % PID ,
4561 )
4662else :
47- EXPECTED_METRICS = (
63+ EXPECTED_GC_METRICS = (
4864 "GC/objects/%d/all" % PID ,
4965 "GC/objects/%d/generation/0" % PID ,
5066 "GC/objects/%d/generation/1" % PID ,
@@ -75,7 +91,7 @@ def data_source():
7591 raises = AssertionError ,
7692)
7793@pytest .mark .parametrize ("top_object_count_limit" , (1 , 0 ))
78- def test_gc_metrics_collection (data_source , top_object_count_limit ):
94+ def test_gc_metrics_collection (gc_data_source , top_object_count_limit ):
7995 @override_generic_settings (
8096 settings ,
8197 {
@@ -85,9 +101,9 @@ def test_gc_metrics_collection(data_source, top_object_count_limit):
85101 )
86102 def _test ():
87103 gc .collect ()
88- metrics_table = set (m [0 ] for m in (data_source () or ()))
104+ metrics_table = set (m [0 ] for m in (gc_data_source () or ()))
89105
90- for metric in EXPECTED_METRICS :
106+ for metric in EXPECTED_GC_METRICS :
91107 assert metric in metrics_table
92108
93109 # Verify object count by type metrics are recorded
@@ -109,9 +125,39 @@ def _test():
109125 reason = "GC Metrics are always disabled on PyPy" ,
110126)
111127@pytest .mark .parametrize ("enabled" , (True , False ))
112- def test_gc_metrics_config (data_source , enabled ):
128+ def test_gc_metrics_config (gc_data_source , enabled ):
113129 @override_generic_settings (settings , {"gc_profiler.enabled" : enabled })
114130 def _test ():
115- assert data_source .enabled == enabled
131+ assert gc_data_source .enabled == enabled
116132
117133 _test ()
134+
135+
136+ EXPECTED_CPU_METRICS = (
137+ "CPU/User Time" ,
138+ "CPU/User/Utilization" ,
139+ "CPU/System Time" ,
140+ "CPU/System/Utilization" ,
141+ "CPU/Total Time" ,
142+ "CPU/Total/Utilization" ,
143+ )
144+
145+
146+ def test_cpu_metrics_collection (cpu_data_source ):
147+ metrics_table = set (m [0 ] for m in (cpu_data_source () or ()))
148+
149+ for metric in EXPECTED_CPU_METRICS :
150+ assert metric in metrics_table
151+
152+
153+ EXPECTED_MEMORY_METRICS = (
154+ "Memory/Physical/%d" % PID ,
155+ "Memory/Physical/Utilization/%d" % PID ,
156+ )
157+
158+
159+ def test_memory_metrics_collection (memory_data_source ):
160+ metrics_table = set (m [0 ] for m in (memory_data_source () or ()))
161+
162+ for metric in EXPECTED_MEMORY_METRICS :
163+ assert metric in metrics_table
0 commit comments