Skip to content

Commit

Permalink
Fix incorrect Delta Metric calculation
Browse files Browse the repository at this point in the history
Problem:
Delta metrics were not calculated properly because the only the delta value of network stats was persisted in a metrics object.

Solution:
Introduce a second field to store the aggregate network stats, and use that value in delta calculation

Testing:
Introduce a 3rd metric to the delta metrics test to ensure that the delta is not used in subsequent calculations.
  • Loading branch information
da-miller committed Nov 9, 2018
1 parent 698678d commit 31a65f3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
12 changes: 10 additions & 2 deletions AWSIoTDeviceDefenderAgentSDK/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,12 @@ def __init__(self, short_names=False, last_metric=None):
self.listening_udp_ports = []

# Network Stats By Interface
self._interface_stats = {}
self.total_counts = {} # The raw values from the system
self._interface_stats = {} # The diff values, if delta metrics are used
if last_metric is None:
self._old_interface_stats = {}
else:
self._old_interface_stats = last_metric._interface_stats
self._old_interface_stats = last_metric.total_counts

self.max_list_size = 50

Expand Down Expand Up @@ -121,6 +122,13 @@ def add_network_stats(self, bytes_in, packets_in, bytes_out, packets_out):
packets_out: int
Number of packets sent from this interface
"""
self.total_counts = {
'bytes_in': bytes_in,
'bytes_out': bytes_out,
'packets_in': packets_in,
'packets_out': packets_out
}

if self._old_interface_stats:
bytes_in_diff = bytes_in - self._old_interface_stats[self.t.bytes_in]
bytes_out_diff = bytes_out - self._old_interface_stats[self.t.bytes_out]
Expand Down
11 changes: 11 additions & 0 deletions AWSIoTDeviceDefenderAgentSDK/tests/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,22 @@ def test_network_stats_delta_calculation(simple_metric):
m2.add_network_stats(bytes_in=125, packets_in=75,
bytes_out=225, packets_out=175)

m3 = metrics.Metrics(last_metric=m2)
m3.timestamp = 10
m3.interval = 10
m3.add_network_stats(bytes_in=150, packets_in=100,
bytes_out=250, packets_out=200)

assert m2.network_stats['bytes_in'] == 25
assert m2.network_stats['packets_in'] == 25
assert m2.network_stats['bytes_out'] == 25
assert m2.network_stats['packets_out'] == 25

assert m3.network_stats['bytes_in'] == 25
assert m3.network_stats['packets_in'] == 25
assert m3.network_stats['bytes_out'] == 25
assert m3.network_stats['packets_out'] == 25


def test_add_network_connections(simple_metric):
assert len(simple_metric._net_connections) == 2
Expand Down

0 comments on commit 31a65f3

Please sign in to comment.