-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathbench_test.go
122 lines (111 loc) · 2.74 KB
/
bench_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// This file is part of the guanguans/id-validator.
// (c) guanguans <[email protected]>
// This source file is subject to the MIT license that is bundled.
package idvalidator
import (
"testing"
)
func BenchmarkIsValid(b *testing.B) {
benchmarks1 := []struct {
name string
id string
}{
{id: "440308199902301512"}, // 无效(出生日期码不合法)
{id: "11010119900307867X"}, // 无效(校验位不合法)
{id: "441282198101011230"}, // 特殊(历史遗留数据)
{id: "370620199505100123"}, // 特殊(出生日期在地址码发布之前)
{id: "110101199003078670"}, // 正常
}
for _, bm := range benchmarks1 {
b.Run(bm.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
IsValid(bm.id, false)
}
})
}
benchmarks2 := []struct {
name string
id string
}{
{id: "370620199505100123"}, // 特殊(出生日期在地址码发布之前)
}
for _, bm := range benchmarks2 {
b.Run(bm.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
IsValid(bm.id, true)
}
})
}
}
func BenchmarkGetInfo(b *testing.B) {
benchmarks1 := []struct {
name string
id string
}{
{id: "11010119900307867X"}, // 无效(校验位不合法)
{id: "441282198101011230"}, // 特殊(历史遗留数据:广东省肇庆市罗定市)
{id: "370620199505100123"}, // 特殊(出生日期在地址码发布之前)
{id: "110101199003078670"}, // 正常
}
for _, bm := range benchmarks1 {
b.Run(bm.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
GetInfo(bm.id, false)
}
})
}
benchmarks2 := []struct {
name string
id string
}{
{id: "500154199301135886"}, // 特殊(出生日期在地址码发布之前)
}
for _, bm := range benchmarks2 {
b.Run(bm.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
GetInfo(bm.id, true)
}
})
}
}
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: 1},
{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++ {
UpgradeId(bm.id)
}
})
}
}