| 613 | var cpuprofile, memprofile, blockprofile *os.File |
| 614 | |
| 615 | func startProfiling(cmd *cobra.Command, options *benchOptions) { |
| 616 | var err error |
| 617 | |
| 618 | // Start CPU profiling. |
| 619 | if options.cpuProfile != "" { |
| 620 | cpuprofile, err = os.Create(options.cpuProfile) |
| 621 | if err != nil { |
| 622 | fmt.Fprintf(cmd.ErrOrStderr(), "bench: could not create cpu profile %q: %v\n", options.cpuProfile, err) |
| 623 | os.Exit(1) |
| 624 | } |
| 625 | err = pprof.StartCPUProfile(cpuprofile) |
| 626 | if err != nil { |
| 627 | fmt.Fprintf(cmd.ErrOrStderr(), "bench: could not start cpu profile %q: %v\n", options.cpuProfile, err) |
| 628 | os.Exit(1) |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | // Start memory profiling. |
| 633 | if options.memProfile != "" { |
| 634 | memprofile, err = os.Create(options.memProfile) |
| 635 | if err != nil { |
| 636 | fmt.Fprintf(cmd.ErrOrStderr(), "bench: could not create memory profile %q: %v\n", options.memProfile, err) |
| 637 | os.Exit(1) |
| 638 | } |
| 639 | runtime.MemProfileRate = 4096 |
| 640 | } |
| 641 | |
| 642 | // Start fatal profiling. |
| 643 | if options.blockProfile != "" { |
| 644 | blockprofile, err = os.Create(options.blockProfile) |
| 645 | if err != nil { |
| 646 | fmt.Fprintf(cmd.ErrOrStderr(), "bench: could not create block profile %q: %v\n", options.blockProfile, err) |
| 647 | os.Exit(1) |
| 648 | } |
| 649 | runtime.SetBlockProfileRate(1) |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | func stopProfiling(cmd *cobra.Command) { |
| 654 | if cpuprofile != nil { |