diff --git a/pythonping/executor.py b/pythonping/executor.py index 50ab95c..58bdcf3 100644 --- a/pythonping/executor.py +++ b/pythonping/executor.py @@ -172,7 +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 + self.stats_packets_sent = 0 + self.stats_packets_returned = 0 for response in initial_set: self.append(response) @@ -212,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 @@ -226,12 +231,23 @@ 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 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) 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', 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,