Commit 2063cfa7 authored by Jarrod Pas's avatar Jarrod Pas
Browse files

Improves statistics for network

parent 8651229a
......@@ -115,7 +115,23 @@ class Network:
@property
def stats(self):
"""Return statistics for the simulation."""
"""Return detailed statistics for the simulation."""
packets = [packet.stats for packet in self.packets]
nodes = [node.stats for node in self.nodes.values()]
stats = {
'sim-time': self.env.now,
'packets': packets,
'nodes': nodes,
}
stats.update(self._stats)
return stats
@property
def stats_summary(self):
"""Return summarized stats for the simulation."""
def gather(group):
"""Gather statistics from a group and return a dict of lists."""
stats = defaultdict(list)
......@@ -124,29 +140,23 @@ class Network:
stats[stat].append(value)
return dict(stats)
packet_stats = gather(self.packets)
node_stats = gather(self.nodes.values())
packets = gather(self.packets)
nodes = gather(self.nodes.values())
stats = {}
stats['sim-time'] = self.env.now
stats['broadcasts'] = sum(node_stats['broadcasts'])
stats['packets'] = len(self.packets)
delivered = list(filter(lambda r: r > 0, packet_stats['recieved']))
stats['delivered'] = len(delivered)
stats['delivery-ratio'] = stats['delivered'] / stats['packets']
stats['delivery-cost'] = stats['broadcasts'] / stats['delivered']
stats['delay'] = sum(packet_stats['delay']) / stats['delivered']
stats['recieved'] = len(packets['recieved'])
stats['duplicates'] = sum(packets['recieved']) - stats['recieved']
stats['delivery-ratio'] = stats['recieved'] / stats['packets']
stats['broadcasts'] = sum(nodes['broadcasts'])
stats['delivery-cost'] = stats['broadcasts'] / stats['recieved']
stats.update(self._stats)
return stats
class Packet:
"""An item to route through the network."""
......@@ -280,8 +290,8 @@ class Node:
'tick_rate': 1,
})
self.stats = defaultdict(int)
self.stats['broadcasts'] = 0
self._stats = defaultdict(int)
self._stats['broadcasts'] = 0
def start(self, network):
"""
......@@ -347,7 +357,7 @@ class Node:
if forwards:
delay = len(packet) / self.options['bandwidth']
yield env.timeout(delay)
self.stats['broadcasts'] += 1
self._stats['broadcasts'] += 1
for target, reason in forwards.items():
try:
......@@ -397,6 +407,10 @@ class Node:
if not self.buffer.add(packet):
raise Node.SendFailed('buffer full')
@property
def stats(self):
return dict(self._stats)
def __repr__(self):
"""Return representation."""
return '%s(**%r)' % (
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment