-
Notifications
You must be signed in to change notification settings - Fork 42
/
prefix_benchmark_test.go
102 lines (97 loc) · 2.22 KB
/
prefix_benchmark_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
96
97
98
99
100
101
102
package ipam
import (
"context"
"fmt"
"testing"
)
func BenchmarkNewPrefix(b *testing.B) {
ctx := context.Background()
benchWithBackends(b, func(b *testing.B, ipam *ipamer) {
for range b.N {
p, err := ipam.NewPrefix(ctx, "192.168.0.0/24")
if err != nil {
panic(err)
}
if p == nil {
panic("Prefix nil")
}
_, err = ipam.DeletePrefix(ctx, p.Cidr)
if err != nil {
panic(err)
}
}
})
}
func BenchmarkAcquireIP(b *testing.B) {
ctx := context.Background()
testCidr := "10.0.0.0/16"
benchWithBackends(b, func(b *testing.B, ipam *ipamer) {
p, err := ipam.NewPrefix(ctx, testCidr)
if err != nil {
panic(err)
}
for range b.N {
ip, err := ipam.AcquireIP(ctx, p.Cidr)
if err != nil {
panic(err)
}
if ip == nil {
panic("IP nil")
}
p, err = ipam.ReleaseIP(ctx, ip)
if err != nil {
panic(err)
}
}
_, err = ipam.DeletePrefix(ctx, testCidr)
if err != nil {
b.Fatalf("error deleting prefix:%v", err)
}
})
}
func BenchmarkAcquireChildPrefix(b *testing.B) {
ctx := context.Background()
benchmarks := []struct {
name string
parentLength uint8
childLength uint8
}{
{name: "8/14", parentLength: 8, childLength: 14},
{name: "8/24", parentLength: 8, childLength: 24},
{name: "16/18", parentLength: 16, childLength: 18},
{name: "16/26", parentLength: 16, childLength: 26},
}
for _, bm := range benchmarks {
test := bm
benchWithBackends(b, func(b *testing.B, ipam *ipamer) {
p, err := ipam.NewPrefix(ctx, fmt.Sprintf("192.168.0.0/%d", test.parentLength))
if err != nil {
panic(err)
}
for range b.N {
p, err := ipam.AcquireChildPrefix(ctx, p.Cidr, test.childLength)
if err != nil {
panic(err)
}
err = ipam.ReleaseChildPrefix(ctx, p)
if err != nil {
panic(err)
}
}
_, err = ipam.DeletePrefix(ctx, p.Cidr)
if err != nil {
b.Fatalf("error deleting prefix:%v", err)
}
})
}
}
func BenchmarkPrefixOverlapping(b *testing.B) {
existingPrefixes := []string{"192.168.0.0/24", "10.0.0.0/8"}
newPrefixes := []string{"192.168.1.0/24", "11.0.0.0/8"}
for range b.N {
err := PrefixesOverlapping(existingPrefixes, newPrefixes)
if err != nil {
b.Errorf("PrefixOverLapping error:%v", err)
}
}
}