github.com/go-darwin/sys@v0.0.0-20220510002607-68fd01f054ca/testdata/testprog/timeprof.go (about)

     1  // Copyright 2018 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"fmt"
     9  	"os"
    10  	"runtime/pprof"
    11  	"time"
    12  )
    13  
    14  func init() {
    15  	register("TimeProf", TimeProf)
    16  }
    17  
    18  func TimeProf() {
    19  	f, err := os.CreateTemp("", "timeprof")
    20  	if err != nil {
    21  		fmt.Fprintln(os.Stderr, err)
    22  		os.Exit(2)
    23  	}
    24  
    25  	if err := pprof.StartCPUProfile(f); err != nil {
    26  		fmt.Fprintln(os.Stderr, err)
    27  		os.Exit(2)
    28  	}
    29  
    30  	t0 := time.Now()
    31  	// We should get a profiling signal 100 times a second,
    32  	// so running for 1/10 second should be sufficient.
    33  	for time.Since(t0) < time.Second/10 {
    34  	}
    35  
    36  	pprof.StopCPUProfile()
    37  
    38  	name := f.Name()
    39  	if err := f.Close(); err != nil {
    40  		fmt.Fprintln(os.Stderr, err)
    41  		os.Exit(2)
    42  	}
    43  
    44  	fmt.Println(name)
    45  }