更改Golang测试/基准时间单位结果

tcomlyy6  于 2023-09-28  发布在  Go
关注(0)|答案(2)|浏览(113)

目前,当我在golang中的一个小函数上进行golang基准测试时,我得到了以下结果

go test -bench=.

输出量:

BenchmarkBcrypt10-4     1000000000           0.08 ns/op
BenchmarkBcrypt15-4            1    2607594388 ns/op
BenchmarkBcrypt18-4            1    20472224268 ns/op
PASS

有没有办法将时间单位从ns改为milisecons或seconds?

更新:

这是我的基准测试文件(bcrypt_test.go):

package main

import "testing"

func BenchmarkBcrypt10(b *testing.B){
    HashPassword("my pass", 10)
}

func BenchmarkBcrypt15(b *testing.B){
    HashPassword("my pass", 15)
}

func BenchmarkBcrypt18(b *testing.B){
    HashPassword("my pass", 18)
}

我的main.go

package main

import (
    "fmt"

    "golang.org/x/crypto/bcrypt"
)

func HashPassword(password string, cost int) (string, error) {
    bytes, err := bcrypt.GenerateFromPassword([]byte(password), cost)
    return string(bytes), err
}

func CheckPasswordHash(password, hash string) bool {
    err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
    return err == nil
}

func main() {
    password := "secret"
    hash, _ := HashPassword(password, 12) // ignore error for the sake of simplicity

    fmt.Println("Password:", password)
    fmt.Println("Hash:    ", hash)

    match := CheckPasswordHash(password, hash)
    fmt.Println("Match:   ", match)
}
sh7euo9m

sh7euo9m1#

我有完美的解决方案给你!
安装所需的benchstat工具:

go get golang.org/x/perf/cmd/benchstat
go install  golang.org/x/perf/cmd/benchstat

生成文件:

gather: ; time go test -bench=. | grep --line-buffered  ^Bench | tee -a /tmp/o
stats: ; benchstat /tmp/o

当然,你不需要使用makefile。如果你喜欢的话,直接在shell中运行命令就可以了。

go test -bench=. >/tmp/o
benchstat /tmp/o

样品输出:

$ make gather stats 
time go test -bench=. | grep --line-buffered  ^Bench | tee -a /tmp/o
BenchmarkBcrypt2-6      1000000000           0.04578 ns/op
BenchmarkBcrypt4-6      1000000000           0.0007562 ns/op
BenchmarkBcrypt6-6      1000000000           0.002905 ns/op
BenchmarkBcrypt8-6      1000000000           0.01147 ns/op
BenchmarkBcrypt10-6     1000000000           0.04584 ns/op
BenchmarkBcrypt15-6            1    1462752100 ns/op
BenchmarkBcrypt18-6            1    11705497684 ns/op
time: Real 0m14.1s  User 0m14.1s  System 0m0.0s
benchstat /tmp/o
name        time/op
Bcrypt2-6   0.05ns ± 0%
Bcrypt4-6   0.00ns ± 1%
Bcrypt6-6   0.00ns ± 0%
Bcrypt8-6   0.01ns ± 0%
Bcrypt10-6  0.05ns ± 1%
Bcrypt18-6   11.7s ± 1%
Bcrypt15-6   1.46s ± 0%

修改的测试文件:

package main

import "testing"

func BenchmarkBcrypt2(b *testing.B) { HashPassword("my pass", 2) }
func BenchmarkBcrypt4(b *testing.B) { HashPassword("my pass", 4) }
func BenchmarkBcrypt6(b *testing.B) { HashPassword("my pass", 6) }
func BenchmarkBcrypt8(b *testing.B) { HashPassword("my pass", 8) }
func BenchmarkBcrypt10(b *testing.B) { HashPassword("my pass", 10) }
func BenchmarkBcrypt15(b *testing.B) { HashPassword("my pass", 15) }
func BenchmarkBcrypt18(b *testing.B) { HashPassword("my pass", 18) }

一些有用的链接:

nukf8bse

nukf8bse2#

乍一看...您可以尝试fmt.Println(b.Elapsed() / time.Duration(b.N))来解决此问题

package main

import (
    "fmt"
    "testing"
    "time"

    "gonum.org/v1/gonum/floats"
)

const arraySize = 1000000

func BenchmarkGonumSum(b *testing.B) {
    nums := make([]float64, arraySize)
    for i := 0; i < arraySize; i++ {
        nums[i] = float64(i)
    }

    b.ResetTimer()

    for i := 0; i < b.N; i++ {
        _ = floats.Sum(nums)
    }
    fmt.Println("number of iterations: ", b.N)
    fmt.Println("elapsed:", b.Elapsed()/time.Duration(b.N))
}

这段代码将产生下一个输出

BenchmarkGonumSum
number of iterations:  1
elapsed: 539.783µs
number of iterations:  100
elapsed: 249.077µs
number of iterations:  4816
elapsed: 188.684µs
number of iterations:  6358
elapsed: 184.344µs
BenchmarkGonumSum-12            6358        184345 ns/op
PASS

正如您在大型迭代计数上看到的,它非常接近标准基准测试的输出

相关问题