我正在使用 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)
}
返回:
最佳答案
您可以将配置文件输出路径更改为当前工作目录,
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/