Add bench testing

This commit is contained in:
guanguans 2021-01-25 17:06:49 +08:00
parent 2875c0d2db
commit 654498ad8b
4 changed files with 98 additions and 5 deletions

View File

@ -1,5 +1,6 @@
# note: call scripts from /scripts # note: call scripts from /scripts
GOCMD=GO111MODULE=on go GOCMD=GO111MODULE=on go
LOCALCMD=/usr/local
linters-install: linters-install:
@golangci-lint --version >/dev/null 2>&1 || { \ @golangci-lint --version >/dev/null 2>&1 || { \
@ -8,12 +9,16 @@ linters-install:
} }
lint: linters-install lint: linters-install
$(PWD)/bin/golangci-lint run $(LOCALCMD)/bin/golangci-lint run
fmt:
$(GOCMD) fmt ./...
vet:
$(GOCMD) vet ./.
test: test:
$(GOCMD) test -cover -race ./... $(GOCMD) test -cover -race ./...
bench: bench:
$(GOCMD) test -bench=. -benchmem ./... $(GOCMD) test -bench=. -benchmem ./...
.PHONY: test lint linters-install

View File

@ -81,7 +81,7 @@ func main() {
## Testing ## Testing
``` bash ``` bash
$ go test $ make test
``` ```
## Changelog ## Changelog

View File

@ -86,7 +86,7 @@ func main() {
## 测试 ## 测试
``` bash ``` bash
$ go test $ make test
``` ```
## 变更日志 ## 变更日志

88
bench_test.go Normal file
View File

@ -0,0 +1,88 @@
package idvalidator
import (
"testing"
)
func BenchmarkIsValid(b *testing.B) {
benchmarks := []struct {
name string
id string
}{
{id: "440308199901101512"},
{id: "610104620927690"},
{id: "810000199408230021"},
{id: "830000199201300022"},
}
for _, bm := range benchmarks {
b.Run(bm.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
IsValid(bm.name)
}
})
}
}
func BenchmarkGetInfo(b *testing.B) {
benchmarks := []struct {
name string
id string
}{
{id: "440308199901101512"},
{id: "610104620927690"},
{id: "810000199408230021"},
{id: "830000199201300022"},
}
for _, bm := range benchmarks {
b.Run(bm.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
GetInfo(bm.name)
}
})
}
}
func BenchmarkFakeId(b *testing.B) {
for i := 0; i < b.N; i++ {
FakeId()
}
}
func BenchmarkFakeRequireId(b *testing.B) {
benchmarks := []struct {
name string
isEighteen bool
address string
birthday string
sex int
}{
{isEighteen: false, address: "浙江省", birthday: "20000101", sex: 1},
{isEighteen: true, address: "浙江省", birthday: "20000101", sex: 0},
{isEighteen: true, address: "台湾省", birthday: "20000101", sex: 0},
{isEighteen: true, address: "香港特别行政区", birthday: "20000101", sex: 0},
}
for _, bm := range benchmarks {
b.Run(bm.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
FakeRequireId(bm.isEighteen, bm.address, bm.birthday, bm.sex)
}
})
}
}
func BenchmarkUpgradeId(b *testing.B) {
benchmarks := []struct {
name string
id string
}{
{id: "610104620927690"},
{id: "61010462092769"},
}
for _, bm := range benchmarks {
b.Run(bm.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
}
})
}
}