From 7bcc03d97219f07f96ce84888dec0b50a4e9e0a9 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Mon, 28 Mar 2022 11:00:29 +0200 Subject: [PATCH] cli/command/container: add BenchmarkStatsFormat() To test: GO111MODULE=off go test -test.v -test.bench '^BenchmarkStatsFormat' -test.run '^$' ./cli/command/container/ goos: darwin goarch: amd64 pkg: github.com/docker/cli/cli/command/container cpu: Intel(R) Core(TM) i7-4790K CPU @ 4.00GHz BenchmarkStatsFormat BenchmarkStatsFormat-8 2482 522721 ns/op 62439 B/op 5600 allocs/op PASS ok github.com/docker/cli/cli/command/container 1.369s Signed-off-by: Sebastiaan van Stijn --- cli/command/container/formatter_stats_test.go | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/cli/command/container/formatter_stats_test.go b/cli/command/container/formatter_stats_test.go index 844679654b..ad395e6f74 100644 --- a/cli/command/container/formatter_stats_test.go +++ b/cli/command/container/formatter_stats_test.go @@ -308,3 +308,38 @@ func TestContainerStatsContextWriteTrunc(t *testing.T) { out.Reset() } } + +func BenchmarkStatsFormat(b *testing.B) { + b.ReportAllocs() + stats := genStats() + + for i := 0; i < b.N; i++ { + for _, s := range stats { + _ = s.CPUPerc() + _ = s.MemUsage() + _ = s.MemPerc() + _ = s.NetIO() + _ = s.BlockIO() + _ = s.PIDs() + } + } +} + +func genStats() []statsContext { + entry := statsContext{s: StatsEntry{ + CPUPercentage: 12.3456789, + Memory: 123.456789, + MemoryLimit: 987.654321, + MemoryPercentage: 12.3456789, + BlockRead: 123.456789, + BlockWrite: 987.654321, + NetworkRx: 123.456789, + NetworkTx: 987.654321, + PidsCurrent: 123456789, + }} + stats := make([]statsContext, 100) + for i := 0; i < 100; i++ { + stats = append(stats, entry) + } + return stats +}