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

View File

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