From: Ole Troan Date: Wed, 27 Nov 2019 22:12:48 +0000 (+0100) Subject: papi: add call stats X-Git-Tag: v20.05-rc0~255 X-Git-Url: https://gerrit.fd.io/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F63%2F23663%2F3;p=vpp.git papi: add call stats Type: feature Change-Id: Ic6d44122d3e62e09402e3f1946f7e57e9b5e7c5f Signed-off-by: Ole Troan --- diff --git a/src/vpp-api/python/vpp_papi/vpp_papi.py b/src/vpp-api/python/vpp_papi/vpp_papi.py index e6eded85a63..7f6efbbae07 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 threading 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 @@ class VPPApiClient(object): 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 @@ class VPPApiClient(object): 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 @@ class VPPApiClient(object): 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 @@ class VPPApiClient(object): 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): diff --git a/test/framework.py b/test/framework.py index cffad835b5a..d474059578b 100644 --- a/test/framework.py +++ b/test/framework.py @@ -616,6 +616,7 @@ class VppTestCase(unittest.TestCase): if hasattr(cls, 'vpp'): if hasattr(cls, 'vapi'): + cls.logger.debug(cls.vapi.vpp.get_stats()) cls.logger.debug("Disconnecting class vapi client on %s", cls.__name__) cls.vapi.disconnect()