-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
95 lines (70 loc) · 1.66 KB
/
main_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
package maxminddb_golang_example
import (
"net"
"testing"
"github.com/oschwald/maxminddb-golang"
"github.com/stretchr/testify/require"
)
type (
Country struct {
Country struct {
ISOCode string `maxminddb:"iso_code"`
} `maxminddb:"country"`
}
)
const (
countryFile = "./GeoLite2-Country_20230721/GeoLite2-Country.mmdb"
cityFile = "./GeoLite2-City_20230721/GeoLite2-City.mmdb"
)
func TestGeoLite2CountryLookupTurkey(t *testing.T) {
if testing.Short() {
t.Skip()
}
testLookupTurkey(t, countryFile)
}
func TestGeoLite2CityLookupTurkey(t *testing.T) {
if testing.Short() {
t.Skip()
}
testLookupTurkey(t, cityFile)
}
func testLookupTurkey(t *testing.T, file string) {
t.Helper()
db, err := maxminddb.Open(file)
require.NoError(t, err)
defer db.Close()
ip := net.ParseIP("95.0.0.0")
var record Country
err = db.Lookup(ip, &record)
require.NoError(t, err)
require.Equal(t, "TR", record.Country.ISOCode)
}
func BenchmarkGeoLite2CountryLookupTurkey(b *testing.B) {
if testing.Short() {
b.Skip()
}
benchmarkLookupTurkey(b, countryFile)
}
func BenchmarkGeoLite2CityLookupTurkey(b *testing.B) {
if testing.Short() {
b.Skip()
}
benchmarkLookupTurkey(b, cityFile)
}
func benchmarkLookupTurkey(b *testing.B, file string) {
b.Helper()
db, err := maxminddb.Open(file)
require.NoError(b, err)
defer db.Close()
ipRoundRobin := NewIPV4RoundRobin(net.ParseIP("95.0.0.0"), net.ParseIP("95.7.255.255"))
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
var record Country
ip := ipRoundRobin.Next()
err = db.Lookup(ip, &record)
require.NoError(b, err)
require.Equal(b, "TR", record.Country.ISOCode)
}
})
}