Skip to content

Commit

Permalink
Add snapshot network network stat print. (#105)
Browse files Browse the repository at this point in the history
* Add snapshot network network stat print.

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 31, 2022
1 parent 1da522f commit e56e4bd
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 13 deletions.
57 changes: 52 additions & 5 deletions internal/print/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ const (
jsonPrefix = ""
jsonIndent = " "
printSeparatedLine = "========================================================================="
printSubTitleLine = "-------------------------------------------------------------------------"
tcpStateNoIndentMinimalSize = 11

tcp4TypeName = "TCP4"
tcp6TypeName = "TCP6"
)

func printSnapshotTitle(title string) {
Expand All @@ -25,13 +29,55 @@ func printSnapshotTitle(title string) {

fmt.Println(printSeparatedLine)
fmt.Printf("%s\n", title)
fmt.Println(printSubTitleLine)
}

func printSnapshotTimestamp(timestamp time.Time) {
fmt.Printf("Timestamp: %v\n", timestamp)
fmt.Println(printSeparatedLine)
}

func printSnapshotNetworkTCPConnections(tcpStat snapshot.TCPStat, tcpType string) {
fmt.Printf("%s-Connections\t%d\n", tcpType, tcpStat.ConnectionCount)
}

func printSnapshotNetworkTCPConnectionsByState(tcpStat snapshot.TCPStat, state, tcpType string) {
printSnapshotConnetionState(tcpType, state)

countByState := tcpStat.ConnectionCountByState

if count, has := countByState[state]; has {
fmt.Printf("\t%d", count)
}

fmt.Printf("\n")
}

func printSnapshotNetworkTCPConnectionsStates(tcpStat snapshot.TCPStat, tcpType string) {
states := snapshot.GetTCPStates()

for _, state := range states {
printSnapshotNetworkTCPConnectionsByState(tcpStat, state, tcpType)
}
}

func printSnapshotNetworkStat(networkStat snapshot.NetworkStat) {
fmt.Println("Print snapshot network stat:")
fmt.Println(printSubTitleLine)

printSnapshotNetworkTCPConnections(networkStat.TCP4Stat, tcp4TypeName)
printSnapshotNetworkTCPConnectionsStates(networkStat.TCP4Stat, tcp4TypeName)

printSnapshotNetworkTCPConnections(networkStat.TCP6Stat, tcp6TypeName)
printSnapshotNetworkTCPConnectionsStates(networkStat.TCP6Stat, tcp6TypeName)

fmt.Println(printSeparatedLine)
}

func printSnapshotStat(spshot snapshot.Snapshot) {
printSnapshotNetworkStat(spshot.Network.NetworkStat)
}

func printSnapshotProcessesPID(processes []snapshot.Process) {
fmt.Printf("PID\t\t")

Expand Down Expand Up @@ -152,7 +198,7 @@ func printSnapshotProcessesTCP4Connections(processes []snapshot.Process) {
fmt.Printf("\n")
}

func printSnapshotProcessConnetionState(prefix string, state string) {
func printSnapshotConnetionState(prefix string, state string) {
fmt.Printf("%v-%v", prefix, state)

if len(state) < tcpStateNoIndentMinimalSize {
Expand All @@ -161,7 +207,7 @@ func printSnapshotProcessConnetionState(prefix string, state string) {
}

func printSnapshotProcessesTCP4ConnectionsByState(processes []snapshot.Process, state string) {
printSnapshotProcessConnetionState("TCP4", state)
printSnapshotConnetionState("TCP4", state)

for _, process := range processes {
countByState := process.NetworkStat.TCP4Stat.ConnectionCountByState
Expand Down Expand Up @@ -193,7 +239,7 @@ func printSnapshotProcessesTCP6Connections(processes []snapshot.Process) {
}

func printSnapshotProcessesTCP6ConnectionsByState(processes []snapshot.Process, state string) {
printSnapshotProcessConnetionState("TCP6", state)
printSnapshotConnetionState("TCP6", state)

for _, process := range processes {
countByState := process.NetworkStat.TCP6Stat.ConnectionCountByState
Expand Down Expand Up @@ -238,7 +284,7 @@ func printSnapshotProcessesMemory(processes []snapshot.Process) {
printSnapshotProcessesMemoryVMLib(processes)
}

func printSnapshotProcesses(processes []snapshot.Process) {
func printSnapshotProcessesStat(processes []snapshot.Process) {
printSnapshotProcessesCmdLine(processes)
printSnapshotProcessesCPU(processes)
printSnapshotProcessesMemory(processes)
Expand All @@ -253,7 +299,8 @@ func printTextSnapshot(snapshot snapshot.Snapshot, title string) {
printSnapshotTitle(title)

printSnapshotTimestamp(snapshot.Timestamp)
printSnapshotProcesses(snapshot.Processes)
printSnapshotStat(snapshot)
printSnapshotProcessesStat(snapshot.Processes)

printSnapshotFoot()
}
Expand Down
49 changes: 41 additions & 8 deletions internal/print/print_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,43 @@ import (
"time"
)

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

func getTestSnapshotNetwork() snapshot.Network {
return snapshot.Network{
NetworkStat: snapshot.NetworkStat{
TCP4Stat: snapshot.TCPStat{
ConnectionCount: 1,
ConnectionCountByState: map[string]int{
snapshot.TCPCloseWait: 1,
},
},
TCP6Stat: snapshot.TCPStat{
ConnectionCount: 1,
ConnectionCountByState: map[string]int{
snapshot.TCPSynSent: 1,
},
},
},
INodeToTCP4: map[string]snapshot.TCPConnection{
"1": snapshot.TCPConnection{
State: snapshot.TCPCloseWait,
INode: "1",
},
},
INodeToTCP6: map[string]snapshot.TCPConnection{
"2": snapshot.TCPConnection{
State: snapshot.TCPSynSent,
INode: "2",
},
},
}
}

Expand All @@ -22,7 +55,7 @@ func TestPrintSnapshotDefault(t *testing.T) {
testProcesses := []snapshot.Process{
snapshot.Process{},
}
testSnapshot := createSnapshot(time.Now(), testProcesses)
testSnapshot := createSnapshot(time.Now(), testProcesses, getTestSnapshotNetwork())

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 @@ -38,7 +71,7 @@ func TestPrintTextSnapshot(t *testing.T) {
snapshot.Process{},
}

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

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

testSnapshot := createSnapshot(time.Now(), testProcesses)
testSnapshot := createSnapshot(time.Now(), testProcesses, snapshot.Network{})
reconcileSnapshot(&testSnapshot, ops)

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

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

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

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

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

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

for i := 1; i < len(testSnapshot.Processes); i++ {
Expand Down

0 comments on commit e56e4bd

Please sign in to comment.