##// END OF EJS Templates
py3: switch from print(..., file=) to write()...
Gregory Szorc -
r40553:9d303434 default
parent child Browse files
Show More
@@ -469,7 +469,7 b' def display(fp=None, format=3, data=None'
469 import sys
469 import sys
470 fp = sys.stdout
470 fp = sys.stdout
471 if len(data.samples) == 0:
471 if len(data.samples) == 0:
472 print('No samples recorded.', file=fp)
472 fp.write(b'No samples recorded.\n')
473 return
473 return
474
474
475 if format == DisplayFormats.ByLine:
475 if format == DisplayFormats.ByLine:
@@ -490,10 +490,9 b' def display(fp=None, format=3, data=None'
490 raise Exception("Invalid display format")
490 raise Exception("Invalid display format")
491
491
492 if format not in (DisplayFormats.Json, DisplayFormats.Chrome):
492 if format not in (DisplayFormats.Json, DisplayFormats.Chrome):
493 print('---', file=fp)
493 fp.write(b'---\n')
494 print('Sample count: %d' % len(data.samples), file=fp)
494 fp.write(b'Sample count: %d\n' % len(data.samples))
495 print('Total time: %f seconds (%f wall)' % data.accumulated_time,
495 fp.write(b'Total time: %f seconds (%f wall)\n' % data.accumulated_time)
496 file=fp)
497
496
498 def display_by_line(data, fp):
497 def display_by_line(data, fp):
499 '''Print the profiler data with each sample line represented
498 '''Print the profiler data with each sample line represented
@@ -501,28 +500,26 b' def display_by_line(data, fp):'
501 stats = SiteStats.buildstats(data.samples)
500 stats = SiteStats.buildstats(data.samples)
502 stats.sort(reverse=True, key=lambda x: x.selfseconds())
501 stats.sort(reverse=True, key=lambda x: x.selfseconds())
503
502
504 print('%5.5s %10.10s %7.7s %-8.8s' %
503 fp.write(b'%5.5s %10.10s %7.7s %-8.8s\n' % (
505 ('% ', 'cumulative', 'self', ''), file=fp)
504 b'% ', b'cumulative', b'self', b''))
506 print('%5.5s %9.9s %8.8s %-8.8s' %
505 fp.write(b'%5.5s %9.9s %8.8s %-8.8s\n' % (
507 ("time", "seconds", "seconds", "name"), file=fp)
506 b"time", b"seconds", b"seconds", b"name"))
508
507
509 for stat in stats:
508 for stat in stats:
510 site = stat.site
509 site = stat.site
511 sitelabel = '%s:%d:%s' % (site.filename(), site.lineno, site.function)
510 sitelabel = '%s:%d:%s' % (site.filename(), site.lineno, site.function)
512 print('%6.2f %9.2f %9.2f %s' % (stat.selfpercent(),
511 fp.write(b'%6.2f %9.2f %9.2f %s\n' % (
513 stat.totalseconds(),
512 stat.selfpercent(), stat.totalseconds(),
514 stat.selfseconds(),
513 stat.selfseconds(), sitelabel))
515 sitelabel),
516 file=fp)
517
514
518 def display_by_method(data, fp):
515 def display_by_method(data, fp):
519 '''Print the profiler data with each sample function represented
516 '''Print the profiler data with each sample function represented
520 as one row in a table. Important lines within that function are
517 as one row in a table. Important lines within that function are
521 output as nested rows. Sorted by self-time per line.'''
518 output as nested rows. Sorted by self-time per line.'''
522 print('%5.5s %10.10s %7.7s %-8.8s' %
519 fp.write(b'%5.5s %10.10s %7.7s %-8.8s\n' %
523 ('% ', 'cumulative', 'self', ''), file=fp)
520 ('% ', 'cumulative', 'self', ''))
524 print('%5.5s %9.9s %8.8s %-8.8s' %
521 fp.write(b'%5.5s %9.9s %8.8s %-8.8s\n' %
525 ("time", "seconds", "seconds", "name"), file=fp)
522 ("time", "seconds", "seconds", "name"))
526
523
527 stats = SiteStats.buildstats(data.samples)
524 stats = SiteStats.buildstats(data.samples)
528
525
@@ -553,11 +550,12 b' def display_by_method(data, fp):'
553 for function in functiondata:
550 for function in functiondata:
554 if function[3] < 0.05:
551 if function[3] < 0.05:
555 continue
552 continue
556 print('%6.2f %9.2f %9.2f %s' % (function[3], # total percent
553 fp.write(b'%6.2f %9.2f %9.2f %s\n' % (
557 function[1], # total cum sec
554 function[3], # total percent
558 function[2], # total self sec
555 function[1], # total cum sec
559 function[0]), # file:function
556 function[2], # total self sec
560 file=fp)
557 function[0])) # file:function
558
561 function[4].sort(reverse=True, key=lambda i: i.selfseconds())
559 function[4].sort(reverse=True, key=lambda i: i.selfseconds())
562 for stat in function[4]:
560 for stat in function[4]:
563 # only show line numbers for significant locations (>1% time spent)
561 # only show line numbers for significant locations (>1% time spent)
@@ -566,7 +564,7 b' def display_by_method(data, fp):'
566 stattuple = (stat.selfpercent(), stat.selfseconds(),
564 stattuple = (stat.selfpercent(), stat.selfseconds(),
567 stat.site.lineno, source)
565 stat.site.lineno, source)
568
566
569 print('%33.0f%% %6.2f line %d: %s' % (stattuple), file=fp)
567 fp.write(b'%33.0f%% %6.2f line %d: %s\n' % stattuple)
570
568
571 def display_about_method(data, fp, function=None, **kwargs):
569 def display_about_method(data, fp, function=None, **kwargs):
572 if function is None:
570 if function is None:
@@ -600,9 +598,9 b' def display_about_method(data, fp, funct'
600 parents = [(parent, count) for parent, count in parents.iteritems()]
598 parents = [(parent, count) for parent, count in parents.iteritems()]
601 parents.sort(reverse=True, key=lambda x: x[1])
599 parents.sort(reverse=True, key=lambda x: x[1])
602 for parent, count in parents:
600 for parent, count in parents:
603 print('%6.2f%% %s:%s line %s: %s' %
601 fp.write(b'%6.2f%% %s:%s line %s: %s\n' %
604 (count / relevant_samples * 100, parent.filename(),
602 (count / relevant_samples * 100, parent.filename(),
605 parent.function, parent.lineno, parent.getsource(50)), file=fp)
603 parent.function, parent.lineno, parent.getsource(50)))
606
604
607 stats = SiteStats.buildstats(data.samples)
605 stats = SiteStats.buildstats(data.samples)
608 stats = [s for s in stats
606 stats = [s for s in stats
@@ -619,23 +617,23 b' def display_about_method(data, fp, funct'
619 total_self_percent += stat.selfpercent()
617 total_self_percent += stat.selfpercent()
620 total_cum_percent += stat.totalpercent()
618 total_cum_percent += stat.totalpercent()
621
619
622 print(
620 fp.write(
623 '\n %s:%s Total: %0.2fs (%0.2f%%) Self: %0.2fs (%0.2f%%)\n' %
621 b'\n %s:%s Total: %0.2fs (%0.2f%%) Self: %0.2fs (%0.2f%%)\n\n'
624 (
622 % (
625 filename or '___',
623 filename or '___',
626 function,
624 function,
627 total_cum_sec,
625 total_cum_sec,
628 total_cum_percent,
626 total_cum_percent,
629 total_self_sec,
627 total_self_sec,
630 total_self_percent
628 total_self_percent
631 ), file=fp)
629 ))
632
630
633 children = [(child, count) for child, count in children.iteritems()]
631 children = [(child, count) for child, count in children.iteritems()]
634 children.sort(reverse=True, key=lambda x: x[1])
632 children.sort(reverse=True, key=lambda x: x[1])
635 for child, count in children:
633 for child, count in children:
636 print(' %6.2f%% line %s: %s' %
634 fp.write(b' %6.2f%% line %s: %s\n' %
637 (count / relevant_samples * 100, child.lineno,
635 (count / relevant_samples * 100, child.lineno,
638 child.getsource(50)), file=fp)
636 child.getsource(50)))
639
637
640 def display_hotpath(data, fp, limit=0.05, **kwargs):
638 def display_hotpath(data, fp, limit=0.05, **kwargs):
641 class HotNode(object):
639 class HotNode(object):
@@ -697,7 +695,7 b' def display_hotpath(data, fp, limit=0.05'
697 # Make frames that didn't actually perform work dark grey
695 # Make frames that didn't actually perform work dark grey
698 elif node.count - childrensamples == 0:
696 elif node.count - childrensamples == 0:
699 finalstring = '\033[90m' + finalstring + '\033[0m'
697 finalstring = '\033[90m' + finalstring + '\033[0m'
700 print(finalstring, file=fp)
698 fp.write(finalstring + b'\n')
701
699
702 newdepth = depth
700 newdepth = depth
703 if len(visiblechildren) > 1 or multiple_siblings:
701 if len(visiblechildren) > 1 or multiple_siblings:
@@ -714,9 +712,8 b' def write_to_flame(data, fp, scriptpath='
714 if scriptpath is None:
712 if scriptpath is None:
715 scriptpath = encoding.environ['HOME'] + '/flamegraph.pl'
713 scriptpath = encoding.environ['HOME'] + '/flamegraph.pl'
716 if not os.path.exists(scriptpath):
714 if not os.path.exists(scriptpath):
717 print("error: missing %s" % scriptpath, file=fp)
715 fp.write(b'error: missing %s\n' % scriptpath)
718 print("get it here: https://github.com/brendangregg/FlameGraph",
716 fp.write(b'get it here: https://github.com/brendangregg/FlameGraph\n')
719 file=fp)
720 return
717 return
721
718
722 fd, path = pycompat.mkstemp()
719 fd, path = pycompat.mkstemp()
@@ -742,7 +739,7 b' def write_to_flame(data, fp, scriptpath='
742 outputfile = '~/flamegraph.svg'
739 outputfile = '~/flamegraph.svg'
743
740
744 os.system("perl ~/flamegraph.pl %s > %s" % (path, outputfile))
741 os.system("perl ~/flamegraph.pl %s > %s" % (path, outputfile))
745 print("Written to %s" % outputfile, file=fp)
742 fp.write(b'Written to %s\n' % outputfile)
746
743
747 _pathcache = {}
744 _pathcache = {}
748 def simplifypath(path):
745 def simplifypath(path):
@@ -874,7 +871,7 b' def write_to_chrome(data, fp, minthresho'
874 fp.write('\n')
871 fp.write('\n')
875
872
876 def printusage():
873 def printusage():
877 print("""
874 print(r"""
878 The statprof command line allows you to inspect the last profile's results in
875 The statprof command line allows you to inspect the last profile's results in
879 the following forms:
876 the following forms:
880
877
General Comments 0
You need to be logged in to leave comments. Login now