Skip to content

Commit

Permalink
Merge pull request #83 from ilyapashuk/master
Browse files Browse the repository at this point in the history
added a variable to hold the number of lost packets
  • Loading branch information
alessandromaggio authored Aug 19, 2022
2 parents 03ad4ae + 3f29e3a commit 2112eb5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
19 changes: 15 additions & 4 deletions pythonping/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand All @@ -226,12 +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 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)

Expand Down
17 changes: 10 additions & 7 deletions test/test_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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"
)
Expand All @@ -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"
)
Expand All @@ -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,
Expand Down

0 comments on commit 2112eb5

Please sign in to comment.