golang测试

    xiaoxiao2024-01-10  164

    转自http://studygolang.com/articles/1155http://www.tuicool.com/articles/RnMJrm

    参考:http://blog.golang.org/profiling-go-programshttp://google-perftools.googlecode.com/svn/trunk/doc/cpuprofile.html

    代码要在Go语言中开启profiling,可以参考以下代码:

    import (

    "runtime/pprof" // 引用pprof package "os"

    )func main() {

    f, _ := os.Create("profile_file") pprof.StartCPUProfile(f) // 开始cpu profile,结果写到文件f中 defer pprof.StopCPUProfile() // 结束profile

    }

    运行运行程序,生成profile文件分析在命令行上执行:

    go tool pprof [binary] [profile]进入pprof环境后,可以用help命令查看帮助信息最常用的命令如top10,可以看最耗时的function这里详细解释一下top命令的输出格式,例如: 14 2.1% 17.2% 58 8.7% std::_Rb_tree::find各字段的含义依次是:

    采样点落在该函数中的次数采样点落在该函数中的百分比上一项的累积百分比采样点落在该函数,以及被它调用的函数中的总次数采样点落在该函数,以及被它调用的函数中的总次数百分比函数名
    最新回复(0)