-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_test.go
More file actions
135 lines (123 loc) · 3.05 KB
/
benchmark_test.go
File metadata and controls
135 lines (123 loc) · 3.05 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
package test
import (
"fmt"
"os"
"testing"
"github.com/ip2location/ip2location-go/v9"
"github.com/pg9182/ip2x"
)
// go test -run='^$' -bench=.+ -benchmem -count 10 -v . > bench.txt
// go run golang.org/x/perf/cmd/benchstat@latest -row .name -col /lib bench.txt
func TestMain(m *testing.M) {
fmt.Printf("db: %s\n", IP2x_DB)
os.Exit(m.Run())
}
func BenchmarkInit(b *testing.B) {
b.Run("lib=ip2x", func(b *testing.B) {
for i := 0; i < b.N; i++ {
ip2x.New(DB)
}
})
b.Run("lib=ip2location", func(b *testing.B) {
for i := 0; i < b.N; i++ {
ip2location.OpenDBWithReader(DB)
}
})
}
func BenchmarkLookupOnly(b *testing.B) {
b.Run("lib=ip2x", func(b *testing.B) {
for i := 0; i < b.N; i++ {
IP2x_DB.Lookup(ips[i%len(ips)])
}
})
b.Run("lib=ip2location", func(b *testing.B) {
for i := 0; i < b.N; i++ {
ip2locationv9_query(IP2LocationV9_DB, ipstrs[i%len(ips)], 0)
}
})
}
func BenchmarkGetAll(b *testing.B) {
b.Run("lib=ip2x", func(b *testing.B) {
for i := 0; i < b.N; i++ {
r, _ := IP2x_DB.Lookup(ips[i%len(ips)])
IP2x_DB.EachField(func(d ip2x.DBField) bool {
r.Get(d)
return true
})
}
})
b.Run("lib=ip2location", func(b *testing.B) {
for i := 0; i < b.N; i++ {
IP2LocationV9_DB.Get_all(ipstrs[i%len(ips)])
}
})
}
func BenchmarkGetOneString(b *testing.B) {
b.Run("lib=ip2x", func(b *testing.B) {
for i := 0; i < b.N; i++ {
r, _ := IP2x_DB.Lookup(ips[i%len(ips)])
r.GetString(ip2x.CountryCode)
}
})
b.Run("lib=ip2location", func(b *testing.B) {
for i := 0; i < b.N; i++ {
IP2LocationV9_DB.Get_country_short(ipstrs[i%len(ips)])
}
})
}
func BenchmarkGetOneFloat(b *testing.B) {
b.Run("lib=ip2x", func(b *testing.B) {
for i := 0; i < b.N; i++ {
r, _ := IP2x_DB.Lookup(ips[i%len(ips)])
r.GetFloat32(ip2x.Latitude)
}
})
b.Run("lib=ip2location", func(b *testing.B) {
for i := 0; i < b.N; i++ {
IP2LocationV9_DB.Get_latitude(ipstrs[i%len(ips)])
}
})
}
func BenchmarkGetTwoString(b *testing.B) {
b.Run("lib=ip2x", func(b *testing.B) {
for i := 0; i < b.N; i++ {
r, _ := IP2x_DB.Lookup(ips[i%len(ips)])
r.GetString(ip2x.CountryCode)
r.GetString(ip2x.Region)
}
})
b.Run("lib=ip2location", func(b *testing.B) {
for i := 0; i < b.N; i++ {
IP2LocationV9_DB.Get_country_short(ipstrs[i%len(ips)])
IP2LocationV9_DB.Get_country_long(ipstrs[i%len(ips)])
}
})
}
func BenchmarkGetTwoFloat(b *testing.B) {
b.Run("lib=ip2x", func(b *testing.B) {
for i := 0; i < b.N; i++ {
r, _ := IP2x_DB.Lookup(ips[i%len(ips)])
r.GetFloat32(ip2x.Latitude)
r.GetFloat32(ip2x.Longitude)
}
})
b.Run("lib=ip2location", func(b *testing.B) {
for i := 0; i < b.N; i++ {
IP2LocationV9_DB.Get_latitude(ipstrs[i%len(ips)])
IP2LocationV9_DB.Get_longitude(ipstrs[i%len(ips)])
}
})
}
func BenchmarkGetNonexistent(b *testing.B) {
b.Run("lib=ip2x", func(b *testing.B) {
for i := 0; i < b.N; i++ {
r, _ := IP2x_DB.Lookup(ips[i%len(ips)])
r.GetString(ip2x.MCC)
}
})
b.Run("lib=ip2location", func(b *testing.B) {
for i := 0; i < b.N; i++ {
IP2LocationV9_DB.Get_mcc(ipstrs[i%len(ips)])
}
})
}