1.4.5.2
This commit is contained in:
135
EdgeCommon/pkg/iplibrary/default_ip_library_plus_test.go
Normal file
135
EdgeCommon/pkg/iplibrary/default_ip_library_plus_test.go
Normal file
@@ -0,0 +1,135 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||||
//go:build plus
|
||||
|
||||
package iplibrary_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/iplibrary"
|
||||
"net"
|
||||
"runtime"
|
||||
"runtime/debug"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestPlusIPLibrary_Init(t *testing.T) {
|
||||
err := iplibrary.InitPlus()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPlusIPLibrary_Init_Many(t *testing.T) {
|
||||
for i := 0; i < 10; i++ {
|
||||
err := iplibrary.InitPlus()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPlusIPLibrary_GC(t *testing.T) {
|
||||
_ = iplibrary.InitPlus()
|
||||
runtime.GC()
|
||||
|
||||
var gcPause = func() {
|
||||
var before = time.Now()
|
||||
runtime.GC()
|
||||
var costSeconds = time.Since(before).Seconds()
|
||||
var stats = &debug.GCStats{}
|
||||
debug.ReadGCStats(stats)
|
||||
t.Log("pause:", stats.PauseTotal.Seconds()*1000, "ms", "cost:", costSeconds*1000, "ms")
|
||||
}
|
||||
|
||||
gcPause()
|
||||
}
|
||||
|
||||
func TestPlusIPLibrary_Switch(t *testing.T) {
|
||||
{
|
||||
err := iplibrary.InitDefault()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log(iplibrary.LookupIP("1.2.3.4").Summary())
|
||||
}
|
||||
|
||||
time.Sleep(1 * time.Second)
|
||||
|
||||
{
|
||||
err := iplibrary.InitPlus()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log(iplibrary.LookupIP("1.2.3.4").Summary())
|
||||
}
|
||||
|
||||
time.Sleep(1 * time.Second)
|
||||
|
||||
{
|
||||
err := iplibrary.InitDefault()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log(iplibrary.LookupIP("1.2.3.4").Summary())
|
||||
}
|
||||
|
||||
time.Sleep(1 * time.Second)
|
||||
|
||||
runtime.GC()
|
||||
debug.FreeOSMemory()
|
||||
|
||||
time.Sleep(1 * time.Hour)
|
||||
}
|
||||
|
||||
func TestPlusIPLibrary_Lookup(t *testing.T) {
|
||||
var stat1 = &runtime.MemStats{}
|
||||
runtime.ReadMemStats(stat1)
|
||||
|
||||
var before = time.Now()
|
||||
|
||||
err := iplibrary.InitPlus()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
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", // 美国 华盛顿
|
||||
"67.220.91.30", // 美国 加利福尼亚
|
||||
"202.96.0.20", // 中国 北京市 联通
|
||||
"111.197.165.199",
|
||||
"111.199.219.151",
|
||||
"66.249.66.69", // 美国北卡罗来纳州勒诺
|
||||
"2222", // wrong ip
|
||||
"2406:8c00:0:3401:133:18:168:70", // ipv6
|
||||
"45.113.194.0",
|
||||
"23.199.72.0", // 缅甸
|
||||
"144.48.80.115", // 日本, 东京
|
||||
"203.207.46.3", // 台湾省, 台北, 信义
|
||||
"1.2.3.4",
|
||||
} {
|
||||
var result = iplibrary.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(), "]", result.Summary())
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkPlusIPLibrary_Lookup(b *testing.B) {
|
||||
_ = iplibrary.InitPlus()
|
||||
|
||||
b.ResetTimer()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = iplibrary.LookupIP("66.249.66.69")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user