Normalize CPU stats (#2154)

* Normalize CPU stats

* Change fixture to make it clearer

* guard for 0

* Update supervisor/docker/stats.py

Co-authored-by: Pascal Vizeli <pvizeli@syshack.ch>

* Update stats.py

* Update stats.py

Co-authored-by: Pascal Vizeli <pvizeli@syshack.ch>
This commit is contained in:
Joakim Sørensen 2020-10-20 22:26:09 +02:00 committed by GitHub
parent 1a59839b1b
commit b513512551
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 4 deletions

View File

@ -45,12 +45,11 @@ class DockerStats:
stats["cpu_stats"]["system_cpu_usage"] stats["cpu_stats"]["system_cpu_usage"]
- stats["precpu_stats"]["system_cpu_usage"] - stats["precpu_stats"]["system_cpu_usage"]
) )
online_cpu = stats["cpu_stats"]["online_cpus"]
if online_cpu == 0.0:
online_cpu = len(stats["cpu_stats"]["cpu_usage"]["percpu_usage"])
if system_delta > 0.0 and cpu_delta > 0.0: if system_delta > 0.0 and cpu_delta > 0.0:
self._cpu = (cpu_delta / system_delta) * online_cpu * 100.0 self._cpu = (cpu_delta / system_delta) * 100.0
else:
self._cpu = 0.0
def _calc_network(self, networks): def _calc_network(self, networks):
"""Calculate Network IO stats.""" """Calculate Network IO stats."""

View File

@ -0,0 +1,20 @@
"""Test docker stats."""
from supervisor.docker.stats import DockerStats
from tests.common import load_json_fixture
def test_cpu_presentage(docker):
"""Test CPU presentage."""
stats_fixtrue = load_json_fixture("container_stats.json")
stats = DockerStats(stats_fixtrue)
stats._calc_cpu_percent(stats_fixtrue) # pylint: disable=protected-access
assert stats.cpu_percent == 90.0
stats_fixtrue["cpu_stats"]["cpu_usage"]["total_usage"] = 0
stats_fixtrue["precpu_stats"]["cpu_usage"]["total_usage"] = 0
stats_fixtrue["cpu_stats"]["system_cpu_usage"] = 0
stats_fixtrue["precpu_stats"]["system_cpu_usage"] = 0
stats._calc_cpu_percent(stats_fixtrue) # pylint: disable=protected-access
assert stats.cpu_percent == 0.0

20
tests/fixtures/container_stats.json vendored Normal file
View File

@ -0,0 +1,20 @@
{
"cpu_stats": {
"cpu_usage": {
"total_usage": 190
},
"system_cpu_usage": 200,
"online_cpus": 24
},
"precpu_stats": {
"cpu_usage": {
"total_usage": 100
},
"system_cpu_usage": 100,
"online_cpus": 24
},
"memory_stats": {
"usage": 250000000,
"limit": 330000000000
}
}