Skip to content

Commit

Permalink
labels: Refactor CIDRLabelsCacheHeapUsage into tests
Browse files Browse the repository at this point in the history
TestCIDRLabelsCacheHeapUsageIP{v4,v6} are meant to estimate the maximum
heap usage when filling up the CIDR labels LRU cache with labels derived
only from IPv4 and labels derived only from IPv6.

Since they give meaningful results only when running them with
benchtime=1x, thery are refactored to be just tests with a t.Log() to
output the heap usage statistics.

Signed-off-by: Fabio Falzoi <fabio.falzoi@isovalent.com>
  • Loading branch information
pippolo84 authored and nathanjsweet committed Oct 27, 2023
1 parent 9f2034e commit 71b7ad5
Showing 1 changed file with 85 additions and 81 deletions.
166 changes: 85 additions & 81 deletions pkg/labels/cidr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,91 @@ func TestIPStringToLabel(t *testing.T) {
}
}

func TestCIDRLabelsCacheHeapUsageIPv4(t *testing.T) {
t.Skip()

// save global config and restore it at the end of the test
enableIPv4, enableIPv6 := option.Config.EnableIPv4, option.Config.EnableIPv6
t.Cleanup(func() {
option.Config.EnableIPv4, option.Config.EnableIPv6 = enableIPv4, enableIPv6
})

option.Config.EnableIPv4, option.Config.EnableIPv6 = true, false

// clear the cache
cidrLabelsCache, _ = simplelru.NewLRU[netip.Prefix, []Label](cidrLabelsCacheMaxSize, nil)

// be sure to fill the cache
prefixes := make([]netip.Prefix, 0, 256*256)
octets := [4]byte{0, 0, 1, 1}
for i := 0; i < 256*256; i++ {
octets[0], octets[1] = byte(i/256), byte(i%256)
prefixes = append(prefixes, netip.PrefixFrom(netip.AddrFrom4(octets), 32))
}

var m1, m2 runtime.MemStats
// One GC does not give precise results,
// because concurrent sweep may be still in progress.
runtime.GC()
runtime.GC()
runtime.ReadMemStats(&m1)

for _, cidr := range prefixes {
_ = GetCIDRLabels(cidr)
}

runtime.GC()
runtime.GC()
runtime.ReadMemStats(&m2)

usage := m2.HeapAlloc - m1.HeapAlloc
t.Logf("Memoization map heap usage: %.2f KiB", float64(usage)/1024)
}

func TestCIDRLabelsCacheHeapUsageIPv6(t *testing.T) {
t.Skip()

// save global config and restore it at the end of the test
enableIPv4, enableIPv6 := option.Config.EnableIPv4, option.Config.EnableIPv6
t.Cleanup(func() {
option.Config.EnableIPv4, option.Config.EnableIPv6 = enableIPv4, enableIPv6
})

option.Config.EnableIPv4, option.Config.EnableIPv6 = true, true

// clear the cache
cidrLabelsCache, _ = simplelru.NewLRU[netip.Prefix, []Label](cidrLabelsCacheMaxSize, nil)

// be sure to fill the cache
prefixes := make([]netip.Prefix, 0, 256*256)
octets := [16]byte{
0x00, 0x00, 0x00, 0xd8, 0x33, 0x33, 0x44, 0x44,
0x55, 0x55, 0x66, 0x66, 0x77, 0x77, 0x88, 0x88,
}
for i := 0; i < 256*256; i++ {
octets[15], octets[14] = byte(i/256), byte(i%256)
prefixes = append(prefixes, netip.PrefixFrom(netip.AddrFrom16(octets), 128))
}

var m1, m2 runtime.MemStats
// One GC does not give precise results,
// because concurrent sweep may be still in progress.
runtime.GC()
runtime.GC()
runtime.ReadMemStats(&m1)

for _, cidr := range prefixes {
_ = GetCIDRLabels(cidr)
}

runtime.GC()
runtime.GC()
runtime.ReadMemStats(&m2)

usage := m2.HeapAlloc - m1.HeapAlloc
t.Logf("Memoization map heap usage: %.2f KiB", float64(usage)/1024)
}

func BenchmarkGetCIDRLabels(b *testing.B) {
// clear the cache
cidrLabelsCache, _ = simplelru.NewLRU[netip.Prefix, []Label](cidrLabelsCacheMaxSize, nil)
Expand Down Expand Up @@ -562,87 +647,6 @@ func BenchmarkGetCIDRLabelsConcurrent(b *testing.B) {
}
}

// BenchmarkCIDRLabelsCacheHeapUsageIPv4 should be run with -benchtime=1x
func BenchmarkCIDRLabelsCacheHeapUsageIPv4(b *testing.B) {
b.Skip()

// clear the cache
cidrLabelsCache, _ = simplelru.NewLRU[netip.Prefix, []Label](cidrLabelsCacheMaxSize, nil)

// be sure to fill the cache
prefixes := make([]netip.Prefix, 0, 256*256)
octets := [4]byte{0, 0, 1, 1}
for i := 0; i < 256*256; i++ {
octets[0], octets[1] = byte(i/256), byte(i%256)
prefixes = append(prefixes, netip.PrefixFrom(netip.AddrFrom4(octets), 32))
}

var m1, m2 runtime.MemStats
// One GC does not give precise results,
// because concurrent sweep may be still in progress.
runtime.GC()
runtime.GC()
runtime.ReadMemStats(&m1)

b.ResetTimer()

for i := 0; i < b.N; i++ {
for _, cidr := range prefixes {
_ = GetCIDRLabels(cidr)
}
}
b.StopTimer()

runtime.GC()
runtime.GC()
runtime.ReadMemStats(&m2)

usage := m2.HeapAlloc - m1.HeapAlloc
b.Logf("Memoization map heap usage: %.2f KiB", float64(usage)/1024)
}

// BenchmarkCIDRLabelsCacheHeapUsageIPv6 should be run with -benchtime=1x
func BenchmarkCIDRLabelsCacheHeapUsageIPv6(b *testing.B) {
b.Skip()

// clear the cache
cidrLabelsCache, _ = simplelru.NewLRU[netip.Prefix, []Label](cidrLabelsCacheMaxSize, nil)

// be sure to fill the cache
prefixes := make([]netip.Prefix, 0, 256*256)
octets := [16]byte{
0x00, 0x00, 0x00, 0xd8, 0x33, 0x33, 0x44, 0x44,
0x55, 0x55, 0x66, 0x66, 0x77, 0x77, 0x88, 0x88,
}
for i := 0; i < 256*256; i++ {
octets[15], octets[14] = byte(i/256), byte(i%256)
prefixes = append(prefixes, netip.PrefixFrom(netip.AddrFrom16(octets), 128))
}

var m1, m2 runtime.MemStats
// One GC does not give precise results,
// because concurrent sweep may be still in progress.
runtime.GC()
runtime.GC()
runtime.ReadMemStats(&m1)

b.ResetTimer()

for i := 0; i < b.N; i++ {
for _, cidr := range prefixes {
_ = GetCIDRLabels(cidr)
}
}
b.StopTimer()

runtime.GC()
runtime.GC()
runtime.ReadMemStats(&m2)

usage := m2.HeapAlloc - m1.HeapAlloc
b.Logf("Memoization map heap usage: %.2f KiB", float64(usage)/1024)
}

func BenchmarkIPStringToLabel(b *testing.B) {
for _, ip := range []string{
"0.0.0.0/0",
Expand Down

0 comments on commit 71b7ad5

Please sign in to comment.