Show More
@@ -427,40 +427,41 b' class DisplayFormats:' | |||
|
427 | 427 | FlameGraph = 4 |
|
428 | 428 | Json = 5 |
|
429 | 429 | |
|
430 | def display(fp=None, format=3, **kwargs): | |
|
430 | def display(fp=None, format=3, data=None, **kwargs): | |
|
431 | 431 | '''Print statistics, either to stdout or the given file object.''' |
|
432 | data = data or state | |
|
432 | 433 | |
|
433 | 434 | if fp is None: |
|
434 | 435 | import sys |
|
435 | 436 | fp = sys.stdout |
|
436 |
if len( |
|
|
437 | if len(data.samples) == 0: | |
|
437 | 438 | print('No samples recorded.', file=fp) |
|
438 | 439 | return |
|
439 | 440 | |
|
440 | 441 | if format == DisplayFormats.ByLine: |
|
441 | display_by_line(fp) | |
|
442 | display_by_line(data, fp) | |
|
442 | 443 | elif format == DisplayFormats.ByMethod: |
|
443 | display_by_method(fp) | |
|
444 | display_by_method(data, fp) | |
|
444 | 445 | elif format == DisplayFormats.AboutMethod: |
|
445 | display_about_method(fp, **kwargs) | |
|
446 | display_about_method(data, fp, **kwargs) | |
|
446 | 447 | elif format == DisplayFormats.Hotpath: |
|
447 | display_hotpath(fp, **kwargs) | |
|
448 | display_hotpath(data, fp, **kwargs) | |
|
448 | 449 | elif format == DisplayFormats.FlameGraph: |
|
449 | write_to_flame(fp, **kwargs) | |
|
450 | write_to_flame(data, fp, **kwargs) | |
|
450 | 451 | elif format == DisplayFormats.Json: |
|
451 | write_to_json(fp) | |
|
452 | write_to_json(data, fp) | |
|
452 | 453 | else: |
|
453 | 454 | raise Exception("Invalid display format") |
|
454 | 455 | |
|
455 | 456 | if format != DisplayFormats.Json: |
|
456 | 457 | print('---', file=fp) |
|
457 |
print('Sample count: %d' % len( |
|
|
458 |
print('Total time: %f seconds' % |
|
|
458 | print('Sample count: %d' % len(data.samples), file=fp) | |
|
459 | print('Total time: %f seconds' % data.accumulated_time, file=fp) | |
|
459 | 460 | |
|
460 | def display_by_line(fp): | |
|
461 | def display_by_line(data, fp): | |
|
461 | 462 | '''Print the profiler data with each sample line represented |
|
462 | 463 | as one row in a table. Sorted by self-time per line.''' |
|
463 |
stats = SiteStats.buildstats( |
|
|
464 | stats = SiteStats.buildstats(data.samples) | |
|
464 | 465 | stats.sort(reverse=True, key=lambda x: x.selfseconds()) |
|
465 | 466 | |
|
466 | 467 | print('%5.5s %10.10s %7.7s %-8.8s' % |
@@ -477,7 +478,7 b' def display_by_line(fp):' | |||
|
477 | 478 | sitelabel), |
|
478 | 479 | file=fp) |
|
479 | 480 | |
|
480 | def display_by_method(fp): | |
|
481 | def display_by_method(data, fp): | |
|
481 | 482 | '''Print the profiler data with each sample function represented |
|
482 | 483 | as one row in a table. Important lines within that function are |
|
483 | 484 | output as nested rows. Sorted by self-time per line.''' |
@@ -486,7 +487,7 b' def display_by_method(fp):' | |||
|
486 | 487 | print('%5.5s %9.9s %8.8s %-8.8s' % |
|
487 | 488 | ("time", "seconds", "seconds", "name"), file=fp) |
|
488 | 489 | |
|
489 |
stats = SiteStats.buildstats( |
|
|
490 | stats = SiteStats.buildstats(data.samples) | |
|
490 | 491 | |
|
491 | 492 | grouped = defaultdict(list) |
|
492 | 493 | for stat in stats: |
@@ -530,7 +531,7 b' def display_by_method(fp):' | |||
|
530 | 531 | |
|
531 | 532 | print('%33.0f%% %6.2f line %s: %s' % (stattuple), file=fp) |
|
532 | 533 | |
|
533 | def display_about_method(fp, function=None, **kwargs): | |
|
534 | def display_about_method(data, fp, function=None, **kwargs): | |
|
534 | 535 | if function is None: |
|
535 | 536 | raise Exception("Invalid function") |
|
536 | 537 | |
@@ -542,7 +543,7 b' def display_about_method(fp, function=No' | |||
|
542 | 543 | parents = {} |
|
543 | 544 | children = {} |
|
544 | 545 | |
|
545 |
for sample in |
|
|
546 | for sample in data.samples: | |
|
546 | 547 | for i, site in enumerate(sample.stack): |
|
547 | 548 | if site.function == function and (not filename |
|
548 | 549 | or site.filename() == filename): |
@@ -566,7 +567,7 b' def display_about_method(fp, function=No' | |||
|
566 | 567 | (count / relevant_samples * 100, parent.filename(), |
|
567 | 568 | parent.function, parent.lineno, parent.getsource(50)), file=fp) |
|
568 | 569 | |
|
569 |
stats = SiteStats.buildstats( |
|
|
570 | stats = SiteStats.buildstats(data.samples) | |
|
570 | 571 | stats = [s for s in stats |
|
571 | 572 | if s.site.function == function and |
|
572 | 573 | (not filename or s.site.filename() == filename)] |
@@ -599,7 +600,7 b' def display_about_method(fp, function=No' | |||
|
599 | 600 | (count / relevant_samples * 100, child.lineno, |
|
600 | 601 | child.getsource(50)), file=fp) |
|
601 | 602 | |
|
602 | def display_hotpath(fp, limit=0.05, **kwargs): | |
|
603 | def display_hotpath(data, fp, limit=0.05, **kwargs): | |
|
603 | 604 | class HotNode(object): |
|
604 | 605 | def __init__(self, site): |
|
605 | 606 | self.site = site |
@@ -623,8 +624,8 b' def display_hotpath(fp, limit=0.05, **kw' | |||
|
623 | 624 | child.add(stack[i:], time) |
|
624 | 625 | |
|
625 | 626 | root = HotNode(None) |
|
626 |
lasttime = |
|
|
627 |
for sample in |
|
|
627 | lasttime = data.samples[0].time | |
|
628 | for sample in data.samples: | |
|
628 | 629 | root.add(sample.stack[::-1], sample.time - lasttime) |
|
629 | 630 | lasttime = sample.time |
|
630 | 631 | |
@@ -671,7 +672,7 b' def display_hotpath(fp, limit=0.05, **kw' | |||
|
671 | 672 | if root.count > 0: |
|
672 | 673 | _write(root, 0, False) |
|
673 | 674 | |
|
674 | def write_to_flame(fp, scriptpath=None, outputfile=None, **kwargs): | |
|
675 | def write_to_flame(data, fp, scriptpath=None, outputfile=None, **kwargs): | |
|
675 | 676 | if scriptpath is None: |
|
676 | 677 | scriptpath = os.environ['HOME'] + '/flamegraph.pl' |
|
677 | 678 | if not os.path.exists(scriptpath): |
@@ -685,7 +686,7 b' def write_to_flame(fp, scriptpath=None, ' | |||
|
685 | 686 | file = open(path, "w+") |
|
686 | 687 | |
|
687 | 688 | lines = {} |
|
688 |
for sample in |
|
|
689 | for sample in data.samples: | |
|
689 | 690 | sites = [s.function for s in sample.stack] |
|
690 | 691 | sites.reverse() |
|
691 | 692 | line = ';'.join(sites) |
@@ -705,10 +706,10 b' def write_to_flame(fp, scriptpath=None, ' | |||
|
705 | 706 | os.system("perl ~/flamegraph.pl %s > %s" % (path, outputfile)) |
|
706 | 707 | print("Written to %s" % outputfile, file=fp) |
|
707 | 708 | |
|
708 | def write_to_json(fp): | |
|
709 | def write_to_json(data, fp): | |
|
709 | 710 | samples = [] |
|
710 | 711 | |
|
711 |
for sample in |
|
|
712 | for sample in data.samples: | |
|
712 | 713 | stack = [] |
|
713 | 714 | |
|
714 | 715 | for frame in sample.stack: |
General Comments 0
You need to be logged in to leave comments.
Login now