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.