Skip to content

Commit 811b487

Browse files
authored
* Fix - CFMTech#56: Force CPU frequency to 0 when unable to fetch it. On some system, it appears to not be feasible to fetch the frequency. Under such circumstances, the CPU frequency is set to 0 and a warning is emited.
1 parent 8e0fe52 commit 811b487

File tree

4 files changed

+53
-2
lines changed

4 files changed

+53
-2
lines changed

docs/sources/changelog.rst

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
Changelog
33
=========
44

5+
* :release:`1.6.4 <2022-05-18>`
6+
* :bug:`#56` Force the CPU frequency to 0 and emit a warning when unable to fetch it from the system.
7+
* :bug:`#54` Fix a bug that crashes the monitor upon non ASCII characters in commit log under Perforce. Improved P4 change number extraction.
8+
59
* :release:`1.6.3 <2021-12-22>`
610
* :bug:`#50` Fix a bug where a skipping fixture resulted in an exception during teardown.
711

docs/sources/operating.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ session. An Execution Context describes much of the machine settings:
5858
CPU_COUNT (integer)
5959
Number of online CPUs the machine can use.
6060
CPU_FREQUENCY_MHZ (integer)
61-
Base frequency of the CPUs (in megahertz).
61+
Base frequency of the CPUs (in megahertz). Set to 0 if unable to fetch it.
6262
CPU_VENDOR (TEXT 256 CHAR)
6363
Full CPU vendor string.
6464
RAM_TOTAL_MB (INTEGER)

pytest_monitor/sys_utils.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import socket
77
import subprocess
88
import sys
9+
import warnings
910

1011

1112
def collect_ci_info():
@@ -67,7 +68,11 @@ class ExecutionContext:
6768
def __init__(self):
6869
self.__cpu_count = multiprocessing.cpu_count()
6970
self.__cpu_vendor = _get_cpu_string()
70-
self.__cpu_freq_base = psutil.cpu_freq().current
71+
try:
72+
self.__cpu_freq_base = psutil.cpu_freq().current
73+
except AttributeError:
74+
warnings.warn("Unable to fetch CPU frequency. Forcing it to 0.")
75+
self.__cpu_freq_base = 0
7176
self.__proc_typ = platform.processor()
7277
self.__tot_mem = int(psutil.virtual_memory().total / 1024**2)
7378
self.__fqdn = socket.getfqdn()

tests/test_monitor_context.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import mock
2+
import pathlib
3+
import sqlite3
4+
5+
6+
@mock.patch('pytest_monitor.sys_utils.psutil.cpu_freq', return_value=None)
7+
def test_when_cpu_freq_cannot_fetch_frequency(cpu_freq_mock, testdir):
8+
"""Make sure that pytest-monitor does the job when we have issue in collecing context resources"""
9+
# create a temporary pytest test module
10+
testdir.makepyfile("""
11+
import time
12+
13+
14+
def test_ok():
15+
time.sleep(0.5)
16+
x = ['a' * i for i in range(100)]
17+
assert len(x) == 100
18+
19+
""")
20+
21+
# run pytest with the following cmd args
22+
result = testdir.runpytest('-vv')
23+
24+
# fnmatch_lines does an assertion internally
25+
result.stdout.fnmatch_lines(['*::test_ok PASSED*'])
26+
27+
pymon_path = pathlib.Path(str(testdir)) / '.pymon'
28+
assert pymon_path.exists()
29+
30+
# make sure that that we get a '0' exit code for the test suite
31+
result.assert_outcomes(passed=1)
32+
33+
db = sqlite3.connect(str(pymon_path))
34+
cursor = db.cursor()
35+
cursor.execute('SELECT ITEM FROM TEST_METRICS;')
36+
assert 1 == len(cursor.fetchall()) # current test
37+
cursor = db.cursor()
38+
cursor.execute('SELECT CPU_FREQUENCY_MHZ FROM EXECUTION_CONTEXTS;')
39+
rows = cursor.fetchall()
40+
assert 1 == len(rows)
41+
assert rows[0][0] == 0
42+

0 commit comments

Comments
 (0)