Add bench testing
This commit is contained in:
parent
2875c0d2db
commit
654498ad8b
11
Makefile
11
Makefile
@ -1,5 +1,6 @@
|
||||
# note: call scripts from /scripts
|
||||
GOCMD=GO111MODULE=on go
|
||||
LOCALCMD=/usr/local
|
||||
|
||||
linters-install:
|
||||
@golangci-lint --version >/dev/null 2>&1 || { \
|
||||
@ -8,12 +9,16 @@ linters-install:
|
||||
}
|
||||
|
||||
lint: linters-install
|
||||
$(PWD)/bin/golangci-lint run
|
||||
$(LOCALCMD)/bin/golangci-lint run
|
||||
|
||||
fmt:
|
||||
$(GOCMD) fmt ./...
|
||||
|
||||
vet:
|
||||
$(GOCMD) vet ./.
|
||||
|
||||
test:
|
||||
$(GOCMD) test -cover -race ./...
|
||||
|
||||
bench:
|
||||
$(GOCMD) test -bench=. -benchmem ./...
|
||||
|
||||
.PHONY: test lint linters-install
|
||||
|
@ -81,7 +81,7 @@ func main() {
|
||||
## Testing
|
||||
|
||||
``` bash
|
||||
$ go test
|
||||
$ make test
|
||||
```
|
||||
|
||||
## Changelog
|
||||
|
@ -86,7 +86,7 @@ func main() {
|
||||
## 测试
|
||||
|
||||
``` bash
|
||||
$ go test
|
||||
$ make test
|
||||
```
|
||||
|
||||
## 变更日志
|
||||
|
88
bench_test.go
Normal file
88
bench_test.go
Normal 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++ {
|
||||
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user