|
| 1 | +from argparse import ArgumentParser |
| 2 | +import sys |
| 3 | +import json |
| 4 | +import re |
| 5 | + |
| 6 | + |
| 7 | +def parse_args(): |
| 8 | + parser = ArgumentParser() |
| 9 | + |
| 10 | + parser.add_argument("--top", type=int, default=10) |
| 11 | + parser.add_argument("--bundles", action="store_true") |
| 12 | + parser.add_argument("--promises", action="store_true") |
| 13 | + parser.add_argument("--functions", action="store_true") |
| 14 | + |
| 15 | + return parser.parse_args() |
| 16 | + |
| 17 | + |
| 18 | +def profile(data, args): |
| 19 | + |
| 20 | + events = sorted( |
| 21 | + [event for event in data["events"]], key=lambda x: x["elapsed"], reverse=True |
| 22 | + ) |
| 23 | + |
| 24 | + filter = [] |
| 25 | + |
| 26 | + if args.bundles: |
| 27 | + filter.append("bundle") |
| 28 | + |
| 29 | + if args.promises: |
| 30 | + filter.append("promise") |
| 31 | + |
| 32 | + if args.functions: |
| 33 | + filter.append("function") |
| 34 | + |
| 35 | + if filter: |
| 36 | + events = [event for event in events if event["type"] in filter] |
| 37 | + |
| 38 | + print("%-60s %-90s %20s" % ("Component", "Location", "Time")) |
| 39 | + for t in events[: args.top]: |
| 40 | + |
| 41 | + label = "%s %s" % (t["type"], t["name"]) |
| 42 | + location = "%s:%s" % (t["filename"], t["offset"]["line"]) |
| 43 | + time_ms = "%.2f ms" % (float(t["elapsed"]) / 1e6) |
| 44 | + |
| 45 | + print("%-60s %-90s %20s" % (label, location, time_ms)) |
| 46 | + |
| 47 | + |
| 48 | +def main(): |
| 49 | + args = parse_args() |
| 50 | + m = re.search(r"\{[.\s\S]*\}", sys.stdin.read()) |
| 51 | + data = json.loads(m.group(0)) |
| 52 | + |
| 53 | + profile(data, args) |
| 54 | + |
| 55 | + |
| 56 | +if __name__ == "__main__": |
| 57 | + main() |
0 commit comments