| 1 |
package btf |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
|
| 6 |
"github.com/go-quicktest/qt" |
| 7 |
) |
| 8 |
|
| 9 |
func countTypes(typs ...Type) int { |
| 10 |
i := 0 |
| 11 |
visited := make(map[Type]struct{}) |
| 12 |
for _, typ := range typs { |
| 13 |
for range postorder(typ, visited) { |
| 14 |
i++ |
| 15 |
} |
| 16 |
} |
| 17 |
return i |
| 18 |
} |
| 19 |
|
| 20 |
func TestDedupSKBuff(t *testing.T) { |
| 21 |
vmlinux := vmlinuxTestdataBytes(t) |
| 22 |
spec, err := loadRawSpec(vmlinux, nil) |
| 23 |
qt.Assert(t, qt.IsNil(err)) |
| 24 |
|
| 25 |
var skBuffOne *Struct |
| 26 |
qt.Assert(t, qt.IsNil(spec.TypeByName("sk_buff", &skBuffOne))) |
| 27 |
|
| 28 |
skbCount := countTypes(skBuffOne) |
| 29 |
|
| 30 |
spec = spec.Copy() |
| 31 |
var skBuffTwo *Struct |
| 32 |
qt.Assert(t, qt.IsNil(spec.TypeByName("sk_buff", &skBuffTwo))) |
| 33 |
|
| 34 |
deduper := newDeduper() |
| 35 |
|
| 36 |
types := []Type{skBuffOne, skBuffTwo} |
| 37 |
for i, typ := range types { |
| 38 |
types[i], err = deduper.deduplicate(typ) |
| 39 |
} |
| 40 |
qt.Assert(t, qt.IsNil(err)) |
| 41 |
|
| 42 |
dedupedCount := countTypes(types...) |
| 43 |
qt.Assert(t, qt.Equals(skbCount, dedupedCount), qt.Commentf("Expected deduplicated sk_buff to have same number of types as original")) |
| 44 |
} |
| 45 |
|
| 46 |
func BenchmarkDeduplicateSKBuff(b *testing.B) { |
| 47 |
b.ReportAllocs() |
| 48 |
|
| 49 |
vmlinux := vmlinuxTestdataBytes(b) |
| 50 |
base, err := loadRawSpec(vmlinux, nil) |
| 51 |
qt.Assert(b, qt.IsNil(err)) |
| 52 |
|
| 53 |
|
| 54 |
types := make([]Type, 0, b.N) |
| 55 |
for range b.N { |
| 56 |
var skb *Struct |
| 57 |
if err := base.Copy().TypeByName("sk_buff", &skb); err != nil { |
| 58 |
b.Fatal(err) |
| 59 |
} |
| 60 |
types = append(types, skb) |
| 61 |
} |
| 62 |
|
| 63 |
dedup := newDeduper() |
| 64 |
|
| 65 |
b.ResetTimer() |
| 66 |
|
| 67 |
for i := range b.N { |
| 68 |
if _, err := dedup.deduplicate(types[i]); err != nil { |
| 69 |
b.Fatal(err) |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
func BenchmarkDeduplicateVMLinux(b *testing.B) { |
| 75 |
b.ReportAllocs() |
| 76 |
|
| 77 |
vmlinux := vmlinuxTestdataBytes(b) |
| 78 |
base, err := loadRawSpec(vmlinux, nil) |
| 79 |
qt.Assert(b, qt.IsNil(err)) |
| 80 |
|
| 81 |
var types [][]Type |
| 82 |
for range b.N { |
| 83 |
var specTypes []Type |
| 84 |
for typ := range base.Copy().All() { |
| 85 |
specTypes = append(specTypes, typ) |
| 86 |
} |
| 87 |
types = append(types, specTypes) |
| 88 |
} |
| 89 |
|
| 90 |
dedup := newDeduper() |
| 91 |
|
| 92 |
b.ResetTimer() |
| 93 |
|
| 94 |
for i := range b.N { |
| 95 |
for _, typ := range types[i] { |
| 96 |
if _, err := dedup.deduplicate(typ); err != nil { |
| 97 |
b.Fatal(err) |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
} |