改变golang测试/基准时间单位结果

时间:2018-05-22 11:23:55

标签: go

目前,当我在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)
}

0 个答案:

没有答案