NFVBENCH-153 Add support for python3
[nfvbench.git] / nfvbench / packet_stats.py
index 16dc965..63b1f11 100644 (file)
@@ -21,7 +21,7 @@ PacketPathStatsManager manages all packet path stats for all chains.
 
 import copy
 
-from traffic_gen.traffic_base import Latency
+from .traffic_gen.traffic_base import Latency
 
 class InterfaceStats(object):
     """A class to hold the RX and TX counters for a virtual or physical interface.
@@ -47,8 +47,13 @@ class InterfaceStats(object):
         self.device = device
         self.shared = shared
         # RX and TX counters for this interface
+        # A None value can be set to mean that the data is not available
         self.tx = 0
         self.rx = 0
+        # This is a special field to hold an optional total rx count that is only
+        # used for column aggregation to compute a total intertface stats
+        # Set to non zero to be picked by the add interface stats method for rx total
+        self.rx_total = None
 
     def get_packet_count(self, direction):
         """Get packet count for given direction.
@@ -79,8 +84,17 @@ class InterfaceStats(object):
 
     def add_if_stats(self, if_stats):
         """Add another ifstats to this instance."""
-        self.tx += if_stats.tx
-        self.rx += if_stats.rx
+        def added_counter(old_value, new_value_to_add):
+            if new_value_to_add:
+                if old_value is None:
+                    return new_value_to_add
+                return old_value + new_value_to_add
+            return old_value
+
+        self.tx = added_counter(self.tx, if_stats.tx)
+        self.rx = added_counter(self.rx, if_stats.rx)
+        # Add special rx total value if set
+        self.rx = added_counter(self.rx, if_stats.rx_total)
 
     def update_stats(self, tx, rx, diff):
         """Update stats for this interface.
@@ -223,6 +237,8 @@ class PacketPathStats(object):
             results = {'lat_min_usec': latency.min_usec,
                        'lat_max_usec': latency.max_usec,
                        'lat_avg_usec': latency.avg_usec}
+            if latency.hdrh:
+                results['hdrh'] = latency.hdrh
         else:
             results = {}
         results['packets'] = counters
@@ -276,7 +292,7 @@ class PacketPathStatsManager(object):
             chains['total'] = agg_pps.get_stats(reverse)
 
         for index, pps in enumerate(self.pps_list):
-            chains[index] = pps.get_stats(reverse)
+            chains[str(index)] = pps.get_stats(reverse)
         return {'interfaces': self._get_if_agg_name(reverse),
                 'chains': chains}
 
@@ -291,11 +307,11 @@ class PacketPathStatsManager(object):
             'Forward': {
                 'interfaces': ['Port0', 'vhost0', 'Port1'],
                 'chains': {
-                    0: {'packets': [2000054, 1999996, 1999996],
+                    '0': {'packets': [2000054, 1999996, 1999996],
                         'min_usec': 10,
                         'max_usec': 187,
                         'avg_usec': 45},
-                    1: {...},
+                    '1': {...},
                     'total': {...}
                 }
             },