去分析 - 错误的文件

Go Profiling - Wrong file

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

代码:

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)

}

Return:

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

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
    }
}

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