papi: add call stats
Type: feature Change-Id: Ic6d44122d3e62e09402e3f1946f7e57e9b5e7c5f Signed-off-by: Ole Troan <ot@cisco.com>
This commit is contained in:

committed by
Andrew Yourtchenko

parent
8b2fffd93f
commit
fd574087e4
@ -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):
|
||||
|
@ -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()
|
||||
|
Reference in New Issue
Block a user