2024-03-30 14:42:56 +08:00
|
|
|
package iplibrary_test
|
2020-11-09 10:45:44 +08:00
|
|
|
|
|
|
|
|
import (
|
2024-03-30 14:42:56 +08:00
|
|
|
"fmt"
|
2024-04-06 15:37:14 +08:00
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/iputils"
|
2024-03-30 14:42:56 +08:00
|
|
|
"github.com/TeaOSLab/EdgeNode/internal/iplibrary"
|
|
|
|
|
"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
|
|
|
|
|
"github.com/TeaOSLab/EdgeNode/internal/utils/testutils"
|
2021-01-03 20:18:47 +08:00
|
|
|
"github.com/iwind/TeaGo/assert"
|
|
|
|
|
"github.com/iwind/TeaGo/logs"
|
2021-10-04 17:42:38 +08:00
|
|
|
"github.com/iwind/TeaGo/rands"
|
2024-03-30 14:42:56 +08:00
|
|
|
"math/rand"
|
2020-11-09 10:45:44 +08:00
|
|
|
"runtime"
|
2021-12-08 15:17:45 +08:00
|
|
|
"runtime/debug"
|
2020-11-09 10:45:44 +08:00
|
|
|
"strconv"
|
2021-12-08 15:17:45 +08:00
|
|
|
"sync"
|
2020-11-09 10:45:44 +08:00
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
2021-01-03 20:18:47 +08:00
|
|
|
func TestIPList_Add_Empty(t *testing.T) {
|
2024-04-06 10:07:39 +08:00
|
|
|
var ipList = iplibrary.NewIPList()
|
2024-03-30 14:42:56 +08:00
|
|
|
ipList.Add(&iplibrary.IPItem{
|
2021-01-03 20:18:47 +08:00
|
|
|
Id: 1,
|
|
|
|
|
})
|
2024-03-30 14:42:56 +08:00
|
|
|
logs.PrintAsJSON(ipList.ItemsMap(), t)
|
|
|
|
|
logs.PrintAsJSON(ipList.AllItemsMap(), t)
|
|
|
|
|
logs.PrintAsJSON(ipList.IPMap(), t)
|
2021-01-03 20:18:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestIPList_Add_One(t *testing.T) {
|
2024-04-06 10:07:39 +08:00
|
|
|
var a = assert.NewAssertion(t)
|
|
|
|
|
|
2024-03-30 14:42:56 +08:00
|
|
|
var ipList = iplibrary.NewIPList()
|
|
|
|
|
ipList.Add(&iplibrary.IPItem{
|
2021-01-03 20:18:47 +08:00
|
|
|
Id: 1,
|
2024-04-06 15:37:14 +08:00
|
|
|
IPFrom: iputils.ToBytes("192.168.1.1"),
|
2021-01-03 20:18:47 +08:00
|
|
|
})
|
2024-03-30 14:42:56 +08:00
|
|
|
ipList.Add(&iplibrary.IPItem{
|
2021-01-03 20:18:47 +08:00
|
|
|
Id: 2,
|
2024-04-06 15:37:14 +08:00
|
|
|
IPTo: iputils.ToBytes("192.168.1.2"),
|
2021-02-02 15:26:00 +08:00
|
|
|
})
|
2024-03-30 14:42:56 +08:00
|
|
|
ipList.Add(&iplibrary.IPItem{
|
2021-02-02 15:26:00 +08:00
|
|
|
Id: 3,
|
2024-04-06 15:37:14 +08:00
|
|
|
IPFrom: iputils.ToBytes("192.168.0.2"),
|
2021-02-02 15:26:00 +08:00
|
|
|
})
|
2024-03-30 14:42:56 +08:00
|
|
|
ipList.Add(&iplibrary.IPItem{
|
2021-02-02 15:26:00 +08:00
|
|
|
Id: 4,
|
2024-04-06 15:37:14 +08:00
|
|
|
IPFrom: iputils.ToBytes("192.168.0.2"),
|
|
|
|
|
IPTo: iputils.ToBytes("192.168.0.1"),
|
2021-10-04 17:42:38 +08:00
|
|
|
})
|
2024-03-30 14:42:56 +08:00
|
|
|
ipList.Add(&iplibrary.IPItem{
|
2021-10-04 17:42:38 +08:00
|
|
|
Id: 5,
|
2024-04-06 15:37:14 +08:00
|
|
|
IPFrom: iputils.ToBytes("2001:db8:0:1::101"),
|
2021-10-04 17:42:38 +08:00
|
|
|
})
|
2024-03-30 14:42:56 +08:00
|
|
|
ipList.Add(&iplibrary.IPItem{
|
2021-10-04 17:42:38 +08:00
|
|
|
Id: 6,
|
2024-04-06 10:07:39 +08:00
|
|
|
IPFrom: nil,
|
2021-02-02 15:26:00 +08:00
|
|
|
Type: "all",
|
2021-01-03 20:18:47 +08:00
|
|
|
})
|
2021-10-04 17:42:38 +08:00
|
|
|
t.Log("===items===")
|
2024-03-30 14:42:56 +08:00
|
|
|
logs.PrintAsJSON(ipList.ItemsMap(), t)
|
2021-10-04 17:42:38 +08:00
|
|
|
|
|
|
|
|
t.Log("===sorted items===")
|
2024-03-30 14:42:56 +08:00
|
|
|
logs.PrintAsJSON(ipList.SortedRangeItems(), t)
|
2021-10-04 17:42:38 +08:00
|
|
|
|
|
|
|
|
t.Log("===all items===")
|
2024-04-06 10:07:39 +08:00
|
|
|
a.IsTrue(len(ipList.AllItemsMap()) == 1)
|
2024-03-30 14:42:56 +08:00
|
|
|
logs.PrintAsJSON(ipList.AllItemsMap(), t) // ip => items
|
|
|
|
|
|
|
|
|
|
t.Log("===ip items===")
|
|
|
|
|
logs.PrintAsJSON(ipList.IPMap())
|
2021-01-03 20:18:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestIPList_Update(t *testing.T) {
|
2024-03-30 14:42:56 +08:00
|
|
|
var ipList = iplibrary.NewIPList()
|
|
|
|
|
ipList.Add(&iplibrary.IPItem{
|
2021-01-03 20:18:47 +08:00
|
|
|
Id: 1,
|
2024-04-06 15:37:14 +08:00
|
|
|
IPFrom: iputils.ToBytes("192.168.1.1"),
|
2021-01-03 20:18:47 +08:00
|
|
|
})
|
2024-03-30 14:42:56 +08:00
|
|
|
|
|
|
|
|
t.Log("===before===")
|
|
|
|
|
logs.PrintAsJSON(ipList.ItemsMap(), t)
|
|
|
|
|
logs.PrintAsJSON(ipList.SortedRangeItems(), t)
|
|
|
|
|
logs.PrintAsJSON(ipList.IPMap(), t)
|
|
|
|
|
|
|
|
|
|
/**ipList.Add(&iplibrary.IPItem{
|
2021-01-03 20:18:47 +08:00
|
|
|
Id: 2,
|
2024-04-06 15:37:14 +08:00
|
|
|
IPFrom: iputils.ToBytes("192.168.1.1"),
|
2021-01-03 20:18:47 +08:00
|
|
|
})**/
|
2024-03-30 14:42:56 +08:00
|
|
|
ipList.Add(&iplibrary.IPItem{
|
|
|
|
|
Id: 1,
|
|
|
|
|
//IPFrom: 123,
|
2024-04-06 15:37:14 +08:00
|
|
|
IPTo: iputils.ToBytes("192.168.1.2"),
|
2021-01-03 20:18:47 +08:00
|
|
|
})
|
2024-03-30 14:42:56 +08:00
|
|
|
|
|
|
|
|
t.Log("===after===")
|
|
|
|
|
logs.PrintAsJSON(ipList.ItemsMap(), t)
|
|
|
|
|
logs.PrintAsJSON(ipList.SortedRangeItems(), t)
|
|
|
|
|
logs.PrintAsJSON(ipList.IPMap(), t)
|
2021-10-04 17:42:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestIPList_Update_AllItems(t *testing.T) {
|
2024-03-30 14:42:56 +08:00
|
|
|
var ipList = iplibrary.NewIPList()
|
|
|
|
|
ipList.Add(&iplibrary.IPItem{
|
2021-10-04 17:42:38 +08:00
|
|
|
Id: 1,
|
2024-03-30 14:42:56 +08:00
|
|
|
Type: iplibrary.IPItemTypeAll,
|
2024-04-06 10:07:39 +08:00
|
|
|
IPFrom: nil,
|
2021-10-04 17:42:38 +08:00
|
|
|
})
|
2024-03-30 14:42:56 +08:00
|
|
|
ipList.Add(&iplibrary.IPItem{
|
2021-10-04 17:42:38 +08:00
|
|
|
Id: 1,
|
2024-04-06 10:07:39 +08:00
|
|
|
IPTo: nil,
|
2021-10-04 17:42:38 +08:00
|
|
|
})
|
|
|
|
|
t.Log("===items map===")
|
2024-03-30 14:42:56 +08:00
|
|
|
logs.PrintAsJSON(ipList.ItemsMap(), t)
|
2021-10-04 17:42:38 +08:00
|
|
|
t.Log("===all items map===")
|
2024-03-30 14:42:56 +08:00
|
|
|
logs.PrintAsJSON(ipList.AllItemsMap(), t)
|
|
|
|
|
t.Log("===ip map===")
|
|
|
|
|
logs.PrintAsJSON(ipList.IPMap())
|
2021-01-03 20:18:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestIPList_Add_Range(t *testing.T) {
|
2024-03-30 14:42:56 +08:00
|
|
|
var a = assert.NewAssertion(t)
|
|
|
|
|
|
|
|
|
|
var ipList = iplibrary.NewIPList()
|
|
|
|
|
ipList.Add(&iplibrary.IPItem{
|
2021-01-03 20:18:47 +08:00
|
|
|
Id: 1,
|
2024-04-06 15:37:14 +08:00
|
|
|
IPFrom: iputils.ToBytes("192.168.1.1"),
|
|
|
|
|
IPTo: iputils.ToBytes("192.168.2.1"),
|
2021-01-03 20:18:47 +08:00
|
|
|
})
|
2024-03-30 14:42:56 +08:00
|
|
|
ipList.Add(&iplibrary.IPItem{
|
2021-01-03 20:18:47 +08:00
|
|
|
Id: 2,
|
2024-04-06 15:37:14 +08:00
|
|
|
IPTo: iputils.ToBytes("192.168.1.2"),
|
2024-03-30 14:42:56 +08:00
|
|
|
})
|
|
|
|
|
ipList.Add(&iplibrary.IPItem{
|
|
|
|
|
Id: 3,
|
2024-04-06 15:37:14 +08:00
|
|
|
IPFrom: iputils.ToBytes("192.168.0.1"),
|
|
|
|
|
IPTo: iputils.ToBytes("192.168.0.2"),
|
2021-01-03 20:18:47 +08:00
|
|
|
})
|
2024-03-30 14:42:56 +08:00
|
|
|
|
|
|
|
|
a.IsTrue(len(ipList.SortedRangeItems()) == 2)
|
|
|
|
|
|
|
|
|
|
t.Log(len(ipList.ItemsMap()), "ips")
|
|
|
|
|
t.Log("===items map===")
|
|
|
|
|
logs.PrintAsJSON(ipList.ItemsMap(), t)
|
|
|
|
|
t.Log("===sorted range items===")
|
|
|
|
|
logs.PrintAsJSON(ipList.SortedRangeItems())
|
|
|
|
|
t.Log("===all items map===")
|
|
|
|
|
logs.PrintAsJSON(ipList.AllItemsMap(), t)
|
|
|
|
|
|
|
|
|
|
t.Log("===ip map===")
|
|
|
|
|
logs.PrintAsJSON(ipList.IPMap(), t)
|
2021-01-03 20:18:47 +08:00
|
|
|
}
|
|
|
|
|
|
2020-11-09 10:45:44 +08:00
|
|
|
func TestNewIPList_Memory(t *testing.T) {
|
2024-03-30 14:42:56 +08:00
|
|
|
var list = iplibrary.NewIPList()
|
|
|
|
|
|
|
|
|
|
var count = 100
|
|
|
|
|
if testutils.IsSingleTesting() {
|
|
|
|
|
count = 2_000_000
|
|
|
|
|
}
|
|
|
|
|
var stat1 = testutils.ReadMemoryStat()
|
2020-11-09 10:45:44 +08:00
|
|
|
|
2024-03-30 14:42:56 +08:00
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
|
list.AddDelay(&iplibrary.IPItem{
|
|
|
|
|
Id: uint64(i),
|
2024-04-06 15:37:14 +08:00
|
|
|
IPFrom: iputils.ToBytes(testutils.RandIP()),
|
|
|
|
|
IPTo: iputils.ToBytes(testutils.RandIP()),
|
2020-11-09 10:45:44 +08:00
|
|
|
ExpiredAt: time.Now().Unix(),
|
|
|
|
|
})
|
|
|
|
|
}
|
2024-04-06 10:07:39 +08:00
|
|
|
|
2024-03-30 14:42:56 +08:00
|
|
|
list.Sort()
|
|
|
|
|
|
|
|
|
|
runtime.GC()
|
2020-11-09 10:45:44 +08:00
|
|
|
|
2024-03-30 14:42:56 +08:00
|
|
|
var stat2 = testutils.ReadMemoryStat()
|
|
|
|
|
t.Log((stat2.HeapInuse-stat1.HeapInuse)>>20, "MB")
|
2020-11-09 10:45:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestIPList_Contains(t *testing.T) {
|
2021-10-04 17:42:38 +08:00
|
|
|
var a = assert.NewAssertion(t)
|
|
|
|
|
|
2024-03-30 14:42:56 +08:00
|
|
|
var list = iplibrary.NewIPList()
|
2020-11-09 10:45:44 +08:00
|
|
|
for i := 0; i < 255; i++ {
|
2024-03-30 14:42:56 +08:00
|
|
|
list.Add(&iplibrary.IPItem{
|
2022-07-25 20:23:40 +08:00
|
|
|
Id: uint64(i),
|
2024-04-06 15:37:14 +08:00
|
|
|
IPFrom: iputils.ToBytes(strconv.Itoa(i) + ".168.0.1"),
|
|
|
|
|
IPTo: iputils.ToBytes(strconv.Itoa(i) + ".168.255.1"),
|
2020-11-09 10:45:44 +08:00
|
|
|
ExpiredAt: 0,
|
|
|
|
|
})
|
|
|
|
|
}
|
2021-10-04 17:42:38 +08:00
|
|
|
for i := 0; i < 255; i++ {
|
2024-03-30 14:42:56 +08:00
|
|
|
list.Add(&iplibrary.IPItem{
|
2022-07-25 20:23:40 +08:00
|
|
|
Id: uint64(1000 + i),
|
2024-04-06 15:37:14 +08:00
|
|
|
IPFrom: iputils.ToBytes("192.167.2." + strconv.Itoa(i)),
|
2021-10-04 17:42:38 +08:00
|
|
|
})
|
|
|
|
|
}
|
2024-04-06 15:37:14 +08:00
|
|
|
|
|
|
|
|
list.Add(&iplibrary.IPItem{
|
|
|
|
|
Id: 10000,
|
|
|
|
|
IPFrom: iputils.ToBytes("::1"),
|
|
|
|
|
})
|
|
|
|
|
list.Add(&iplibrary.IPItem{
|
|
|
|
|
Id: 10001,
|
|
|
|
|
IPFrom: iputils.ToBytes("::2"),
|
|
|
|
|
IPTo: iputils.ToBytes("::5"),
|
|
|
|
|
})
|
|
|
|
|
|
2024-03-30 14:42:56 +08:00
|
|
|
t.Log(len(list.ItemsMap()), "ip")
|
|
|
|
|
|
|
|
|
|
var before = time.Now()
|
2024-04-06 15:37:14 +08:00
|
|
|
a.IsTrue(list.Contains(iputils.ToBytes("192.168.1.100")))
|
|
|
|
|
a.IsTrue(list.Contains(iputils.ToBytes("192.168.2.100")))
|
|
|
|
|
a.IsFalse(list.Contains(iputils.ToBytes("192.169.3.100")))
|
|
|
|
|
a.IsFalse(list.Contains(iputils.ToBytes("192.167.3.100")))
|
|
|
|
|
a.IsTrue(list.Contains(iputils.ToBytes("192.167.2.100")))
|
|
|
|
|
a.IsTrue(list.Contains(iputils.ToBytes("::1")))
|
|
|
|
|
a.IsTrue(list.Contains(iputils.ToBytes("::3")))
|
|
|
|
|
a.IsFalse(list.Contains(iputils.ToBytes("::8")))
|
2021-10-04 17:42:38 +08:00
|
|
|
t.Log(time.Since(before).Seconds()*1000, "ms")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestIPList_Contains_Many(t *testing.T) {
|
2024-03-30 14:42:56 +08:00
|
|
|
var list = iplibrary.NewIPList()
|
2021-10-04 17:42:38 +08:00
|
|
|
for i := 0; i < 1_000_000; i++ {
|
2024-03-30 14:42:56 +08:00
|
|
|
list.AddDelay(&iplibrary.IPItem{
|
2022-07-25 20:23:40 +08:00
|
|
|
Id: uint64(i),
|
2024-04-06 15:37:14 +08:00
|
|
|
IPFrom: iputils.ToBytes(strconv.Itoa(rands.Int(0, 255)) + "." + strconv.Itoa(rands.Int(0, 255)) + "." + strconv.Itoa(rands.Int(0, 255)) + "." + strconv.Itoa(rands.Int(0, 255))),
|
|
|
|
|
IPTo: iputils.ToBytes(strconv.Itoa(rands.Int(0, 255)) + "." + strconv.Itoa(rands.Int(0, 255)) + "." + strconv.Itoa(rands.Int(0, 255)) + "." + strconv.Itoa(rands.Int(0, 255))),
|
2021-10-04 17:42:38 +08:00
|
|
|
ExpiredAt: 0,
|
|
|
|
|
})
|
|
|
|
|
}
|
2024-03-30 14:42:56 +08:00
|
|
|
|
|
|
|
|
var before = time.Now()
|
2024-04-06 10:07:39 +08:00
|
|
|
list.Sort()
|
2024-03-30 14:42:56 +08:00
|
|
|
t.Log("sort cost:", time.Since(before).Seconds()*1000, "ms")
|
|
|
|
|
t.Log(len(list.ItemsMap()), "ip")
|
|
|
|
|
|
|
|
|
|
before = time.Now()
|
2024-04-06 15:37:14 +08:00
|
|
|
_ = list.Contains(iputils.ToBytes("192.168.1.100"))
|
2024-03-30 14:42:56 +08:00
|
|
|
t.Log("contains cost:", time.Since(before).Seconds()*1000, "ms")
|
2021-01-03 20:18:47 +08:00
|
|
|
}
|
|
|
|
|
|
2021-02-02 15:26:00 +08:00
|
|
|
func TestIPList_ContainsAll(t *testing.T) {
|
2024-03-30 14:42:56 +08:00
|
|
|
var a = assert.NewAssertion(t)
|
|
|
|
|
|
2024-04-21 10:32:04 +08:00
|
|
|
{
|
|
|
|
|
var list = iplibrary.NewIPList()
|
|
|
|
|
list.Add(&iplibrary.IPItem{
|
|
|
|
|
Id: 1,
|
|
|
|
|
Type: "all",
|
|
|
|
|
IPFrom: nil,
|
|
|
|
|
})
|
|
|
|
|
var b = list.Contains(iputils.ToBytes("192.168.1.1"))
|
|
|
|
|
a.IsTrue(b)
|
2021-02-02 15:26:00 +08:00
|
|
|
|
2024-04-21 10:32:04 +08:00
|
|
|
list.Delete(1)
|
2021-02-02 15:26:00 +08:00
|
|
|
|
2024-04-21 10:32:04 +08:00
|
|
|
b = list.Contains(iputils.ToBytes("192.168.1.1"))
|
|
|
|
|
a.IsFalse(b)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
var list = iplibrary.NewIPList()
|
|
|
|
|
list.Add(&iplibrary.IPItem{
|
|
|
|
|
Id: 1,
|
|
|
|
|
Type: "all",
|
|
|
|
|
IPFrom: iputils.ToBytes("0.0.0.0"),
|
|
|
|
|
})
|
|
|
|
|
var b = list.Contains(iputils.ToBytes("192.168.1.1"))
|
|
|
|
|
a.IsTrue(b)
|
|
|
|
|
|
|
|
|
|
list.Delete(1)
|
|
|
|
|
|
|
|
|
|
b = list.Contains(iputils.ToBytes("192.168.1.1"))
|
|
|
|
|
a.IsFalse(b)
|
|
|
|
|
}
|
2021-02-02 15:26:00 +08:00
|
|
|
}
|
|
|
|
|
|
2021-10-04 17:42:38 +08:00
|
|
|
func TestIPList_ContainsIPStrings(t *testing.T) {
|
|
|
|
|
var a = assert.NewAssertion(t)
|
|
|
|
|
|
2024-03-30 14:42:56 +08:00
|
|
|
var list = iplibrary.NewIPList()
|
2021-10-04 17:42:38 +08:00
|
|
|
for i := 0; i < 255; i++ {
|
2024-03-30 14:42:56 +08:00
|
|
|
list.Add(&iplibrary.IPItem{
|
2022-07-25 20:23:40 +08:00
|
|
|
Id: uint64(i),
|
2024-04-06 15:37:14 +08:00
|
|
|
IPFrom: iputils.ToBytes(strconv.Itoa(i) + ".168.0.1"),
|
|
|
|
|
IPTo: iputils.ToBytes(strconv.Itoa(i) + ".168.255.1"),
|
2021-10-04 17:42:38 +08:00
|
|
|
ExpiredAt: 0,
|
|
|
|
|
})
|
|
|
|
|
}
|
2024-03-30 14:42:56 +08:00
|
|
|
t.Log(len(list.ItemsMap()), "ip")
|
2021-10-04 17:42:38 +08:00
|
|
|
|
|
|
|
|
{
|
|
|
|
|
item, ok := list.ContainsIPStrings([]string{"192.168.1.100"})
|
|
|
|
|
t.Log("item:", item)
|
|
|
|
|
a.IsTrue(ok)
|
|
|
|
|
}
|
|
|
|
|
{
|
|
|
|
|
item, ok := list.ContainsIPStrings([]string{"192.167.1.100"})
|
|
|
|
|
t.Log("item:", item)
|
|
|
|
|
a.IsFalse(ok)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-03 20:18:47 +08:00
|
|
|
func TestIPList_Delete(t *testing.T) {
|
2024-03-30 14:42:56 +08:00
|
|
|
var list = iplibrary.NewIPList()
|
|
|
|
|
list.Add(&iplibrary.IPItem{
|
2021-01-03 20:18:47 +08:00
|
|
|
Id: 1,
|
2024-04-06 15:37:14 +08:00
|
|
|
IPFrom: iputils.ToBytes("192.168.0.1"),
|
2021-01-03 20:18:47 +08:00
|
|
|
ExpiredAt: 0,
|
|
|
|
|
})
|
2024-03-30 14:42:56 +08:00
|
|
|
list.Add(&iplibrary.IPItem{
|
2021-01-03 20:18:47 +08:00
|
|
|
Id: 2,
|
2024-04-06 15:37:14 +08:00
|
|
|
IPFrom: iputils.ToBytes("192.168.0.1"),
|
2024-03-30 14:42:56 +08:00
|
|
|
ExpiredAt: 0,
|
|
|
|
|
})
|
|
|
|
|
list.Add(&iplibrary.IPItem{
|
|
|
|
|
Id: 3,
|
2024-04-06 15:37:14 +08:00
|
|
|
IPFrom: iputils.ToBytes("192.168.1.1"),
|
|
|
|
|
IPTo: iputils.ToBytes("192.168.2.1"),
|
2021-01-03 20:18:47 +08:00
|
|
|
ExpiredAt: 0,
|
|
|
|
|
})
|
2024-03-30 14:42:56 +08:00
|
|
|
t.Log("===before===")
|
|
|
|
|
logs.PrintAsJSON(list.ItemsMap(), t)
|
|
|
|
|
logs.PrintAsJSON(list.AllItemsMap(), t)
|
|
|
|
|
logs.PrintAsJSON(list.SortedRangeItems())
|
|
|
|
|
logs.PrintAsJSON(list.IPMap(), t)
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
var found bool
|
|
|
|
|
for _, item := range list.SortedRangeItems() {
|
|
|
|
|
if item.Id == 3 {
|
|
|
|
|
found = true
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if !found {
|
|
|
|
|
t.Fatal("should be found")
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-03 20:18:47 +08:00
|
|
|
|
|
|
|
|
list.Delete(1)
|
|
|
|
|
|
2024-03-30 14:42:56 +08:00
|
|
|
t.Log("===after===")
|
|
|
|
|
logs.PrintAsJSON(list.ItemsMap(), t)
|
|
|
|
|
logs.PrintAsJSON(list.AllItemsMap(), t)
|
|
|
|
|
logs.PrintAsJSON(list.SortedRangeItems())
|
|
|
|
|
logs.PrintAsJSON(list.IPMap(), t)
|
|
|
|
|
|
|
|
|
|
list.Delete(3)
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
var found bool
|
|
|
|
|
for _, item := range list.SortedRangeItems() {
|
|
|
|
|
if item.Id == 3 {
|
|
|
|
|
found = true
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if found {
|
|
|
|
|
t.Fatal("should be not found")
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-03 20:18:47 +08:00
|
|
|
}
|
|
|
|
|
|
2024-03-30 14:42:56 +08:00
|
|
|
func TestIPList_GC(t *testing.T) {
|
|
|
|
|
var a = assert.NewAssertion(t)
|
|
|
|
|
|
|
|
|
|
var list = iplibrary.NewIPList()
|
|
|
|
|
list.Add(&iplibrary.IPItem{
|
2021-01-03 20:18:47 +08:00
|
|
|
Id: 1,
|
2024-04-06 15:37:14 +08:00
|
|
|
IPFrom: iputils.ToBytes("192.168.1.100"),
|
|
|
|
|
IPTo: iputils.ToBytes("192.168.1.101"),
|
2021-01-03 20:18:47 +08:00
|
|
|
ExpiredAt: time.Now().Unix() + 1,
|
|
|
|
|
})
|
2024-03-30 14:42:56 +08:00
|
|
|
list.Add(&iplibrary.IPItem{
|
2021-01-03 20:18:47 +08:00
|
|
|
Id: 2,
|
2024-04-06 15:37:14 +08:00
|
|
|
IPFrom: iputils.ToBytes("192.168.1.102"),
|
|
|
|
|
IPTo: iputils.ToBytes("192.168.1.103"),
|
2021-01-03 20:18:47 +08:00
|
|
|
ExpiredAt: 0,
|
|
|
|
|
})
|
2024-03-30 14:42:56 +08:00
|
|
|
logs.PrintAsJSON(list.ItemsMap(), t)
|
|
|
|
|
logs.PrintAsJSON(list.AllItemsMap(), t)
|
|
|
|
|
|
|
|
|
|
time.Sleep(3 * time.Second)
|
2021-01-03 20:18:47 +08:00
|
|
|
|
|
|
|
|
t.Log("===AFTER GC===")
|
2024-03-30 14:42:56 +08:00
|
|
|
logs.PrintAsJSON(list.ItemsMap(), t)
|
|
|
|
|
logs.PrintAsJSON(list.SortedRangeItems(), t)
|
|
|
|
|
|
|
|
|
|
a.IsTrue(len(list.ItemsMap()) == 1)
|
|
|
|
|
a.IsTrue(len(list.SortedRangeItems()) == 1)
|
2020-11-09 10:45:44 +08:00
|
|
|
}
|
|
|
|
|
|
2024-04-06 10:07:39 +08:00
|
|
|
func TestManyLists(t *testing.T) {
|
2021-12-08 15:17:45 +08:00
|
|
|
debug.SetMaxThreads(20)
|
|
|
|
|
|
2024-03-30 14:42:56 +08:00
|
|
|
var lists = []*iplibrary.IPList{}
|
2021-12-08 15:17:45 +08:00
|
|
|
var locker = &sync.Mutex{}
|
|
|
|
|
for i := 0; i < 1000; i++ {
|
|
|
|
|
locker.Lock()
|
2024-03-30 14:42:56 +08:00
|
|
|
lists = append(lists, iplibrary.NewIPList())
|
2021-12-08 15:17:45 +08:00
|
|
|
locker.Unlock()
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-30 14:42:56 +08:00
|
|
|
if testutils.IsSingleTesting() {
|
|
|
|
|
time.Sleep(3 * time.Second)
|
|
|
|
|
}
|
2021-12-08 15:17:45 +08:00
|
|
|
t.Log(runtime.NumGoroutine())
|
|
|
|
|
t.Log(len(lists), "lists")
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-30 14:42:56 +08:00
|
|
|
func BenchmarkIPList_Add(b *testing.B) {
|
2020-11-09 10:45:44 +08:00
|
|
|
runtime.GOMAXPROCS(1)
|
|
|
|
|
|
2024-03-30 14:42:56 +08:00
|
|
|
var list = iplibrary.NewIPList()
|
2022-03-17 19:48:04 +08:00
|
|
|
for i := 1; i < 200_000; i++ {
|
2024-03-30 14:42:56 +08:00
|
|
|
list.AddDelay(&iplibrary.IPItem{
|
2022-07-25 20:23:40 +08:00
|
|
|
Id: uint64(i),
|
2024-04-06 15:37:14 +08:00
|
|
|
IPFrom: iputils.ToBytes(strconv.Itoa(rands.Int(0, 255)) + "." + strconv.Itoa(rands.Int(0, 255)) + ".0.1"),
|
|
|
|
|
IPTo: iputils.ToBytes(strconv.Itoa(rands.Int(0, 255)) + "." + strconv.Itoa(rands.Int(0, 255)) + ".0.1"),
|
2020-11-09 10:45:44 +08:00
|
|
|
ExpiredAt: time.Now().Unix() + 60,
|
|
|
|
|
})
|
|
|
|
|
}
|
2024-03-30 14:42:56 +08:00
|
|
|
|
2022-03-17 19:48:04 +08:00
|
|
|
list.Sort()
|
|
|
|
|
|
2024-03-30 14:42:56 +08:00
|
|
|
b.Log(len(list.ItemsMap()), "ip")
|
2022-03-17 19:48:04 +08:00
|
|
|
|
|
|
|
|
b.ResetTimer()
|
2024-03-30 14:42:56 +08:00
|
|
|
|
2020-11-09 10:45:44 +08:00
|
|
|
for i := 0; i < b.N; i++ {
|
2024-03-30 14:42:56 +08:00
|
|
|
var ip = fmt.Sprintf("%d.%d.%d.%d", rand.Int()%255, rand.Int()%255, rand.Int()%255, rand.Int()%255)
|
|
|
|
|
list.Add(&iplibrary.IPItem{
|
|
|
|
|
Type: "",
|
|
|
|
|
Id: uint64(i % 1_000_000),
|
2024-04-06 15:37:14 +08:00
|
|
|
IPFrom: iputils.ToBytes(ip),
|
2024-04-06 10:07:39 +08:00
|
|
|
IPTo: nil,
|
2024-03-30 14:42:56 +08:00
|
|
|
ExpiredAt: fasttime.Now().Unix() + 3600,
|
|
|
|
|
EventLevel: "",
|
|
|
|
|
})
|
2020-11-09 10:45:44 +08:00
|
|
|
}
|
|
|
|
|
}
|
2024-03-30 14:42:56 +08:00
|
|
|
|
|
|
|
|
func BenchmarkIPList_Contains(b *testing.B) {
|
|
|
|
|
runtime.GOMAXPROCS(1)
|
|
|
|
|
|
|
|
|
|
var list = iplibrary.NewIPList()
|
|
|
|
|
for i := 1; i < 1_000_000; i++ {
|
|
|
|
|
var item = &iplibrary.IPItem{
|
|
|
|
|
Id: uint64(i),
|
2024-04-06 15:37:14 +08:00
|
|
|
IPFrom: iputils.ToBytes(strconv.Itoa(rands.Int(0, 255)) + "." + strconv.Itoa(rands.Int(0, 255)) + ".0.1"),
|
2024-03-30 14:42:56 +08:00
|
|
|
ExpiredAt: time.Now().Unix() + 60,
|
|
|
|
|
}
|
2024-04-06 10:07:39 +08:00
|
|
|
if i%100 == 0 {
|
2024-04-06 15:37:14 +08:00
|
|
|
item.IPTo = iputils.ToBytes(strconv.Itoa(rands.Int(0, 255)) + "." + strconv.Itoa(rands.Int(0, 255)) + ".0.1")
|
2024-03-30 14:42:56 +08:00
|
|
|
}
|
|
|
|
|
list.Add(item)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//b.Log(len(list.ItemsMap()), "ip")
|
|
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
|
for pb.Next() {
|
2024-04-06 15:37:14 +08:00
|
|
|
_ = list.Contains(iputils.ToBytes(testutils.RandIP()))
|
2024-03-30 14:42:56 +08:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func BenchmarkIPList_Sort(b *testing.B) {
|
|
|
|
|
var list = iplibrary.NewIPList()
|
|
|
|
|
for i := 0; i < 1_000_000; i++ {
|
|
|
|
|
var item = &iplibrary.IPItem{
|
|
|
|
|
Id: uint64(i),
|
2024-04-06 15:37:14 +08:00
|
|
|
IPFrom: iputils.ToBytes(strconv.Itoa(rands.Int(0, 255)) + "." + strconv.Itoa(rands.Int(0, 255)) + ".0.1"),
|
2024-03-30 14:42:56 +08:00
|
|
|
ExpiredAt: time.Now().Unix() + 60,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if i%100 == 0 {
|
2024-04-06 15:37:14 +08:00
|
|
|
item.IPTo = iputils.ToBytes(strconv.Itoa(rands.Int(0, 255)) + "." + strconv.Itoa(rands.Int(0, 255)) + ".0.1")
|
2024-03-30 14:42:56 +08:00
|
|
|
}
|
|
|
|
|
|
2024-04-06 10:07:39 +08:00
|
|
|
list.AddDelay(item)
|
2024-03-30 14:42:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
|
for pb.Next() {
|
|
|
|
|
list.Sort()
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|