Skip to content

Commit 57798c6

Browse files
authored
Merge pull request #4403 from Stavrospanakakis/replace-fields-splitn-cgroups-fs
libcontainer/cgroups/fs: remove todo since strings.Fields performs well
2 parents e1635d5 + 1be0676 commit 57798c6

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

libcontainer/cgroups/fs/cpuacct.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func getCpuUsageBreakdown(path string) (uint64, uint64, error) {
9191
if err != nil {
9292
return 0, 0, err
9393
}
94-
// TODO: use strings.SplitN instead.
94+
9595
fields := strings.Fields(data)
9696
if len(fields) < 4 || fields[0] != userField || fields[2] != systemField {
9797
return 0, 0, malformedLine(path, file, data)

libcontainer/cgroups/fs/cpuacct_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,18 @@ func TestCpuacctStatsWithoutUsageAll(t *testing.T) {
9595
expectedStats, actualStats.CpuStats.CpuUsage)
9696
}
9797
}
98+
99+
func BenchmarkGetCpuUsageBreakdown(b *testing.B) {
100+
path := tempDir(b, "cpuacct")
101+
writeFileContents(b, path, map[string]string{
102+
"cpuacct.stat": cpuAcctStatContents,
103+
})
104+
105+
b.ResetTimer()
106+
for i := 0; i < b.N; i++ {
107+
_, _, err := getCpuUsageBreakdown(path)
108+
if err != nil {
109+
b.Fatal(err)
110+
}
111+
}
112+
}

libcontainer/cgroups/fs/util_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func init() {
1818
}
1919

2020
// tempDir creates a new test directory for the specified subsystem.
21-
func tempDir(t *testing.T, subsystem string) string {
21+
func tempDir(t testing.TB, subsystem string) string {
2222
path := filepath.Join(t.TempDir(), subsystem)
2323
// Ensure the full mock cgroup path exists.
2424
if err := os.Mkdir(path, 0o755); err != nil {
@@ -29,7 +29,7 @@ func tempDir(t *testing.T, subsystem string) string {
2929

3030
// writeFileContents writes the specified contents on the mock of the specified
3131
// cgroup files.
32-
func writeFileContents(t *testing.T, path string, fileContents map[string]string) {
32+
func writeFileContents(t testing.TB, path string, fileContents map[string]string) {
3333
for file, contents := range fileContents {
3434
err := cgroups.WriteFile(path, file, contents)
3535
if err != nil {

0 commit comments

Comments
 (0)