From 654498ad8b1f7b574125c88e831a80dc786c4b15 Mon Sep 17 00:00:00 2001 From: guanguans Date: Mon, 25 Jan 2021 17:06:49 +0800 Subject: [PATCH] Add bench testing --- Makefile | 11 +++++-- README-EN.md | 2 +- README.md | 2 +- bench_test.go | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 98 insertions(+), 5 deletions(-) create mode 100644 bench_test.go diff --git a/Makefile b/Makefile index f207973..c664aba 100644 --- a/Makefile +++ b/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 diff --git a/README-EN.md b/README-EN.md index b278e6e..3e1ad37 100644 --- a/README-EN.md +++ b/README-EN.md @@ -81,7 +81,7 @@ func main() { ## Testing ``` bash -$ go test +$ make test ``` ## Changelog diff --git a/README.md b/README.md index c63b851..3c2881c 100644 --- a/README.md +++ b/README.md @@ -86,7 +86,7 @@ func main() { ## 测试 ``` bash -$ go test +$ make test ``` ## 变更日志 diff --git a/bench_test.go b/bench_test.go new file mode 100644 index 0000000..821ad3d --- /dev/null +++ b/bench_test.go @@ -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++ { + + } + }) + } +}