Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #
- # Generate trace event data from MongoDB test logs.
- # Can easily view this data in Google Chrome's chrome://tracing tab.
- #
- import re
- import dateutil.parser
- import json
- import sys
- def parse_ts(ts):
- dtime = dateutil.parser.parse(ts)
- return ((dtime.minute * 60 * 1000) + (dtime.second * 1000) + dtime.microsecond/1000) * 1000
- event_matchers = [
- # (BeginEventRegex, EndEventRegex, EventName)
- ("ReplSetTest starting set", "ReplSetTest finished starting set", "ReplSetTest.startSet"),
- ("ReplSetTest stopSet stopping all", "ReplSetTest stopSet stopped all replica set nodes", "ReplSetTest.stopSet.shutdown")
- ]
- # Matches a log line output by a JS test, not a mongo server node.
- # timestamp: 2019-10-02T10:59:34.291-0400
- ts_match = "\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}-\d{4}"
- jsTestLineMatch = ".*\] (?P<ts>%s) (?P<msg>.*)" % ts_match
- def line_to_event(line):
- js = re.search(jsTestLineMatch, line)
- # js test log events.
- if js and "----" not in js.group("msg") and "New session" not in js.group("msg"):
- msg = js.group("msg")
- ts = parse_ts(js.group("ts"))
- # Check against each matcher.
- for matcher in event_matchers:
- begin, end, name = matcher
- if re.search(begin, msg):
- return {"name": name, "pid": "test", "cat": "test", "ph": "B", "ts": ts}
- if re.search(end, msg):
- return {"name": name, "pid": "test", "cat": "test", "ph": "E", "ts": ts}
- return None
- if __name__=="__main__":
- # Parse all lines from the given log file.
- logfile = sys.argv[1]
- lines = open(logfile).read().splitlines()
- events = [l for l in map(line_to_event, lines) if l]
- # Print all the events.
- for e in events:
- print e
- # Save the output to a JSON for viewing in Chrome tracing tab.
- json.dump(events, open("events.json", 'w'))
- # Also print out the duration of all spans that took place.
- begins = [e for e in events if e["ph"] == "B"]
- for begin in begins:
- name = begin["name"]
- end = [e for e in events if e["ph"] == "E" and e["name"] == name][0]
- dur = end["ts"] - begin["ts"]
- print name, ("%dms" % (dur/1000.0))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement