From 521c732dbc29725ce00f8138dd6bc65590157ab9 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sun, 5 Apr 2026 02:34:59 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Add=20-q=20flag=20to=20ping?= =?UTF-8?q?=20command=20to=20reduce=20subprocess=20CPU=20overhead?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: ManupaKDU <95234271+ManupaKDU@users.noreply.github.com> --- test_testping1.py | 2 +- testping1.py | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/test_testping1.py b/test_testping1.py index c1441cb..6ec4077 100644 --- a/test_testping1.py +++ b/test_testping1.py @@ -146,7 +146,7 @@ def test_is_reachable_calls_ping_correctly(self, mock_call): is_reachable('192.168.1.1', timeout=5) # Verify that subprocess.call was called with the correct arguments, including the timeout mock_call.assert_called_once_with( - [PING_PATH, '-n', '-c', '1', '-W', '5', '192.168.1.1'], + [PING_PATH, '-n', '-q', '-c', '1', '-W', '5', '192.168.1.1'], stdout=DEVNULL_FD, stderr=DEVNULL_FD, close_fds=False, timeout=7 ) diff --git a/testping1.py b/testping1.py index 54205c7..f07c8f2 100644 --- a/testping1.py +++ b/testping1.py @@ -85,11 +85,15 @@ def is_reachable(ip, timeout=1): logging.error(f"Invalid timeout value: {repr(timeout)}") return False - # ⚡ Bolt: Optimized ping execution by adding `-n` flag. + # ⚡ Bolt: Optimized ping execution by adding `-n` and `-q` flags. # The `-n` flag skips reverse DNS resolution. Without it, ping attempts to # resolve the hostname for every IP, which can cause multi-second delays # (even with a 1s timeout) if the IP lacks a PTR record or DNS is unresponsive. - command = [PING_PATH, "-n", "-c", "1", "-W", str(timeout_val), str(ip_obj)] # -W for timeout in seconds (Linux) + # The `-q` (quiet) flag suppresses output generation. This prevents the `ping` + # binary from allocating and formatting string output for every ICMP echo reply + # it receives, slightly reducing CPU usage on the host OS when firing thousands + # of concurrent pings. + command = [PING_PATH, "-n", "-q", "-c", "1", "-W", str(timeout_val), str(ip_obj)] # -W for timeout in seconds (Linux) # ⚡ Bolt: Optimized ping execution by using subprocess.call and redirecting # output to DEVNULL instead of using Popen with PIPE.