Handle output for the collector based on format and arguments. Args: collector: The collector instance with profiling data args: Parsed command-line arguments pid: Process ID (for generating filenames) mode: Profiling mode used
(collector, args, pid, mode)
| 651 | |
| 652 | |
| 653 | def _handle_output(collector, args, pid, mode): |
| 654 | """Handle output for the collector based on format and arguments. |
| 655 | |
| 656 | Args: |
| 657 | collector: The collector instance with profiling data |
| 658 | args: Parsed command-line arguments |
| 659 | pid: Process ID (for generating filenames) |
| 660 | mode: Profiling mode used |
| 661 | """ |
| 662 | if args.format == "binary": |
| 663 | # Binary format already wrote to file incrementally, just finalize |
| 664 | collector.export(None) |
| 665 | filename = collector.filename |
| 666 | print(f"Binary profile written to {filename} ({collector.total_samples} samples)") |
| 667 | elif args.format == "pstats": |
| 668 | if args.outfile: |
| 669 | # If outfile is a directory, generate filename inside it |
| 670 | if os.path.isdir(args.outfile): |
| 671 | filename = os.path.join(args.outfile, _generate_output_filename(args.format, pid)) |
| 672 | collector.export(filename) |
| 673 | else: |
| 674 | collector.export(args.outfile) |
| 675 | else: |
| 676 | # Print to stdout with defaults applied |
| 677 | sort_choice = args.sort if args.sort is not None else "nsamples" |
| 678 | limit = args.limit if args.limit is not None else 15 |
| 679 | sort_mode = _sort_to_mode(sort_choice) |
| 680 | collector.print_stats( |
| 681 | sort_mode, limit, not args.no_summary, mode |
| 682 | ) |
| 683 | else: |
| 684 | # Export to file |
| 685 | if args.outfile and os.path.isdir(args.outfile): |
| 686 | # If outfile is a directory, generate filename inside it |
| 687 | filename = os.path.join(args.outfile, _generate_output_filename(args.format, pid)) |
| 688 | else: |
| 689 | filename = args.outfile or _generate_output_filename(args.format, pid) |
| 690 | collector.export(filename) |
| 691 | |
| 692 | # Auto-open browser for HTML output if --browser flag is set |
| 693 | if args.format in ('flamegraph', 'diff_flamegraph', 'heatmap') and getattr(args, 'browser', False): |
| 694 | _open_in_browser(filename) |
| 695 | |
| 696 | |
| 697 | def _validate_args(args, parser): |
no test coverage detected
searching dependent graphs…