Skip to content

Commit

Permalink
minor changes and wrap up unit testing
Browse files Browse the repository at this point in the history
  • Loading branch information
danehlim committed Jun 7, 2024
1 parent cb9a0c5 commit 680c19f
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 12 deletions.
4 changes: 2 additions & 2 deletions agent/stats/queue_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func aggregateUsagePerCore(dockerStatUsageSlice *[]uint64, lastStatBeforeLastRes
return
}

aggregatedUsageSlice := []uint64{}
var aggregatedUsageSlice []uint64
peakNumCores := utils.MaxNum(len(*dockerStatUsageSlice), len(lastStatBeforeLastRestartStatUsageSlice))
for i := 0; i < peakNumCores; i++ {
coreUsage := uint64(0)
Expand All @@ -102,7 +102,7 @@ func aggregateBlockStat(dockerStatBlockStatSlice *[]types.BlkioStatEntry,
return
}

aggregatedBlockStatSlice := []types.BlkioStatEntry{}
var aggregatedBlockStatSlice []types.BlkioStatEntry
blockStatsMap := make(map[BlockStatKey]BlockStatValue)

// Add block stat entries from stats to map (merging duplicates).
Expand Down
95 changes: 85 additions & 10 deletions agent/stats/queue_unix_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// TO-DO: add unit test flags back in
//go:build linux && unit
// +build linux,unit

// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
//
Expand All @@ -24,8 +25,55 @@ import (
)

func TestAggregateOSDependentStats(t *testing.T) {
dockerStat := getTestStatsJSONForOSDependentStats(1, 2, 3, 4, 5, 6, 7, 8, 9)
lastStatBeforeLastRestart := getTestStatsJSONForOSDependentStats(1, 2, 3, 4, 5, 6, 7, 8, 9)
dockerStat := getTestStatsJSONForOSDependentStats(1, 2, 3, 4, 5, 6, 7, 8, 9,
[]types.BlkioStatEntry{
{
Major: 202,
Minor: 192,
Op: "Read",
Value: 1,
},
{
Major: 202,
Minor: 192,
Op: "Write",
Value: 2,
},
{
Major: 253,
Minor: 1,
Op: "Read",
Value: 3,
},
{
Major: 253,
Minor: 1,
Op: "Write",
Value: 4,
},
})
lastStatBeforeLastRestart := getTestStatsJSONForOSDependentStats(1, 2, 3, 4, 5, 6, 7, 8, 9,
[]types.BlkioStatEntry{
{
Major: 253,
Minor: 1,
Op: "Read",
Value: 1234,
},
{
Major: 253,
Minor: 1,
Op: "Write",
Value: 5678,
},
})

// Sanity length checks/enforcement.
require.Equal(t, 2, len(dockerStat.CPUStats.CPUUsage.PercpuUsage))
require.Equal(t, 2, len(lastStatBeforeLastRestart.CPUStats.CPUUsage.PercpuUsage))
require.Equal(t, 4, len(dockerStat.BlkioStats.IoServiceBytesRecursive))
require.Equal(t, 2, len(lastStatBeforeLastRestart.BlkioStats.IoServiceBytesRecursive))

expectedAggregatedStat := types.StatsJSON{
Stats: types.Stats{
CPUStats: types.CPUStats{
Expand All @@ -49,9 +97,36 @@ func TestAggregateOSDependentStats(t *testing.T) {
MaxUsage: utils.MaxNum(dockerStat.MemoryStats.MaxUsage, lastStatBeforeLastRestart.MemoryStats.MaxUsage),
Failcnt: dockerStat.MemoryStats.Failcnt + lastStatBeforeLastRestart.MemoryStats.Failcnt,
},
//BlkioStats: types.BlkioStats{
// IoServiceBytesRecursive: ioServiceBytesRecursive,
//},
BlkioStats: types.BlkioStats{
IoServiceBytesRecursive: []types.BlkioStatEntry{
{
Major: lastStatBeforeLastRestart.BlkioStats.IoServiceBytesRecursive[0].Major,
Minor: lastStatBeforeLastRestart.BlkioStats.IoServiceBytesRecursive[0].Minor,
Op: lastStatBeforeLastRestart.BlkioStats.IoServiceBytesRecursive[0].Op,
Value: lastStatBeforeLastRestart.BlkioStats.IoServiceBytesRecursive[0].Value +
dockerStat.BlkioStats.IoServiceBytesRecursive[2].Value,
},
{
Major: lastStatBeforeLastRestart.BlkioStats.IoServiceBytesRecursive[1].Major,
Minor: lastStatBeforeLastRestart.BlkioStats.IoServiceBytesRecursive[1].Minor,
Op: lastStatBeforeLastRestart.BlkioStats.IoServiceBytesRecursive[1].Op,
Value: lastStatBeforeLastRestart.BlkioStats.IoServiceBytesRecursive[1].Value +
dockerStat.BlkioStats.IoServiceBytesRecursive[3].Value,
},
{
Major: dockerStat.BlkioStats.IoServiceBytesRecursive[0].Major,
Minor: dockerStat.BlkioStats.IoServiceBytesRecursive[0].Minor,
Op: dockerStat.BlkioStats.IoServiceBytesRecursive[0].Op,
Value: dockerStat.BlkioStats.IoServiceBytesRecursive[0].Value,
},
{
Major: dockerStat.BlkioStats.IoServiceBytesRecursive[1].Major,
Minor: dockerStat.BlkioStats.IoServiceBytesRecursive[1].Minor,
Op: dockerStat.BlkioStats.IoServiceBytesRecursive[1].Op,
Value: dockerStat.BlkioStats.IoServiceBytesRecursive[1].Value,
},
},
},
},
Networks: map[string]types.NetworkStats{
testNetworkNameA: {
Expand All @@ -74,7 +149,7 @@ func TestAggregateOSDependentStats(t *testing.T) {
}

func getTestStatsJSONForOSDependentStats(usageCoreA, usageCoreB, periods, throttledPeriods, throttledTime, maxUsage,
failCnt, rxErrors, txErrors uint64) *types.StatsJSON {
failCnt, rxErrors, txErrors uint64, ioServiceBytesRecursive []types.BlkioStatEntry) *types.StatsJSON {
return &types.StatsJSON{
Stats: types.Stats{
CPUStats: types.CPUStats{
Expand All @@ -91,9 +166,9 @@ func getTestStatsJSONForOSDependentStats(usageCoreA, usageCoreB, periods, thrott
MaxUsage: maxUsage,
Failcnt: failCnt,
},
//BlkioStats: types.BlkioStats{
// IoServiceBytesRecursive: ioServiceBytesRecursive,
//},
BlkioStats: types.BlkioStats{
IoServiceBytesRecursive: ioServiceBytesRecursive,
},
},
Networks: map[string]types.NetworkStats{
testNetworkNameA: {
Expand Down

0 comments on commit 680c19f

Please sign in to comment.