登录
首页 >  Golang >  Go问答

如何在 golang 中对标准库包进行性能测试?

来源:stackoverflow

时间:2024-02-24 08:18:24 325浏览 收藏

大家好,今天本人给大家带来文章《如何在 golang 中对标准库包进行性能测试?》,文中内容主要涉及到,如果你对Golang方面的知识点感兴趣,那就请各位朋友继续看下去吧~希望能真正帮到你们,谢谢!

问题内容

我想为 golang 做出一点贡献,并且需要运行基准测试,例如用于编码/json。

我的所有尝试都失败了(go1.13):

❯ go test -bench encoding/json
build .: cannot find module for path .

❯ go test encoding/json
ok      encoding/json   1.412s

❯ go test -bench std/encoding/json
build .: cannot find module for path .

❯ go test std/encoding/json
# std/encoding/json
package std/encoding/json (test)
        imports internal/testenv: use of internal package internal/testenv not allowed
FAIL    std/encoding/json [setup failed]
FAIL

❯ go test std
ok      archive/tar     (cached)

❯ go test -bench std
build .: cannot find module for path .

那么我如何专门测试encoding/json?


解决方案


如果这是 go 源代码树的全新检出,则在引导(即运行 all.bash)后,进入 go/src/ 并运行

../bin/go test ./encoding/json -bench=.

或者您可以通过在 /usr/local/go/src 下以相同的方式针对已安装的版本运行它。

如果您计划提交性能增强,他们会希望您进行之前/之后的基准比较。因此前后运行基准测试 10-20 次,并使用 benchstat 工具比较结果。

../bin/go test ./encoding/json -bench=. -count=10 >after

我相信这是由于 go 模块造成的,但对我有用的是从 stdlib 中执行:

cd /usr/local/go/src/encoding/json
go test -run='^$' -bench .

本篇关于《如何在 golang 中对标准库包进行性能测试?》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>