fix(environment): CPU usage from two reads a second apart
get_environment promised two samples one second apart but read /proc/stat once, so the figure was the average since boot: a box at 100 % for the last minute showed its lifetime average. It now reads the cpu line twice, a second apart, and uses the difference. Closes #10
This commit is contained in:
1 parent
c211ba0d9e
commit
097d9b9581
3 files changed
+96
-11
No files matched your search
@@ -2005,3 +2005,61 @@ class TestManageService:
|
||||
with pytest.raises(ValueError):
|
||||
driver.manage_service("x; reboot", "start")
|
||||
assert sent == []
|
||||
|
||||
|
||||
# Busy 15 % on average since boot, 90 % in the second between the two reads.
|
||||
PROC_STAT_CPU_TWICE = """\
|
||||
cpu 1000 0 500 8500 0 0 0 0 0 0
|
||||
cpu 1090 0 500 8510 0 0 0 0 0 0
|
||||
"""
|
||||
|
||||
MEMINFO = """\
|
||||
MemTotal: 124608 kB
|
||||
MemFree: 43204 kB
|
||||
MemAvailable: 62000 kB
|
||||
"""
|
||||
|
||||
|
||||
class TestGetEnvironmentCpu:
|
||||
"""CPU usage is the second between two reads, not the average since boot (#10)."""
|
||||
|
||||
def _answer(self, driver, cpu: str) -> list:
|
||||
sent: list = []
|
||||
|
||||
def send(cmd, **kw):
|
||||
sent.append(cmd)
|
||||
return MEMINFO if "meminfo" in cmd else cpu
|
||||
|
||||
driver._send_command = send
|
||||
return sent
|
||||
|
||||
def test_usage_is_the_difference_of_two_reads(self, driver):
|
||||
self._answer(driver, PROC_STAT_CPU_TWICE)
|
||||
|
||||
assert driver.get_environment()["cpu"] == {0: {"%usage": 90.0}}
|
||||
|
||||
def test_the_two_reads_are_a_second_apart(self, driver):
|
||||
sent = self._answer(driver, PROC_STAT_CPU_TWICE)
|
||||
|
||||
driver.get_environment()
|
||||
|
||||
command = next(cmd for cmd in sent if "/proc/stat" in cmd)
|
||||
assert command.count("/proc/stat") == 2
|
||||
assert "sleep 1" in command
|
||||
|
||||
def test_counters_that_did_not_move_are_zero(self, driver):
|
||||
self._answer(driver, "cpu 1000 0 500 8500 0 0 0\ncpu 1000 0 500 8500 0 0 0\n")
|
||||
|
||||
assert driver.get_environment()["cpu"][0]["%usage"] == 0.0
|
||||
|
||||
def test_a_single_read_is_not_taken_for_usage(self, driver):
|
||||
self._answer(driver, "cpu 1000 0 500 8500 0 0 0\n")
|
||||
|
||||
assert driver.get_environment()["cpu"][0]["%usage"] == 0.0
|
||||
|
||||
def test_memory_is_still_read(self, driver):
|
||||
self._answer(driver, PROC_STAT_CPU_TWICE)
|
||||
|
||||
memory = driver.get_environment()["memory"]
|
||||
|
||||
assert memory == {"available_ram": 62000 * 1024, "used_ram": (124608 - 62000) * 1024}
|
||||
Reference in new issue
Block a user