comparison mercurial/statprof.py @ 42835:db6d7cbda80b

statprof: use context manager for file when writing flame graph Differential Revision: https://phab.mercurial-scm.org/D6780
author Martin von Zweigbergk <martinvonz@google.com>
date Fri, 30 Aug 2019 16:44:31 -0700
parents c085cb134b9e
children cd3b5be5515d
comparison
equal deleted inserted replaced
42834:c085cb134b9e 42835:db6d7cbda80b
727 if not os.path.exists(scriptpath): 727 if not os.path.exists(scriptpath):
728 fp.write(b'error: missing %s\n' % scriptpath) 728 fp.write(b'error: missing %s\n' % scriptpath)
729 fp.write(b'get it here: https://github.com/brendangregg/FlameGraph\n') 729 fp.write(b'get it here: https://github.com/brendangregg/FlameGraph\n')
730 return 730 return
731 731
732 fd, path = pycompat.mkstemp()
733
734 file = open(path, "w+")
735
736 lines = {} 732 lines = {}
737 for sample in data.samples: 733 for sample in data.samples:
738 sites = [s.function for s in sample.stack] 734 sites = [s.function for s in sample.stack]
739 sites.reverse() 735 sites.reverse()
740 line = ';'.join(sites) 736 line = ';'.join(sites)
741 if line in lines: 737 if line in lines:
742 lines[line] = lines[line] + 1 738 lines[line] = lines[line] + 1
743 else: 739 else:
744 lines[line] = 1 740 lines[line] = 1
745 741
746 for line, count in lines.iteritems(): 742 fd, path = pycompat.mkstemp()
747 file.write("%s %d\n" % (line, count)) 743
748 744 with open(path, "w+") as file:
749 file.close() 745 for line, count in lines.iteritems():
746 file.write("%s %d\n" % (line, count))
750 747
751 if outputfile is None: 748 if outputfile is None:
752 outputfile = '~/flamegraph.svg' 749 outputfile = '~/flamegraph.svg'
753 750
754 os.system("perl ~/flamegraph.pl %s > %s" % (path, outputfile)) 751 os.system("perl ~/flamegraph.pl %s > %s" % (path, outputfile))