Files
runc/libcontainer/cgroups/fs/util_test.go
T
Stavros Panakakis 1be06760ed libcontainer/cgroups/fs: remove todo since strings.Fields performs well
Initially, this was a commit to switch from strings.Fields to
strings.SplitN in getCpuUsageBreakdown, since strings.Fields
was probably slower than strings.SplitN in some old Go versions.

Afterwards, strings.Cut was also considered for potential
speed improvements.

After writing a benchmark test, we learned that:
 - strings.Fields performance is now adequate;
 - strings.SplitN is slower than strings.Fields;
 - strings.Cut had <5% performance gain from strings.Fields;

So, remove the TODO and keep the benchmark test.

Signed-off-by: Stavros Panakakis <stavrospanakakis@gmail.com>
2024-09-26 12:34:32 +03:00

40 lines
885 B
Go

/*
Utility for testing cgroup operations.
Creates a mock of the cgroup filesystem for the duration of the test.
*/
package fs
import (
"os"
"path/filepath"
"testing"
"github.com/opencontainers/runc/libcontainer/cgroups"
)
func init() {
cgroups.TestMode = true
}
// tempDir creates a new test directory for the specified subsystem.
func tempDir(t testing.TB, subsystem string) string {
path := filepath.Join(t.TempDir(), subsystem)
// Ensure the full mock cgroup path exists.
if err := os.Mkdir(path, 0o755); err != nil {
t.Fatal(err)
}
return path
}
// writeFileContents writes the specified contents on the mock of the specified
// cgroup files.
func writeFileContents(t testing.TB, path string, fileContents map[string]string) {
for file, contents := range fileContents {
err := cgroups.WriteFile(path, file, contents)
if err != nil {
t.Fatal(err)
}
}
}