Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. #
  2. # Generate trace event data from MongoDB test logs.
  3. # Can easily view this data in Google Chrome's chrome://tracing tab.
  4. #
  5.  
  6. import re
  7. import dateutil.parser
  8. import json
  9. import sys
  10.  
  11. def parse_ts(ts):
  12. dtime = dateutil.parser.parse(ts)
  13. return ((dtime.minute * 60 * 1000) + (dtime.second * 1000) + dtime.microsecond/1000) * 1000
  14.  
  15. event_matchers = [
  16. # (BeginEventRegex, EndEventRegex, EventName)
  17. ("ReplSetTest starting set", "ReplSetTest finished starting set", "ReplSetTest.startSet"),
  18. ("ReplSetTest stopSet stopping all", "ReplSetTest stopSet stopped all replica set nodes", "ReplSetTest.stopSet.shutdown")
  19. ]
  20.  
  21. # Matches a log line output by a JS test, not a mongo server node.
  22. # timestamp: 2019-10-02T10:59:34.291-0400
  23. ts_match = "\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}-\d{4}"
  24. jsTestLineMatch = ".*\] (?P<ts>%s) (?P<msg>.*)" % ts_match
  25.  
  26. def line_to_event(line):
  27. js = re.search(jsTestLineMatch, line)
  28. # js test log events.
  29. if js and "----" not in js.group("msg") and "New session" not in js.group("msg"):
  30. msg = js.group("msg")
  31. ts = parse_ts(js.group("ts"))
  32. # Check against each matcher.
  33. for matcher in event_matchers:
  34. begin, end, name = matcher
  35. if re.search(begin, msg):
  36. return {"name": name, "pid": "test", "cat": "test", "ph": "B", "ts": ts}
  37. if re.search(end, msg):
  38. return {"name": name, "pid": "test", "cat": "test", "ph": "E", "ts": ts}
  39.  
  40. return None
  41.  
  42. if __name__=="__main__":
  43. # Parse all lines from the given log file.
  44. logfile = sys.argv[1]
  45. lines = open(logfile).read().splitlines()
  46. events = [l for l in map(line_to_event, lines) if l]
  47. # Print all the events.
  48. for e in events:
  49. print e
  50.  
  51. # Save the output to a JSON for viewing in Chrome tracing tab.
  52. json.dump(events, open("events.json", 'w'))
  53.  
  54. # Also print out the duration of all spans that took place.
  55. begins = [e for e in events if e["ph"] == "B"]
  56. for begin in begins:
  57. name = begin["name"]
  58. end = [e for e in events if e["ph"] == "E" and e["name"] == name][0]
  59. dur = end["ts"] - begin["ts"]
  60. print name, ("%dms" % (dur/1000.0))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement