Skip to content

Commit

Permalink
Fix/#56 (#57)
Browse files Browse the repository at this point in the history
* Fix - #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.
  • Loading branch information
js-dieu committed May 18, 2022
1 parent 8e0fe52 commit 811b487
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
4 changes: 4 additions & 0 deletions docs/sources/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
Changelog
=========

* :release:`1.6.4 <2022-05-18>`
* :bug:`#56` Force the CPU frequency to 0 and emit a warning when unable to fetch it from the system.
* :bug:`#54` Fix a bug that crashes the monitor upon non ASCII characters in commit log under Perforce. Improved P4 change number extraction.

* :release:`1.6.3 <2021-12-22>`
* :bug:`#50` Fix a bug where a skipping fixture resulted in an exception during teardown.

Expand Down
2 changes: 1 addition & 1 deletion docs/sources/operating.rst
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ session. An Execution Context describes much of the machine settings:
CPU_COUNT (integer)
Number of online CPUs the machine can use.
CPU_FREQUENCY_MHZ (integer)
Base frequency of the CPUs (in megahertz).
Base frequency of the CPUs (in megahertz). Set to 0 if unable to fetch it.
CPU_VENDOR (TEXT 256 CHAR)
Full CPU vendor string.
RAM_TOTAL_MB (INTEGER)
Expand Down
7 changes: 6 additions & 1 deletion pytest_monitor/sys_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import socket
import subprocess
import sys
import warnings


def collect_ci_info():
Expand Down Expand Up @@ -67,7 +68,11 @@ class ExecutionContext:
def __init__(self):
self.__cpu_count = multiprocessing.cpu_count()
self.__cpu_vendor = _get_cpu_string()
self.__cpu_freq_base = psutil.cpu_freq().current
try:
self.__cpu_freq_base = psutil.cpu_freq().current
except AttributeError:
warnings.warn("Unable to fetch CPU frequency. Forcing it to 0.")
self.__cpu_freq_base = 0
self.__proc_typ = platform.processor()
self.__tot_mem = int(psutil.virtual_memory().total / 1024**2)
self.__fqdn = socket.getfqdn()
Expand Down
42 changes: 42 additions & 0 deletions tests/test_monitor_context.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import mock
import pathlib
import sqlite3


@mock.patch('pytest_monitor.sys_utils.psutil.cpu_freq', return_value=None)
def test_when_cpu_freq_cannot_fetch_frequency(cpu_freq_mock, testdir):
"""Make sure that pytest-monitor does the job when we have issue in collecing context resources"""
# create a temporary pytest test module
testdir.makepyfile("""
import time
def test_ok():
time.sleep(0.5)
x = ['a' * i for i in range(100)]
assert len(x) == 100
""")

# run pytest with the following cmd args
result = testdir.runpytest('-vv')

# fnmatch_lines does an assertion internally
result.stdout.fnmatch_lines(['*::test_ok PASSED*'])

pymon_path = pathlib.Path(str(testdir)) / '.pymon'
assert pymon_path.exists()

# make sure that that we get a '0' exit code for the test suite
result.assert_outcomes(passed=1)

db = sqlite3.connect(str(pymon_path))
cursor = db.cursor()
cursor.execute('SELECT ITEM FROM TEST_METRICS;')
assert 1 == len(cursor.fetchall()) # current test
cursor = db.cursor()
cursor.execute('SELECT CPU_FREQUENCY_MHZ FROM EXECUTION_CONTEXTS;')
rows = cursor.fetchall()
assert 1 == len(rows)
assert rows[0][0] == 0

0 comments on commit 811b487

Please sign in to comment.