Files
EdgeCommon/pkg/iplibrary/default_ip_library_test.go

83 lines
1.9 KiB
Go
Raw Normal View History

// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
package iplibrary_test
import (
2022-08-21 23:09:25 +08:00
"fmt"
"github.com/TeaOSLab/EdgeCommon/pkg/iplibrary"
"net"
2022-08-21 23:09:25 +08:00
"runtime"
"runtime/debug"
"testing"
2022-08-21 23:09:25 +08:00
"time"
)
func TestIPLibrary_Lookup(t *testing.T) {
2022-08-21 23:09:25 +08:00
var stat1 = &runtime.MemStats{}
runtime.ReadMemStats(stat1)
var lib = iplibrary.NewIPLibrary()
2022-08-21 23:09:25 +08:00
var before = time.Now()
err := lib.Init()
if err != nil {
t.Fatal(err)
}
2022-08-21 23:09:25 +08:00
var costMs = time.Since(before).Seconds() * 1000
runtime.GC()
debug.FreeOSMemory()
var stat2 = &runtime.MemStats{}
runtime.ReadMemStats(stat2)
t.Log((stat2.Alloc-stat1.Alloc)/1024/1024, "M", fmt.Sprintf("%.2f", costMs), "ms")
for _, ip := range []string{
"127.0.0.1",
"8.8.8.8",
"4.4.4.4",
"202.96.0.20",
"66.249.66.69",
"2222", // wrong ip
"2406:8c00:0:3401:133:18:168:70", // ipv6
} {
var result = lib.Lookup(net.ParseIP(ip))
t.Log(ip, "=>", result.IsOk(), "[", result.CountryName(), result.CountryId(), "][", result.ProvinceName(), result.ProvinceId(), "][", result.TownName(), result.TownId(), "][", result.ProviderName(), result.ProviderId(), "]")
}
}
func TestIPLibrary_LookupIP(t *testing.T) {
var lib = iplibrary.NewIPLibrary()
err := lib.Init()
if err != nil {
t.Fatal(err)
}
for _, ip := range []string{
"66.249.66.69",
} {
var result = lib.LookupIP(ip)
if result.IsOk() {
t.Log(ip, "=>", result.IsOk(), "[", result.CountryName(), result.CountryId(), "][", result.ProvinceName(), result.ProvinceId(), "][", result.TownName(), result.TownId(), "][", result.ProviderName(), result.ProviderId(), "]")
} else {
t.Log(ip, "=>", result.IsOk())
}
}
}
func BenchmarkIPLibrary_Lookup(b *testing.B) {
var lib = iplibrary.NewIPLibrary()
err := lib.Init()
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = lib.LookupIP("66.249.66.69")
}
}