Skip to content

Commit 48830c3

Browse files
committed
See more detailed CPu measurements
1 parent d3d5c03 commit 48830c3

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/crawlee/_utils/system.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,10 @@ def get_cpu_info() -> CpuInfo:
9393
It utilizes the `psutil` library. Function `psutil.cpu_percent()` returns a float representing the current
9494
system-wide CPU utilization as a percentage.
9595
"""
96-
logger.debug('Calling get_cpu_info()...')
97-
cpu_percent = psutil.cpu_percent(interval=0.1)
98-
return CpuInfo(used_ratio=cpu_percent / 100)
96+
97+
cpu_percent = psutil.cpu_percent(percpu=True)
98+
logger.info(f'Calling get_cpu_info()...: {cpu_percent}')
99+
return CpuInfo(used_ratio=sum(cpu_percent)/len(cpu_percent) / 100)
99100

100101

101102
def get_memory_info() -> MemoryInfo:

tests/unit/crawlers/_basic/test_basic_crawler.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
from dataclasses import dataclass
1313
from datetime import timedelta
1414
from itertools import product
15+
from subprocess import run
1516
from typing import TYPE_CHECKING, Any, Literal, cast
1617
from unittest.mock import AsyncMock, Mock, call, patch
1718

19+
import psutil
1820
import pytest
1921

2022
from crawlee import ConcurrencySettings, Glob, service_locator
@@ -1290,6 +1292,13 @@ async def test_timeout_in_handler(sleep_type: str, _) -> None:
12901292
# Test is skipped in older Python versions.
12911293
from asyncio import timeout # type:ignore[attr-defined] # noqa: PLC0415
12921294

1295+
# Debug CPu usage before starting the test
1296+
run("ps -ewfaxo comm,user,pid,%cpu,cmd", shell=True)
1297+
1298+
run("ps -awxo pid,%cpu,comm", shell=True)
1299+
1300+
1301+
12931302
handler_timeout = timedelta(seconds=1)
12941303
max_request_retries = 3
12951304
double_handler_timeout_s = handler_timeout.total_seconds() * 2
@@ -1301,6 +1310,8 @@ async def test_timeout_in_handler(sleep_type: str, _) -> None:
13011310
max_request_retries=max_request_retries,
13021311
)
13031312
crawler.log.setLevel(logging.DEBUG)
1313+
1314+
crawler.log.info(f'Calling get_cpu_info()...: {psutil.cpu_percent(percpu=True)}')
13041315
logging.getLogger('crawlee.storage_clients._file_system._request_queue_client').setLevel(logging.DEBUG)
13051316
logging.getLogger('crawlee._autoscaling.autoscaled_pool').setLevel(logging.INFO)
13061317

0 commit comments

Comments
 (0)