From bcdf5073632f63bc947f3c9048999cd7fd8f75aa Mon Sep 17 00:00:00 2001 From: Wei Fu Date: Mon, 19 Aug 2024 18:49:58 +0800 Subject: [PATCH] core/mount: add benchmark test for GetUsernsFD ```bash $ go test -bench=. -benchmem ./ -exec sudo goos: linux goarch: amd64 pkg: github.com/containerd/containerd/v2/core/mount cpu: AMD Ryzen 7 5800H with Radeon Graphics BenchmarkBatchRunGetUsernsFD_Concurrent1-16 2398 532424 ns/op 1145 B/op 43 allocs/op BenchmarkBatchRunGetUsernsFD_Concurrent10-16 343 3701695 ns/op 11552 B/op 421 allocs/op PASS ok github.com/containerd/containerd/v2/core/mount 2.978s ``` Signed-off-by: Wei Fu --- core/mount/mount_idmapped_linux_test.go | 29 +++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/core/mount/mount_idmapped_linux_test.go b/core/mount/mount_idmapped_linux_test.go index 1dd36d2e36ef..b399d8348bd4 100644 --- a/core/mount/mount_idmapped_linux_test.go +++ b/core/mount/mount_idmapped_linux_test.go @@ -20,6 +20,7 @@ import ( "fmt" "os" "path/filepath" + "sync" "syscall" "testing" @@ -29,6 +30,34 @@ import ( "golang.org/x/sys/unix" ) +func BenchmarkBatchRunGetUsernsFD_Concurrent1(b *testing.B) { + for range b.N { + benchmarkBatchRunGetUsernsFD(1) + } +} + +func BenchmarkBatchRunGetUsernsFD_Concurrent10(b *testing.B) { + for range b.N { + benchmarkBatchRunGetUsernsFD(10) + } +} + +func benchmarkBatchRunGetUsernsFD(n int) { + var wg sync.WaitGroup + wg.Add(n) + for i := 0; i < n; i++ { + go func() { + defer wg.Done() + fd, err := getUsernsFD(testUIDMaps, testGIDMaps) + if err != nil { + panic(err) + } + fd.Close() + }() + } + wg.Wait() +} + var ( testUIDMaps = []syscall.SysProcIDMap{ {ContainerID: 1000, HostID: 0, Size: 100},