From db176845a2f4d9d17d0aa5aec44e7d45158734f0 Mon Sep 17 00:00:00 2001 From: ilyapashuk Date: Sat, 25 Jun 2022 18:25:07 +0300 Subject: [PATCH 1/4] added a variable to hold the number of lost packets this commits adds a "packets_lost_number" variable to a ResponseList object. this variable holds an integer number of the lost packets. --- pythonping/executor.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pythonping/executor.py b/pythonping/executor.py index 7e42001..b57a896 100644 --- a/pythonping/executor.py +++ b/pythonping/executor.py @@ -173,6 +173,8 @@ def __init__(self, initial_set=[], verbose=False, output=sys.stdout): self.rtt_min = 0 self.rtt_max = 0 self.packets_lost = 0 + # this variable holds int number of lost packets + self.packets_lost_number = 0 for response in initial_set: self.append(response) @@ -228,6 +230,7 @@ def append(self, value): self.rtt_min = value.time_elapsed self.packets_lost = self.packets_lost + ((0 if value.success else 1) - self.packets_lost) / len(self) + if not value.success: self.packets_lost_number += 1 if self.verbose: print(value, file=self.output) From 9156dc3ec86f53e9f12b76b6839a186f3a8d24b9 Mon Sep 17 00:00:00 2001 From: ilyapashuk Date: Fri, 29 Jul 2022 15:39:55 +0300 Subject: [PATCH 2/4] added stats variables for packets --- pythonping/executor.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/pythonping/executor.py b/pythonping/executor.py index b57a896..dc9544a 100644 --- a/pythonping/executor.py +++ b/pythonping/executor.py @@ -172,9 +172,8 @@ def __init__(self, initial_set=[], verbose=False, output=sys.stdout): self.rtt_avg = 0 self.rtt_min = 0 self.rtt_max = 0 - self.packets_lost = 0 - # this variable holds int number of lost packets - self.packets_lost_number = 0 + self.stats_packets_sent = 0 + self.stats_packets_returned = 0 for response in initial_set: self.append(response) @@ -214,9 +213,13 @@ def rtt_avg_ms(self): def clear(self): self._responses = [] + self.stats_packets_sent = 0 + self.stats_packets_returned = 0 + def append(self, value): self._responses.append(value) + self.stats_packets_sent += 1 if len(self) == 1: self.rtt_avg = value.time_elapsed self.rtt_max = value.time_elapsed @@ -228,13 +231,18 @@ def append(self, value): self.rtt_max = value.time_elapsed if value.time_elapsed < self.rtt_min: self.rtt_min = value.time_elapsed - - self.packets_lost = self.packets_lost + ((0 if value.success else 1) - self.packets_lost) / len(self) - if not value.success: self.packets_lost_number += 1 + if value.success: self.stats_packets_returned += 1 if self.verbose: print(value, file=self.output) - + @property + def stats_packets_lost(self): return self.stats_packets_sent - self.stats_packets_returned + @property + def stats_success_ratio(self): return self.stats_packets_returned / self.stats_packets_sent + @property + def stats_lost_ratio(self): return 1 - self.stats_success_ratio + @property + def packets_lost(self): return self.stats_lost_ratio def __len__(self): return len(self._responses) From abc4fc567c8c271e85a96a4627ee4878ee71a7de Mon Sep 17 00:00:00 2001 From: ilyapashuk Date: Sun, 31 Jul 2022 13:43:22 +0300 Subject: [PATCH 3/4] added tests for changes in ResponseList object --- test/test_executor.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/test/test_executor.py b/test/test_executor.py index ac8eef7..cfa16d6 100644 --- a/test/test_executor.py +++ b/test/test_executor.py @@ -255,9 +255,10 @@ def test_no_packets_lost(self): SuccessfulResponseMock(None, 1) ]) + self.assertEqual(rs.stats_packets_sent, rs.stats_packets_returned, 'unable to correctly count sent and returned packets when all responses successful') self.assertEqual( - rs.packet_loss, - 0.0, + rs.stats_packets_lost, + 0, "Unable to calculate packet loss correctly when all responses successful" ) @@ -268,9 +269,9 @@ def test_all_packets_lost(self): FailingResponseMock(None, 1), FailingResponseMock(None, 1) ]) - + self.assertEqual(rs.stats_packets_returned, 0, 'unable to correctly count sent and returned packets when all responses failed') self.assertEqual( - rs.packet_loss, + rs.stats_lost_ratio, 1.0, "Unable to calculate packet loss correctly when all responses failed" ) @@ -282,9 +283,10 @@ def test_some_packets_lost(self): FailingResponseMock(None, 1), FailingResponseMock(None, 1) ]) - + self.assertEqual(rs.stats_packets_sent, 4, 'unable to correctly count sent packets when some of the responses failed') + self.assertEqual(rs.stats_packets_returned, 2, 'unable to correctly count returned packets when some of the responses failed') self.assertEqual( - rs.packet_loss, + rs.stats_lost_ratio, 0.5, "Unable to calculate packet loss correctly when some of the responses failed" ) @@ -296,7 +298,8 @@ def test_some_packets_lost_mixed(self): FailingResponseMock(None, 1), SuccessfulResponseMock(None, 1), ]) - + self.assertEqual(rs.stats_packets_sent, 4, 'unable to correctly count sent packets when when failing responses are mixed with successful responses') + self.assertEqual(rs.stats_packets_returned, 2, 'unable to correctly count returned packets when failing responses are mixed with successful responses') self.assertEqual( rs.packet_loss, 0.5, From 44e76d4b0e5489442d7b03020472d7bbe676ea92 Mon Sep 17 00:00:00 2001 From: Alessandro Maggio Date: Fri, 19 Aug 2022 15:11:42 +0200 Subject: [PATCH 4/4] feat: Add stats properties --- pythonping/executor.py | 5 +++++ setup.py | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/pythonping/executor.py b/pythonping/executor.py index 8498bc5..58bdcf3 100644 --- a/pythonping/executor.py +++ b/pythonping/executor.py @@ -235,14 +235,19 @@ def append(self, value): if self.verbose: print(value, file=self.output) + @property def stats_packets_lost(self): return self.stats_packets_sent - self.stats_packets_returned + @property def stats_success_ratio(self): return self.stats_packets_returned / self.stats_packets_sent + @property def stats_lost_ratio(self): return 1 - self.stats_success_ratio + @property def packets_lost(self): return self.stats_lost_ratio + def __len__(self): return len(self._responses) diff --git a/setup.py b/setup.py index 977cd45..e460001 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ long_description = file.read() setup(name='pythonping', - version='1.1.2', + version='1.1.3', description='A simple way to ping in Python', url='https://github.com/alessandromaggio/pythonping', author='Alessandro Maggio',