zoukankan      html  css  js  c++  java
  • golang代码中生成pprof和trace报告

    // 生成 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()
            }
        }
    }
    
    
  • 相关阅读:
    USACO3.2.5Magic Squares
    USACO3.1.4Shaping Regions
    USACO3.2.3Spinning Wheels
    USACO3.1.3Humble Numbers
    USACO3.1.6Stamps
    USACO3.1.1AgriNet
    USACO3.3.5A Game
    USACO3.2.4Feed Ratios
    USACO3.2.2Stringsobits
    USACO3.2.6Sweet Butter
  • 原文地址:https://www.cnblogs.com/YYRise/p/10797794.html
Copyright © 2011-2022 走看看