File tree Expand file tree Collapse file tree 2 files changed +55
-4
lines changed Expand file tree Collapse file tree 2 files changed +55
-4
lines changed Original file line number Diff line number Diff line change 1818"""
1919
2020import os
21- import time
2221
2322from newrelic .common .system_info import logical_processor_count
2423from newrelic .common .stopwatch import start_timer
@@ -48,14 +47,25 @@ def __call__(self):
4847 return
4948
5049 new_times = os .times ()
50+ elapsed_time = self ._timer .restart_timer ()
51+ elapsed_cpu_time = elapsed_time * logical_processor_count ()
52+
5153 user_time = new_times [0 ] - self ._times [0 ]
54+ user_utilization = user_time / elapsed_cpu_time
5255
53- elapsed_time = self ._timer .restart_timer ()
54- utilization = user_time / (elapsed_time * logical_processor_count ())
56+ system_time = new_times [1 ] - self ._times [1 ]
57+ system_utilization = system_time / elapsed_cpu_time
58+
59+ total_time = sum (new_times [0 :4 ]) - sum (self ._times [0 :4 ])
60+ total_utilization = total_time / elapsed_cpu_time
5561
5662 self ._times = new_times
5763
5864 yield ('CPU/User Time' , user_time )
59- yield ('CPU/User/Utilization' , utilization )
65+ yield ('CPU/User/Utilization' , user_utilization )
66+ yield ('CPU/System Time' , system_time )
67+ yield ('CPU/System/Utilization' , system_utilization )
68+ yield ('CPU/Total Time' , total_time )
69+ yield ('CPU/Total/Utilization' , total_utilization )
6070
6171cpu_usage_data_source = _CPUUsageDataSource
Original file line number Diff line number Diff line change 1+ # Copyright 2010 New Relic, Inc.
2+ #
3+ # Licensed under the Apache License, Version 2.0 (the "License");
4+ # you may not use this file except in compliance with the License.
5+ # You may obtain a copy of the License at
6+ #
7+ # http://www.apache.org/licenses/LICENSE-2.0
8+ #
9+ # Unless required by applicable law or agreed to in writing, software
10+ # distributed under the License is distributed on an "AS IS" BASIS,
11+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+ # See the License for the specific language governing permissions and
13+ # limitations under the License.
14+
15+ import pytest
16+ from newrelic .samplers .cpu_usage import cpu_usage_data_source
17+
18+
19+ @pytest .fixture
20+ def data_source ():
21+ sampler = cpu_usage_data_source (settings = ())["factory" ](environ = ())
22+ sampler .start ()
23+ yield sampler
24+ sampler .stop ()
25+
26+
27+ EXPECTED_METRICS = (
28+ "CPU/User Time" ,
29+ "CPU/User/Utilization" ,
30+ "CPU/System Time" ,
31+ "CPU/System/Utilization" ,
32+ "CPU/Total Time" ,
33+ "CPU/Total/Utilization" ,
34+ )
35+
36+
37+ def test_cpu_metrics_collection (data_source ):
38+ metrics_table = set (m [0 ] for m in (data_source () or ()))
39+
40+ for metric in EXPECTED_METRICS :
41+ assert metric in metrics_table
You can’t perform that action at this time.
0 commit comments