comparison contrib/catapipe.py @ 39252:9a81f126f9fa

contrib: new script to read events from a named pipe and emit catapult traces I'm starting to get more serious about getting some insight into where we're spending our time, both in hg itself but also in the test suite. As a first pass, I'm going to try and produce catapult traces[0] that can be viewed with Chrome's `about:tracing` tool. 0: https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/edit#heading=h.nso4gcezn7n1 Differential Revision: https://phab.mercurial-scm.org/D4342
author Augie Fackler <augie@google.com>
date Tue, 21 Aug 2018 15:01:09 -0400
parents
children e9706686451b
comparison
equal deleted inserted replaced
39251:bb2b462f81da 39252:9a81f126f9fa
1 #!/usr/bin/env python3
2 #
3 # Copyright 2018 Google LLC.
4 #
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version.
7 """Tool read primitive events from a pipe to produce a catapult trace.
8
9 For now the event stream supports
10
11 START $SESSIONID ...
12
13 and
14
15 END $SESSIONID ...
16
17 events. Everything after the SESSIONID (which must not contain spaces)
18 is used as a label for the event. Events are timestamped as of when
19 they arrive in this process and are then used to produce catapult
20 traces that can be loaded in Chrome's about:tracing utility. It's
21 important that the event stream *into* this process stay simple,
22 because we have to emit it from the shell scripts produced by
23 run-tests.py.
24
25 Typically you'll want to place the path to the named pipe in the
26 HGCATAPULTSERVERPIPE environment variable, which both run-tests and hg
27 understand.
28 """
29 from __future__ import absolute_import, print_function
30
31 import argparse
32 import datetime
33 import json
34 import os
35
36 _TYPEMAP = {
37 'START': 'B',
38 'END': 'E',
39 }
40
41 _threadmap = {}
42
43 def main():
44 parser = argparse.ArgumentParser()
45 parser.add_argument('pipe', type=str, nargs=1,
46 help='Path of named pipe to create and listen on.')
47 parser.add_argument('output', default='trace.json', type=str, nargs='?',
48 help='Path of named pipe to create and listen on.')
49 parser.add_argument('--debug', default=False, action='store_true',
50 help='Print useful debug messages')
51 args = parser.parse_args()
52 fn = args.pipe[0]
53 os.mkfifo(fn)
54 try:
55 with open(fn) as f, open(args.output, 'w') as out:
56 out.write('[\n')
57 start = datetime.datetime.now()
58 while True:
59 ev = f.readline().strip()
60 if not ev:
61 continue
62 now = datetime.datetime.now()
63 if args.debug:
64 print(ev)
65 verb, session, label = ev.split(' ', 2)
66 if session not in _threadmap:
67 _threadmap[session] = len(_threadmap)
68 pid = _threadmap[session]
69 ts_micros = (now - start).total_seconds() * 1000000
70 out.write(json.dumps(
71 {
72 "name": label,
73 "cat": "misc",
74 "ph": _TYPEMAP[verb],
75 "ts": ts_micros,
76 "pid": pid,
77 "tid": 1,
78 "args": {}
79 }))
80 out.write(',\n')
81 finally:
82 os.unlink(fn)
83
84 if __name__ == '__main__':
85 main()