Skip to content

Commit

Permalink
optimize perfd code
Browse files Browse the repository at this point in the history
  • Loading branch information
aoliaoaoaojiao committed Oct 28, 2022
1 parent 691f351 commit c1a9dd7
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 24 deletions.
36 changes: 12 additions & 24 deletions device.go
Original file line number Diff line number Diff line change
Expand Up @@ -647,30 +647,18 @@ func (d *device) PerfStart(opts ...PerfOption) (data <-chan []byte, err error) {
if perfOptions.SysCPU || perfOptions.SysMem || perfOptions.SysDisk ||
perfOptions.SysNetwork || len(perfOptions.ProcessAttributes) > 1 {

if perfOptions.SysCPU {
memoryAttr := []string{
"vmCompressorPageCount",
"vmExtPageCount",
"vmFreeCount",
"vmIntPageCount",
"vmPurgeableCount",
"vmWireCount",
"vmUsedCount",
"__vmSwapUsage"}
perfOptions.SystemAttributes = append(perfOptions.SystemAttributes, memoryAttr...)
}

if perfOptions.SysMem && !perfOptions.SysCPU {
memoryAttr := []string{
"vmCompressorPageCount",
"vmExtPageCount",
"vmFreeCount",
"vmIntPageCount",
"vmPurgeableCount",
"vmWireCount",
"vmUsedCount",
"__vmSwapUsage"}
perfOptions.SystemAttributes = append(perfOptions.SystemAttributes, memoryAttr...)
sysCpuAndMemAttr := []string{
"vmCompressorPageCount",
"vmExtPageCount",
"vmFreeCount",
"vmIntPageCount",
"vmPurgeableCount",
"vmWireCount",
"vmUsedCount",
"__vmSwapUsage"}

if perfOptions.SysMem || perfOptions.SysCPU {
perfOptions.SystemAttributes = append(perfOptions.SystemAttributes, sysCpuAndMemAttr...)
}

if perfOptions.SysDisk {
Expand Down
70 changes: 70 additions & 0 deletions perfd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,76 @@ func TestPerfSystemMonitor(t *testing.T) {
}
}

func TestPerfSystemCpu(t *testing.T) {
setupLockdownSrv(t)

data, err := dev.PerfStart(
WithPerfSystemCPU(true),
WithPerfOutputInterval(1000),
)
if err != nil {
t.Fatal(err)
}

timer := time.NewTimer(time.Duration(time.Second * 10))
for {
select {
case <-timer.C:
dev.PerfStop()
return
case d := <-data:
fmt.Println(string(d))
}
}
}

func TestPerfSystemMem(t *testing.T) {
setupLockdownSrv(t)

data, err := dev.PerfStart(
WithPerfSystemMem(true),
WithPerfOutputInterval(1000),
)
if err != nil {
t.Fatal(err)
}

timer := time.NewTimer(time.Duration(time.Second * 10))
for {
select {
case <-timer.C:
dev.PerfStop()
return
case d := <-data:
fmt.Println(string(d))
}
}
}

func TestPerfNotSystemPerfData(t *testing.T) {
setupLockdownSrv(t)

data, err := dev.PerfStart(
WithPerfSystemMem(false),
WithPerfSystemCPU(false),
WithPerfOutputInterval(1000),
)
if err != nil {
t.Fatal(err)
}

timer := time.NewTimer(time.Duration(time.Second * 10))
for {
select {
case <-timer.C:
dev.PerfStop()
return
case d := <-data:
fmt.Println(string(d))
}
}
}

func TestPerfProcessMonitor(t *testing.T) {
setupLockdownSrv(t)

Expand Down

0 comments on commit c1a9dd7

Please sign in to comment.