Skip to content

Instantly share code, notes, and snippets.

@djmunro
Created December 9, 2015 02:14
Show Gist options
  • Save djmunro/adf20468c11b4a962477 to your computer and use it in GitHub Desktop.
Save djmunro/adf20468c11b4a962477 to your computer and use it in GitHub Desktop.
import re
import sys
import json
import itertools
from collections import Counter
from collections import defaultdict
SIGNALS = []
METHOD_CALLS = []
METHOD_RETURNS = []
# split logs by date
# LOG_DATA = 'Wed Dec 2 13:08:35 2015\nhello\nWed Dec 2 13:08:35 2015\nworld'
LOG_DATA = open(sys.argv[1], 'r').read()
messages = re.split(r'\n.?\w{3} \w+ +\d+ \d+:\d+:\d+ \d+[\r\s]*\n', '\n' + LOG_DATA)
# extract things from signal line
for message in messages:
MATCH_SIGNAL = re.compile(r'signal sender=(?P<sender>.*) -> dest=(?P<dest>.*) serial=(?P<serial>.*) path=(?P<path>.*) interface=(?P<interface>.*) member=(?P<member>.*)\n\s+string (?P<function>.*)\n\s+string (?P<params>.*)')
MATCH_METHOD_CALL = re.compile(r'method call sender=(?P<sender>.*) -> dest=(?P<dest>.*) serial=(?P<serial>.*) path=(?P<path>.*) interface=(?P<interface>.*) member=(?P<member>.*)\n\s+string (?P<function>.*)\n\s+string (?P<params>.*)')
MATCH_METHOD_RETURN = re.compile(r'method return sender=(?P<sender>.*) -> dest=(?P<dest>.*) reply_serial=(?P<reply_serial>.*)')
signal = MATCH_SIGNAL.search(message)
method_call = MATCH_METHOD_CALL.search(message)
method_return = MATCH_METHOD_RETURN.search(message)
if signal:
SIGNALS.append(signal.groupdict())
if method_call:
METHOD_CALLS.append(method_call.groupdict())
if method_return:
METHOD_RETURNS.append(method_return.groupdict())
def groupby_key(message):
return message['function']
""" Signal printout
"""
print '{0: <30}{1: <15}{2:}'.format('Signals','Occurrence','Sender')
# this prints out all the signal names, and their respective occurences
grouped = itertools.groupby(sorted(SIGNALS, key=groupby_key), groupby_key)
for group, items in grouped:
items = list(items)
print '{func:<30}{occurences:<15}{path}'.format(
func=items[0]['function'],
occurences=len(items),
path=items[0]['path']
)
"""
# Alternative was return both function & path to the counter
print '-----'
c = Counter((x['function'], x['path']) for x in SIGNALS)
for (function, path), count in c.items():
print '{func}\t{count}\t{path}'.format(
func=function,
count=count,
path=path
)
"""
""" Methods printout
"""
print '\n{0: <35}{1: <15}{2: <10}{3}'.format('Methods: (Member=Invoke)','Occurrence','Sender', 'Destination')
grouped = itertools.groupby(sorted(METHOD_CALLS, key=groupby_key), groupby_key)
for group, items in grouped:
items = list(items)
print '{func:<35}{occurences:<15}{sender:<10}{destination}'.format(
func=items[0]['function'],
occurences=len(items),
sender=items[0]['sender'],
destination=items[0]['dest']
)
""" SERVICES printout
"""
print '\n{0: <45}{1: <15}'.format('Services','Occurrence')
x = Counter((x['path']) for x in SIGNALS) #give me a Counter with all the serives from path=
y = Counter((x['path']) for x in METHOD_CALLS)
def dsum(*dicts):
ret = defaultdict(int)
for d in dicts:
for k, v in d.items():
ret[k] += v
return dict(ret)
mydic=dsum(x, y)
for service, occurrence in mydic.iteritems() :
print '{key:<45}{value}'.format(key=service, value=occurrence)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment