gpt4 book ai didi

go - 更改 golang 测试/基准时间单位结果

转载 作者:行者123 更新时间:2023-12-04 14:00:10 36 4
gpt4 key购买 nike

目前,在对 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或秒?

更新:

这是我的基准文件( 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)
}

最佳答案

我有一个完美的解决方案给你!
安装所需的 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) }

一些有用的链接:
  • https://go.googlesource.com/proposal/+/master/design/14313-benchmark-format.md
  • https://cs.opensource.google/go/x/perf
  • 关于go - 更改 golang 测试/基准时间单位结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50466452/

    36 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com