papi: add call stats
Type: feature
Change-Id: Ic6d44122d3e62e09402e3f1946f7e57e9b5e7c5f
Signed-off-by: Ole Troan <ot@cisco.com>
diff --git a/src/vpp-api/python/vpp_papi/vpp_papi.py b/src/vpp-api/python/vpp_papi/vpp_papi.py
index e6eded8..7f6efbb 100644
--- a/src/vpp-api/python/vpp_papi/vpp_papi.py
+++ b/src/vpp-api/python/vpp_papi/vpp_papi.py
@@ -27,6 +27,7 @@
import fnmatch
import weakref
import atexit
+import time
from . vpp_serializer import VPPType, VPPEnumType, VPPUnionType
from . vpp_serializer import VPPMessage, vpp_get_type, VPPTypeAlias
@@ -353,6 +354,7 @@
self.use_socket = use_socket
self.server_address = server_address
self._apifiles = apifiles
+ self.stats = {}
if use_socket:
from . vpp_transport_socket import VppTransport
@@ -596,6 +598,24 @@
raise VPPValueError('Invalid argument {} to {}'
.format(list(d), msg.name))
+ def _add_stat(self, name, ms):
+ if not name in self.stats:
+ self.stats[name] = {'max': ms, 'count': 1, 'avg': ms}
+ else:
+ if ms > self.stats[name]['max']:
+ self.stats[name]['max'] = ms
+ self.stats[name]['count'] += 1
+ n = self.stats[name]['count']
+ self.stats[name]['avg'] = self.stats[name]['avg'] * (n - 1) / n + ms / n
+
+ def get_stats(self):
+ s = '\n=== API PAPI STATISTICS ===\n'
+ s += '{:<30} {:>4} {:>6} {:>6}\n'.format('message', 'cnt', 'avg', 'max')
+ for n in sorted(self.stats.items(), key=lambda v: v[1]['avg'], reverse=True):
+ s += '{:<30} {:>4} {:>6.2f} {:>6.2f}\n'.format(n[0], n[1]['count'],
+ n[1]['avg'], n[1]['max'])
+ return s
+
def _call_vpp(self, i, msgdef, multipart, **kwargs):
"""Given a message, send the message and await a reply.
@@ -611,7 +631,7 @@
the response. It will raise an IOError exception if there was
no response within the timeout window.
"""
-
+ ts = time.time()
if 'context' not in kwargs:
context = self.get_context()
kwargs['context'] = context
@@ -669,6 +689,8 @@
if len(s) > 80:
s = s[:80] + "..."
self.logger.debug(s)
+ te = time.time()
+ self._add_stat(msgdef.name, (te - ts) * 1000)
return rl
def _call_vpp_async(self, i, msg, **kwargs):