Skip to content

Commit

Permalink
Add option sorted by. (#69)
Browse files Browse the repository at this point in the history
* Add option sorted by.

Signed-off-by: Pan Li <incarnation.p.lee@outlook.com>
Co-authored-by: Restyled.io <commits@restyled.io>
  • Loading branch information
Incarnation-p-lee and restyled-commits committed Jul 3, 2022
1 parent 5e1d29f commit 194eeb0
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 18 deletions.
5 changes: 5 additions & 0 deletions internal/cmdflags/cmdflags.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ var supportedCmdFlags = []cmdflag{
defaultValue: options.GetNameDefaultValue(options.TopCount),
description: "the top count of process to be printed",
},
cmdflag{
flagName: options.SortedBy,
defaultValue: options.GetNameDefaultValue(options.SortedBy),
description: "the metrics to be sorted when print the snapshot",
},
}

// ParseOptions will parse the flags from command line to options.
Expand Down
2 changes: 1 addition & 1 deletion internal/cmdflags/cmdflags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func TestParseOptions(t *testing.T) {

ParseOptions(ops)

assert.IsEqual(t, 5, ops.OptionsCount(), "optionsCount should be 5.")
assert.IsEqual(t, 6, ops.OptionsCount(), "optionsCount should be 6.")

option1, _ := ops.GetOption(0)
option2, _ := ops.GetOption(1)
Expand Down
12 changes: 12 additions & 0 deletions internal/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ const (
ProcessIDs = "pids"
// TopCount indicates the limit of print times.
TopCount = "top-count"
// SortedBy indicates how to sort the print.
SortedBy = "sorted-by"

// AllProcessIDs indicates all process in a system.
AllProcessIDs = "-1"
Expand All @@ -39,6 +41,10 @@ const (
TextOutput = "text"
// DefaultTopCount indicates the default print times.
DefaultTopCount = "7"
// SortedByCPU will sort the print by CPU.
SortedByCPU = "cpu"
// SortedByMemory will sort the print by memory.
SortedByMemory = "memory"
)

var namesToDefaultValues = map[string]string{
Expand All @@ -47,6 +53,7 @@ var namesToDefaultValues = map[string]string{
OutputFormat: TextOutput,
ProcessIDs: AllProcessIDs,
TopCount: DefaultTopCount,
SortedBy: SortedByCPU,
}

// GetNameDefaultValue will return the default value for option name, or unknown.
Expand Down Expand Up @@ -136,6 +143,11 @@ func (ops *Options) GetTopCount() int {
return ops.getIntOption(TopCount)
}

// GetSortedBy will return the metrics sorted by.
func (ops *Options) GetSortedBy() string {
return ops.getStringOption(SortedBy)
}

// GetOption will return the option by index, out of range will return error.
func (ops *Options) GetOption(index int) (Option, error) {
limit := len(ops.allOptions)
Expand Down
20 changes: 15 additions & 5 deletions internal/options/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package options

import (
"github.com/Incarnation-p-lee/cachalot/pkg/assert"
"strconv"
"testing"
)

Expand Down Expand Up @@ -173,15 +174,24 @@ func TestIsAllProcessIDsFalse(t *testing.T) {
assert.IsFalse(t, ops.IsAllProcessIDs(), "options should not have all process ids")
}

func TesGetTopCount(t *testing.T) {
ops := CreateOptions()
func TestGetTopCount(t *testing.T) {
ops, testCount := CreateOptions(), 2

ops.AppendOption(Option{
Key: TopCount,
Val: DefaultTopCount,
Val: strconv.Itoa(testCount),
})

topCount := ops.GetTopCount()
assert.IsEqual(t, testCount, ops.GetTopCount(), "options should have same top count")
}

func TestGetSortedBy(t *testing.T) {
ops := CreateOptions()

ops.AppendOption(Option{
Key: SortedBy,
Val: SortedByMemory,
})

assert.IsEqual(t, DefaultTopCount, topCount, "options should have same top count")
assert.IsEqual(t, "memory", ops.GetSortedBy(), "options should have same sorted by")
}
19 changes: 19 additions & 0 deletions internal/print/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/Incarnation-p-lee/cachalot/pkg/snapshot"
"internal/options"
"log"
"sort"
"time"
)

Expand Down Expand Up @@ -87,8 +88,26 @@ func reconcileSnapshotTopCount(snapshot *snapshot.Snapshot, topCount int) {
}
}

func reconcileSnapshotSortedBy(snapshot *snapshot.Snapshot, sortedBy string) {
switch sortedBy {
case options.SortedByMemory:
sort.Slice(snapshot.Processes, func(a, b int) bool {
memoryA, memoryB := snapshot.Processes[a].MemoryStat, snapshot.Processes[b].MemoryStat
return memoryA.UsageInPercentage > memoryB.UsageInPercentage
})
case options.SortedByCPU:
fallthrough
default:
sort.Slice(snapshot.Processes, func(a, b int) bool {
cpuA, cpuB := snapshot.Processes[a].CPUStat, snapshot.Processes[b].CPUStat
return cpuA.UsageInPercentage > cpuB.UsageInPercentage
})
}
}

func reconcileSnapshot(snapshot *snapshot.Snapshot, ops *options.Options) {
reconcileSnapshotTopCount(snapshot, ops.GetTopCount())
reconcileSnapshotSortedBy(snapshot, ops.GetSortedBy())
}

// Snapshot will print the data module of given snapshot.
Expand Down
52 changes: 52 additions & 0 deletions internal/print/print_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,55 @@ func TestReconcileSnapshot(t *testing.T) {
assert.IsEqual(t, topCount, len(testSnapshot.Processes),
"process count after reconcile should be the same as top count")
}

func TestReconcileSnapshotSortedByCPU(t *testing.T) {
testProcesses := []snapshot.Process{
snapshot.Process{
CPUStat: snapshot.CPUStat{UsageInPercentage: 12.0},
},
snapshot.Process{
CPUStat: snapshot.CPUStat{UsageInPercentage: 21.0},
},
snapshot.Process{
CPUStat: snapshot.CPUStat{UsageInPercentage: 32.0},
},
snapshot.Process{
CPUStat: snapshot.CPUStat{UsageInPercentage: 21.0},
},
}

testSnapshot := snapshot.CreateSnapshot(time.Now(), testProcesses)
reconcileSnapshotSortedBy(&testSnapshot, "cpu")

for i := 1; i < len(testSnapshot.Processes); i++ {
first, second := testSnapshot.Processes[i-1], testSnapshot.Processes[i]
assert.IsTrue(t, first.CPUStat.UsageInPercentage >= second.CPUStat.UsageInPercentage,
"the processes should be sorted by cpu in desc order")
}
}

func TestReconcileSnapshotSortedByMemory(t *testing.T) {
testProcesses := []snapshot.Process{
snapshot.Process{
MemoryStat: snapshot.MemoryStat{UsageInPercentage: 2.0},
},
snapshot.Process{
MemoryStat: snapshot.MemoryStat{UsageInPercentage: 11.0},
},
snapshot.Process{
MemoryStat: snapshot.MemoryStat{UsageInPercentage: 22.0},
},
snapshot.Process{
MemoryStat: snapshot.MemoryStat{UsageInPercentage: 11.0},
},
}

testSnapshot := snapshot.CreateSnapshot(time.Now(), testProcesses)
reconcileSnapshotSortedBy(&testSnapshot, "memory")

for i := 1; i < len(testSnapshot.Processes); i++ {
first, second := testSnapshot.Processes[i-1], testSnapshot.Processes[i]
assert.IsTrue(t, first.MemoryStat.UsageInPercentage >= second.MemoryStat.UsageInPercentage,
"the processes should be sorted by memory in desc order")
}
}
5 changes: 0 additions & 5 deletions internal/sampling/sampling_process.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"log"
"path"
"path/filepath"
"sort"
"strconv"
"strings"
)
Expand Down Expand Up @@ -68,10 +67,6 @@ func sampleAllProcesses(ops *options.Options) []snapshot.Process {
processes = append(processes, <-processChan)
}

sort.Slice(processes, func(a, b int) bool {
return processes[a].CPUStat.UsageInPercentage > processes[b].CPUStat.UsageInPercentage
})

return processes
}

Expand Down
7 changes: 0 additions & 7 deletions internal/sampling/sampling_process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,6 @@ func TestSampleAllProcess(t *testing.T) {
processes := sampleAllProcesses(ops)

assert.IsTrue(t, len(processes) > 0, "all proccess slice count should not be 0.")

for i := 0; i < len(processes)-1; i++ {
a, b := processes[i], processes[i+1]

assert.IsTrue(t, a.CPUStat.UsageInPercentage >= b.CPUStat.UsageInPercentage,
"the process usage should be sorted in desc order")
}
}

func TestSampleOneProcessSnapshotNilOptions(t *testing.T) {
Expand Down

0 comments on commit 194eeb0

Please sign in to comment.