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 <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2022-03-28 11:00:29 +02:00
parent d0df532a25
commit 7bcc03d972
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
1 changed files with 35 additions and 0 deletions

View File

@ -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
}