Merge pull request #1688 from luoyunpeng/optimize-blockIOTypecheck

use char to check blockIO type
This commit is contained in:
Sebastiaan van Stijn 2019-03-12 10:32:31 +01:00 committed by GitHub
commit bf4a96e564
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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)
}
}