mirror of
https://github.com/home-assistant/supervisor.git
synced 2025-07-19 07:06:30 +00:00
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:
parent
1a59839b1b
commit
b513512551
@ -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."""
|
||||||
|
20
tests/docker/test_stats.py
Normal file
20
tests/docker/test_stats.py
Normal 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
20
tests/fixtures/container_stats.json
vendored
Normal 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
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user