Skip to content

Commit

Permalink
Sample network before process.
Browse files Browse the repository at this point in the history
Signed-off-by: Pan Li <incarnation.p.lee@outlook.com>
  • Loading branch information
Incarnation-p-lee committed Jul 9, 2022
1 parent 8b61363 commit 9f9eab9
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 28 deletions.
21 changes: 14 additions & 7 deletions internal/print/print_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,19 @@ import (
"time"
)

func createSnapshot(timestamp time.Time, processes []snapshot.Process) snapshot.Snapshot {
return snapshot.Snapshot{
Timestamp: timestamp,
Processes: processes,
}
}

func TestPrintSnapshotDefault(t *testing.T) {
ops := options.CreateOptions()
testProcesses := []snapshot.Process{
snapshot.Process{},
}
testSnapshot := snapshot.CreateSnapshot(time.Now(), testProcesses)
testSnapshot := createSnapshot(time.Now(), testProcesses)

assert.IsNil(t, Snapshot(testSnapshot, "", ops), "print empty title snapshot should have nil error")
assert.IsNil(t, Snapshot(testSnapshot, "abc", ops), "print text title snapshot should have nil error")
Expand All @@ -31,7 +38,7 @@ func TestPrintTextSnapshot(t *testing.T) {
snapshot.Process{},
}

testSnapshot := snapshot.CreateSnapshot(time.Now(), testProcesses)
testSnapshot := createSnapshot(time.Now(), testProcesses)
ops.AppendOption(options.Option{
Key: options.OutputFormat,
Val: options.TextOutput,
Expand All @@ -46,7 +53,7 @@ func TestPrintJsonSnapshot(t *testing.T) {
snapshot.Process{},
}

testSnapshot := snapshot.CreateSnapshot(time.Now(), testProcesses)
testSnapshot := createSnapshot(time.Now(), testProcesses)
ops.AppendOption(options.Option{
Key: options.OutputFormat,
Val: options.JSONOutput,
Expand All @@ -68,7 +75,7 @@ func TestReconcileSnapshot(t *testing.T) {
snapshot.Process{},
}

testSnapshot := snapshot.CreateSnapshot(time.Now(), testProcesses)
testSnapshot := createSnapshot(time.Now(), testProcesses)
reconcileSnapshot(&testSnapshot, ops)

assert.IsEqual(t, topCount, len(testSnapshot.Processes),
Expand All @@ -91,7 +98,7 @@ func TestReconcileSnapshotSortedByCPU(t *testing.T) {
},
}

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

for i := 1; i < len(testSnapshot.Processes); i++ {
Expand All @@ -117,7 +124,7 @@ func TestReconcileSnapshotSortedByMemory(t *testing.T) {
},
}

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

for i := 1; i < len(testSnapshot.Processes); i++ {
Expand All @@ -143,7 +150,7 @@ func TestReconcileSnapshotSortedByThreadsCount(t *testing.T) {
},
}

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

for i := 1; i < len(testSnapshot.Processes); i++ {
Expand Down
12 changes: 10 additions & 2 deletions internal/sampling/sampling.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@ func Sample(ops *options.Options) snapshot.Snapshot {
return snapshot.Snapshot{}
}

timestamp, processes := time.Now(), sampleAllProcesses(ops)
snapshot := snapshot.Snapshot{
Timestamp: time.Now(),
Network: sampleNetwork(),
Processes: []snapshot.Process{},
}

processes := sampleProcesses(ops)

snapshot.AppendProcesses(processes)

return snapshot.CreateSnapshot(timestamp, processes)
return snapshot
}
2 changes: 1 addition & 1 deletion internal/sampling/sampling_process.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func getSamplingProcessIDs(ops *options.Options) []int {
return getProcessIDs(ops.GetProcessIDs())
}

func sampleAllProcesses(ops *options.Options) []snapshot.Process {
func sampleProcesses(ops *options.Options) []snapshot.Process {
allPIDs := getSamplingProcessIDs(ops)

pIDCount := len(allPIDs)
Expand Down
2 changes: 1 addition & 1 deletion internal/sampling/sampling_process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestSampleAllProcess(t *testing.T) {
Val: options.AllProcessIDs,
})

processes := sampleAllProcesses(ops)
processes := sampleProcesses(ops)

assert.IsTrue(t, len(processes) > 0, "all proccess slice count should not be 0.")
}
Expand Down
17 changes: 9 additions & 8 deletions pkg/snapshot/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,6 @@ type ThreadsStat struct {
ThreadsCount int
}

// CreateSnapshot will create one object with given timestamp.
func CreateSnapshot(timestamp time.Time, processes []Process) Snapshot {
return Snapshot{
Timestamp: timestamp,
Processes: processes,
}
}

// CreateCPUStat will create one object with cpu usage and limit, count in mCore.
func CreateCPUStat(jiffiesUsed, jiffiesInTotal int) CPUStat {
usageInPercentage := float64(jiffiesUsed) / float64(jiffiesInTotal) * 100.0
Expand All @@ -85,3 +77,12 @@ func (process *Process) SetCPUStat(cpuStat CPUStat) {
func (snapshot *Snapshot) AppendProcess(process Process) {
snapshot.Processes = append(snapshot.Processes, process)
}

// AppendProcesses will add the given processes to the tail of snapshot.
func (snapshot *Snapshot) AppendProcesses(processes []Process) {
if snapshot.Processes == nil {
snapshot.Processes = []Process{}
}

snapshot.Processes = append(snapshot.Processes, processes...)
}
45 changes: 36 additions & 9 deletions pkg/snapshot/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@ func createProcess(pID int, cmdLine string, cpuStat CPUStat) Process {
}
}

func TestCreateSnapshot(t *testing.T) {
testLastHour, processes := time.Now().Add(-time.Hour), []Process{}
snapshot := CreateSnapshot(testLastHour, processes)

assert.IsEqual(t, testLastHour, snapshot.Timestamp, "snapshot should have the same timestamp.")
assert.IsEqual(t, 0, len(snapshot.Processes), "snapshot should have empty processes slice.")
}

func TestCreateCPUStat(t *testing.T) {
used, limited := 250, 500
stat := CreateCPUStat(used, limited)
Expand All @@ -48,11 +40,46 @@ func TestSetUsage(t *testing.T) {

func TestAppendProcess(t *testing.T) {
process := createProcess(123, "ls -l", CPUStat{})
snapshot := CreateSnapshot(time.Now(), []Process{})
snapshot := Snapshot{
Timestamp: time.Now(),
Processes: []Process{},
}

assert.IsEqual(t, 0, len(snapshot.Processes), "snapshot processes count should be 0.")

snapshot.AppendProcess(process)

assert.IsEqual(t, 1, len(snapshot.Processes), "snapshot processes count should be 0.")
}

func TestAppendProcesses(t *testing.T) {
snapshot := Snapshot{
Timestamp: time.Now(),
Processes: []Process{
Process{},
},
}
processes := []Process{
createProcess(123, "ls -l", CPUStat{}),
createProcess(123, "ls -l", CPUStat{}),
}

snapshot.AppendProcesses(processes)

assert.IsEqual(t, 3, len(snapshot.Processes), "processes size should be 3")
}

func TestAppendProcessesWithExistNil(t *testing.T) {
snapshot := Snapshot{
Timestamp: time.Now(),
Processes: nil,
}
processes := []Process{
createProcess(123, "ls -l", CPUStat{}),
createProcess(123, "ls -l", CPUStat{}),
}

snapshot.AppendProcesses(processes)

assert.IsEqual(t, 2, len(snapshot.Processes), "processes size should be 2")
}

0 comments on commit 9f9eab9

Please sign in to comment.