Skip to content

Commit

Permalink
Merge pull request #1 from ldkingvivi/use_netip
Browse files Browse the repository at this point in the history
Use netip from yl2chen#48
  • Loading branch information
ldkingvivi authored Mar 23, 2024
2 parents c4996e1 + e1b2462 commit d5fcd23
Show file tree
Hide file tree
Showing 6 changed files with 782 additions and 22 deletions.
193 changes: 177 additions & 16 deletions cidranger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ package cidranger

import (
"encoding/json"
iptrie "github.com/yl2chen/cidranger/iptire"
"io/ioutil"
"math/rand"
"net"
"net/netip"
"testing"
"time"

"github.com/stretchr/testify/assert"
rnet "github.com/yl2chen/cidranger/net"
"go4.org/netipx"
)

/*
Expand Down Expand Up @@ -51,10 +54,12 @@ func testContainsAgainstBase(t *testing.T, iterations int, ipGen ipGenerator) {
}
rangers := []Ranger{NewPCTrieRanger()}
baseRanger := newBruteRanger()
trie := iptrie.NewTrie[struct{}]()
for _, ranger := range rangers {
configureRangerWithAWSRanges(t, ranger)
}
configureRangerWithAWSRanges(t, baseRanger)
configureTrieWithAWSRanges(t, trie)

for i := 0; i < iterations; i++ {
nn := ipGen()
Expand All @@ -65,7 +70,25 @@ func testContainsAgainstBase(t *testing.T, iterations int, ipGen ipGenerator) {
assert.NoError(t, err)
assert.Equal(t, expected, actual)
}
addr, ok := netip.AddrFromSlice(nn.ToIP())
if !ok {
t.Errorf("netip addr convert fail")
continue
}
got := trie.Find(addr)
var gotvalue bool
if got != nil {
gotvalue = true
}
assert.Equal(t, expected, gotvalue)
}
}

func testNormalizePrefix(pfx netip.Prefix) netip.Prefix {
if pfx.Addr().Is4() {
pfx = netip.PrefixFrom(netip.AddrFrom16(pfx.Addr().As16()), pfx.Bits()+96)
}
return pfx.Masked()
}

func testContainingNetworksAgainstBase(t *testing.T, iterations int, ipGen ipGenerator) {
Expand All @@ -74,10 +97,13 @@ func testContainingNetworksAgainstBase(t *testing.T, iterations int, ipGen ipGen
}
rangers := []Ranger{NewPCTrieRanger()}
baseRanger := newBruteRanger()
trie := iptrie.NewTrie[struct{}]()

for _, ranger := range rangers {
configureRangerWithAWSRanges(t, ranger)
}
configureRangerWithAWSRanges(t, baseRanger)
configureTrieWithAWSRanges(t, trie)

for i := 0; i < iterations; i++ {
nn := ipGen()
Expand All @@ -91,6 +117,35 @@ func testContainingNetworksAgainstBase(t *testing.T, iterations int, ipGen ipGen
assert.Contains(t, expected, network)
}
}

addr, ok := netip.AddrFromSlice(nn.ToIP())
if !ok {
t.Errorf("netip addr convert fail")
continue
}
got := trie.ContainingNetworks(addr)
assert.Equal(t, len(expected), len(got))
builderExpected := new(netipx.IPSetBuilder)
builderGot := new(netipx.IPSetBuilder)

for _, p := range expected {
n := p.Network()
prefix, ok := netipx.FromStdIPNet(&n)
if !ok {
t.Errorf("netip addr convert fail")
}
builderExpected.AddPrefix(testNormalizePrefix(prefix))
}
expSet, err := builderExpected.IPSet()

for _, g := range got {
builderGot.AddPrefix(g.Network)
}
gotSet, err := builderGot.IPSet()

if !expSet.Equal(gotSet) {
t.Errorf("not same set")
}
}
}

Expand All @@ -100,10 +155,13 @@ func testCoversNetworksAgainstBase(t *testing.T, iterations int, netGen networkG
}
rangers := []Ranger{NewPCTrieRanger()}
baseRanger := newBruteRanger()
trie := iptrie.NewTrie[struct{}]()

for _, ranger := range rangers {
configureRangerWithAWSRanges(t, ranger)
}
configureRangerWithAWSRanges(t, baseRanger)
configureTrieWithAWSRanges(t, trie)

for i := 0; i < iterations; i++ {
network := netGen()
Expand All @@ -117,6 +175,35 @@ func testCoversNetworksAgainstBase(t *testing.T, iterations int, netGen networkG
assert.Contains(t, expected, network)
}
}

queryPrefix, ok := netipx.FromStdIPNet(&network.IPNet)
if !ok {
t.Errorf("netip addr convert fail")
}

got := trie.CoveredNetworks(queryPrefix)
assert.Equal(t, len(expected), len(got))
builderExpected := new(netipx.IPSetBuilder)
builderGot := new(netipx.IPSetBuilder)

for _, p := range expected {
n := p.Network()
prefix, ok := netipx.FromStdIPNet(&n)
if !ok {
t.Errorf("netip addr convert fail")
}
builderExpected.AddPrefix(testNormalizePrefix(prefix))
}
expSet, err := builderExpected.IPSet()

for _, g := range got {
builderGot.AddPrefix(g.Network)
}
gotSet, err := builderGot.IPSet()

if !expSet.Equal(gotSet) {
t.Errorf("not same set")
}
}
}

Expand All @@ -129,57 +216,65 @@ func testCoversNetworksAgainstBase(t *testing.T, iterations int, netGen networkG
func BenchmarkPCTrieHitIPv4UsingAWSRanges(b *testing.B) {
benchmarkContainsUsingAWSRanges(b, net.ParseIP("52.95.110.1"), NewPCTrieRanger())
}
func BenchmarkBruteRangerHitIPv4UsingAWSRanges(b *testing.B) {
benchmarkContainsUsingAWSRanges(b, net.ParseIP("52.95.110.1"), newBruteRanger())

func BenchmarkTrieHitIPv4UsingAWSRanges(b *testing.B) {
benchmarkTrieContainsUsingAWSRanges(b, netip.MustParseAddr("52.95.110.1"), iptrie.NewTrie[struct{}]())
}

func BenchmarkPCTrieHitIPv6UsingAWSRanges(b *testing.B) {
benchmarkContainsUsingAWSRanges(b, net.ParseIP("2620:107:300f::36b7:ff81"), NewPCTrieRanger())
}
func BenchmarkBruteRangerHitIPv6UsingAWSRanges(b *testing.B) {
benchmarkContainsUsingAWSRanges(b, net.ParseIP("2620:107:300f::36b7:ff81"), newBruteRanger())

func BenchmarkTrieHitIPv6UsingAWSRanges(b *testing.B) {
benchmarkTrieContainsUsingAWSRanges(b, netip.MustParseAddr("2620:107:300f::36b7:ff81"), iptrie.NewTrie[struct{}]())
}

func BenchmarkPCTrieMissIPv4UsingAWSRanges(b *testing.B) {
benchmarkContainsUsingAWSRanges(b, net.ParseIP("123.123.123.123"), NewPCTrieRanger())
}
func BenchmarkBruteRangerMissIPv4UsingAWSRanges(b *testing.B) {
benchmarkContainsUsingAWSRanges(b, net.ParseIP("123.123.123.123"), newBruteRanger())

func BenchmarkTrieMissIPv4UsingAWSRanges(b *testing.B) {
benchmarkTrieContainsUsingAWSRanges(b, netip.MustParseAddr("123.123.123.123"), iptrie.NewTrie[struct{}]())
}

func BenchmarkPCTrieHMissIPv6UsingAWSRanges(b *testing.B) {
benchmarkContainsUsingAWSRanges(b, net.ParseIP("2620::ffff"), NewPCTrieRanger())
}
func BenchmarkBruteRangerMissIPv6UsingAWSRanges(b *testing.B) {
benchmarkContainsUsingAWSRanges(b, net.ParseIP("2620::ffff"), newBruteRanger())

func BenchmarkTrieHMissIPv6UsingAWSRanges(b *testing.B) {
benchmarkTrieContainsUsingAWSRanges(b, netip.MustParseAddr("2620::ffff"), iptrie.NewTrie[struct{}]())
}

func BenchmarkPCTrieHitContainingNetworksIPv4UsingAWSRanges(b *testing.B) {
benchmarkContainingNetworksUsingAWSRanges(b, net.ParseIP("52.95.110.1"), NewPCTrieRanger())
}
func BenchmarkBruteRangerHitContainingNetworksIPv4UsingAWSRanges(b *testing.B) {
benchmarkContainingNetworksUsingAWSRanges(b, net.ParseIP("52.95.110.1"), newBruteRanger())

func BenchmarkTrieHitContainingNetworksIPv4UsingAWSRanges(b *testing.B) {
benchmarkTrieContainingNetworksUsingAWSRanges(b, netip.MustParseAddr("52.95.110.1"), iptrie.NewTrie[struct{}]())
}

func BenchmarkPCTrieHitContainingNetworksIPv6UsingAWSRanges(b *testing.B) {
benchmarkContainingNetworksUsingAWSRanges(b, net.ParseIP("2620:107:300f::36b7:ff81"), NewPCTrieRanger())
}
func BenchmarkBruteRangerHitContainingNetworksIPv6UsingAWSRanges(b *testing.B) {
benchmarkContainingNetworksUsingAWSRanges(b, net.ParseIP("2620:107:300f::36b7:ff81"), newBruteRanger())

func BenchmarkTrieHitContainingNetworksIPv6UsingAWSRanges(b *testing.B) {
benchmarkTrieContainingNetworksUsingAWSRanges(b, netip.MustParseAddr("2620:107:300f::36b7:ff81"), iptrie.NewTrie[struct{}]())
}

func BenchmarkPCTrieMissContainingNetworksIPv4UsingAWSRanges(b *testing.B) {
benchmarkContainingNetworksUsingAWSRanges(b, net.ParseIP("123.123.123.123"), NewPCTrieRanger())
}
func BenchmarkBruteRangerMissContainingNetworksIPv4UsingAWSRanges(b *testing.B) {
benchmarkContainingNetworksUsingAWSRanges(b, net.ParseIP("123.123.123.123"), newBruteRanger())

func BenchmarkTrieMissContainingNetworksIPv4UsingAWSRanges(b *testing.B) {
benchmarkTrieContainingNetworksUsingAWSRanges(b, netip.MustParseAddr("123.123.123.123"), iptrie.NewTrie[struct{}]())
}

func BenchmarkPCTrieHMissContainingNetworksIPv6UsingAWSRanges(b *testing.B) {
benchmarkContainingNetworksUsingAWSRanges(b, net.ParseIP("2620::ffff"), NewPCTrieRanger())
}
func BenchmarkBruteRangerMissContainingNetworksIPv6UsingAWSRanges(b *testing.B) {
benchmarkContainingNetworksUsingAWSRanges(b, net.ParseIP("2620::ffff"), newBruteRanger())

func BenchmarkTrieHMissContainingNetworksIPv6UsingAWSRanges(b *testing.B) {
benchmarkTrieContainingNetworksUsingAWSRanges(b, netip.MustParseAddr("2620::ffff"), iptrie.NewTrie[struct{}]())
}

func BenchmarkNewPathprefixTriev4(b *testing.B) {
Expand All @@ -190,6 +285,38 @@ func BenchmarkNewPathprefixTriev6(b *testing.B) {
benchmarkNewPathprefixTrie(b, "8000::/24")
}

func BenchmarkPCTLoad(b *testing.B) {
ranger := NewPCTrieRanger()
b.ResetTimer()

for n := 0; n < b.N; n++ {
for _, prefix := range awsRanges.Prefixes {
_, network, _ := net.ParseCIDR(prefix.IPPrefix)
_ = ranger.Insert(NewBasicRangerEntry(*network))
}
for _, prefix := range awsRanges.IPv6Prefixes {
_, network, _ := net.ParseCIDR(prefix.IPPrefix)
_ = ranger.Insert(NewBasicRangerEntry(*network))
}
}
}

func BenchmarkTrieLoad(b *testing.B) {
trie := iptrie.NewTrie[struct{}]()
b.ResetTimer()

for n := 0; n < b.N; n++ {
for _, prefix := range awsRanges.Prefixes {
network, _ := netip.ParsePrefix(prefix.IPPrefix)
trie.Insert(network, &struct{}{})
}
for _, prefix := range awsRanges.IPv6Prefixes {
network, _ := netip.ParsePrefix(prefix.IPPrefix)
trie.Insert(network, &struct{}{})
}
}
}

func benchmarkContainsUsingAWSRanges(tb testing.TB, nn net.IP, ranger Ranger) {
configureRangerWithAWSRanges(tb, ranger)
for n := 0; n < tb.(*testing.B).N; n++ {
Expand Down Expand Up @@ -217,6 +344,27 @@ func benchmarkNewPathprefixTrie(b *testing.B, net1 string) {
}
}

func benchmarkTrieContainsUsingAWSRanges(tb testing.TB, nn netip.Addr, trie *iptrie.Trie[struct{}]) {
configureTrieWithAWSRanges(tb, trie)
for n := 0; n < tb.(*testing.B).N; n++ {
trie.Contains(nn)
}
}

func benchmarkTrieFindUsingAWSRanges(tb testing.TB, nn netip.Addr, trie *iptrie.Trie[struct{}]) {
configureTrieWithAWSRanges(tb, trie)
for n := 0; n < tb.(*testing.B).N; n++ {
trie.Find(nn)
}
}

func benchmarkTrieContainingNetworksUsingAWSRanges(tb testing.TB, nn netip.Addr, trie *iptrie.Trie[struct{}]) {
configureTrieWithAWSRanges(tb, trie)
for n := 0; n < tb.(*testing.B).N; n++ {
trie.ContainingNetworks(nn)
}
}

/*
******************************************************************
Helper methods and initialization.
Expand Down Expand Up @@ -299,6 +447,19 @@ func configureRangerWithAWSRanges(tb testing.TB, ranger Ranger) {
}
}

func configureTrieWithAWSRanges(tb testing.TB, trie *iptrie.Trie[struct{}]) {
for _, prefix := range awsRanges.Prefixes {
network, err := netip.ParsePrefix(prefix.IPPrefix)
assert.NoError(tb, err)
trie.Insert(network, &struct{}{})
}
for _, prefix := range awsRanges.IPv6Prefixes {
network, err := netip.ParsePrefix(prefix.IPPrefix)
assert.NoError(tb, err)
trie.Insert(network, &struct{}{})
}
}

func init() {
awsRanges = loadAWSRanges()
for _, prefix := range awsRanges.IPv6Prefixes {
Expand Down
10 changes: 8 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
module github.com/yl2chen/cidranger

go 1.13
go 1.21

require (
github.com/stretchr/testify v1.6.1
gopkg.in/yaml.v2 v2.2.2 // indirect
go4.org/netipx v0.0.0-20231129151722-fdeea329fbba
)

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
)
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
go4.org/netipx v0.0.0-20231129151722-fdeea329fbba h1:0b9z3AuHCjxk0x/opv64kcgZLBseWJUpBw5I82+2U4M=
go4.org/netipx v0.0.0-20231129151722-fdeea329fbba/go.mod h1:PLyyIXexvUFg3Owu6p/WfdlivPbZJsZdgWZlrGope/Y=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Loading

0 comments on commit d5fcd23

Please sign in to comment.