use char to check blockIO type

Signed-off-by: Elliot Luo <956941328@qq.com>
This commit is contained in:
Elliot Luo 2019-03-11 10:01:22 +08:00
parent fdb0ef7be0
commit 0bb397f9ef
2 changed files with 15 additions and 8 deletions

View File

@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"io"
"strings"
"sync"
"time"
@ -202,10 +201,13 @@ func calculateCPUPercentWindows(v *types.StatsJSON) float64 {
func calculateBlockIO(blkio types.BlkioStats) (uint64, uint64) {
var blkRead, blkWrite uint64
for _, bioEntry := range blkio.IoServiceBytesRecursive {
switch strings.ToLower(bioEntry.Op) {
case "read":
if len(bioEntry.Op) == 0 {
continue
}
switch bioEntry.Op[0] {
case 'r', 'R':
blkRead = blkRead + bioEntry.Value
case "write":
case 'w', 'W':
blkWrite = blkWrite + bioEntry.Value
}
}

View File

@ -11,15 +11,20 @@ func TestCalculateBlockIO(t *testing.T) {
IoServiceBytesRecursive: []types.BlkioStatEntry{
{Major: 8, Minor: 0, Op: "read", Value: 1234},
{Major: 8, Minor: 1, Op: "read", Value: 4567},
{Major: 8, Minor: 0, Op: "Read", Value: 6},
{Major: 8, Minor: 1, Op: "Read", Value: 8},
{Major: 8, Minor: 0, Op: "write", Value: 123},
{Major: 8, Minor: 1, Op: "write", Value: 456},
{Major: 8, Minor: 0, Op: "Write", Value: 6},
{Major: 8, Minor: 1, Op: "Write", Value: 8},
{Major: 8, Minor: 1, Op: "", Value: 456},
},
}
blkRead, blkWrite := calculateBlockIO(blkio)
if blkRead != 5801 {
t.Fatalf("blkRead = %d, want 5801", blkRead)
if blkRead != 5815 {
t.Fatalf("blkRead = %d, want 5815", blkRead)
}
if blkWrite != 579 {
t.Fatalf("blkWrite = %d, want 579", blkWrite)
if blkWrite != 593 {
t.Fatalf("blkWrite = %d, want 593", blkWrite)
}
}