去分析 - 错误的文件

标签 go profiling pprof

我正在使用 github.com/pkg/profile 在 Go 中进行分析,它在我运行我的代码时创建文件,但返回来自示例页面代码,运行我的代码会怎样? 提前致谢

代码:

package main

import (
    "fmt"
    "github.com/pkg/profile"
    "time"
)

func main() {

    defer profile.Start(profile.MemProfile).Stop()

    var inicio = time.Now().UnixNano()

    var text = "Olá Mundo!"

    fmt.Println(text)

    var fim = time.Now().UnixNano()

    fmt.Println(fim - inicio)

}

返回:

enter image description here

最佳答案

您可以将配置文件输出路径更改为当前工作目录,

profile.ProfilePath(path)

如果您无法检索任何样本,则意味着您的 MemProfileRate 不够小,无法实际捕获微小的变化。

如果您分配的内存量较少,则将MemProfileRate 设置为较小的值,如果您分配的内存量较大,则保持默认值即可。如果您认为您捕获了微小的内存变化,则增加 MemProfileRate

profile.MemProfileRate(100)

当您使用 profile 包时,您不应该忘记的一件事是您的调用应该被推迟。

defer profile.Start(xxx).Stop()

这是完整的程序。

package main

import (
    "os"

    "github.com/pkg/profile"
)

func main() {
    dir, _ := os.Getwd()
    defer profile.Start(profile.MemProfile, profile.MemProfileRate(100), profile.ProfilePath(dir)).Stop()
    //decrease mem profile rate for capturing more samples
    for i := 0; i < 10000; i++ {
        tmp := make([]byte, 100000)
        tmp[0] = tmp[1] << 0 //fake workload
    }
}

您还可以设置配置文件路径,以便在当前工作目录中输出配置文件。

关于去分析 - 错误的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52883635/

相关文章:

ruby - Golang 中 Ruby 的 "methods"方法的等价物是什么?

profiling - Julia (Julia)是否等效于Python的%timeit

profiling - 将 pprof 与 gperftools 一起使用会导致 curl 错误

macos - golang 工具 pprof 无法正常工作 - 无论分析目标如何,输出都相同

go - 可以在生产中使用 golang pprof 而不影响性能吗?

json - 如何在golang中获取结构的json字段名称?

amazon-web-services - AWS S3 上传的文件未显示

python - pydrake : How do I identify slow Python LeafSystem's (to possibly rewrite in C++)?

mongodb - 如何将具有自定义 MarshalBSONValue 的结构的 nil 指针序列化为 BSON?

python - 使用 IPython 魔法计时 python 脚本