// 生成 CPU 报告
import (
    "context"
    "runtime/pprof"
    "log"    
)

func cpuProfile(ctx context.Context) {
    f, err := os.Create("cpu.prof")
    if err != nil {
        log.Fatal(err)
    }
    log.Println("CPU Profile started")
    pprof.StartCPUProfile(f)
    go func(){
        select{
        case <-ctx.Done():
            pprof.StopCPUProfile()
            f.Close()
        }
    }
 }
// 生成堆内存报告

import (
    "context"
    "runtime/pprof"
    "log"    
)

func heapProfile(ctx context.Context) {
    f, err := os.Create("heap.prof")
    if err != nil {
        log.Fatal(err)
    }
    defer f.Close()
    pprof.WriteHeapProfile(f)
    go func(){
        select{
        case <-ctx.Done():
            f.Close()
        }
    }
}

// 生成trace报告
import (
    "context"
    "runtime/trace"
    "log"    
)
func traceProfile(ctx context.Context) {
    f, err := os.OpenFile("trace.out")
    if err != nil {
        log.Fatal(err)
    }
    log.Println("Trace started")
    trace.Start(f)
    go func(){
        select{
        case <-ctx.Done():
            trace.Stop()
            f.Close()
        }
    }
}

相关文章:

  • 2022-02-25
  • 2021-09-06
  • 2022-12-23
  • 2022-12-23
  • 2021-11-19
  • 2022-12-23
  • 2021-08-04
  • 2023-03-24
猜你喜欢
  • 2021-10-28
  • 2021-12-15
  • 2021-06-01
  • 2021-12-25
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案