-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathbench_test.go
79 lines (69 loc) · 1.33 KB
/
bench_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
package netlink_test
import (
"testing"
"github.com/mdlayher/netlink"
)
var attrBench = []struct {
name string
attrs []netlink.Attribute
}{
{
name: "0",
},
{
name: "1",
attrs: makeAttributes(1),
},
{
name: "8",
attrs: makeAttributes(8),
},
{
name: "64",
attrs: makeAttributes(64),
},
{
name: "512",
attrs: makeAttributes(512),
},
}
func BenchmarkMarshalAttributes(b *testing.B) {
for _, tt := range attrBench {
b.Run(tt.name, func(b *testing.B) {
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := netlink.MarshalAttributes(tt.attrs); err != nil {
b.Fatalf("failed to marshal: %v", err)
}
}
})
}
}
func BenchmarkUnmarshalAttributes(b *testing.B) {
for _, tt := range attrBench {
b.Run(tt.name, func(b *testing.B) {
buf, err := netlink.MarshalAttributes(tt.attrs)
if err != nil {
b.Fatalf("failed to marshal: %v", err)
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := netlink.UnmarshalAttributes(buf); err != nil {
b.Fatalf("failed to unmarshal: %v", err)
}
}
})
}
}
func makeAttributes(n int) []netlink.Attribute {
attrs := make([]netlink.Attribute, 0, n)
for i := 0; i < n; i++ {
attrs = append(attrs, netlink.Attribute{
Type: uint16(i),
Data: make([]byte, n),
})
}
return attrs
}