Skip to content

Commit

Permalink
性能参数化
Browse files Browse the repository at this point in the history
  • Loading branch information
aoliaoaoaojiao committed Jan 11, 2023
1 parent 2ab2cdb commit 7aaad2b
Show file tree
Hide file tree
Showing 9 changed files with 290 additions and 388 deletions.
77 changes: 74 additions & 3 deletions cmd/perfmon.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,91 @@
package cmd

import (
"github.com/SonicCloudOrg/sonic-android-supply/cmd/perfmon"
"fmt"
"github.com/SonicCloudOrg/sonic-android-supply/src/entity"
"github.com/SonicCloudOrg/sonic-android-supply/src/perfmonUtil"
"github.com/SonicCloudOrg/sonic-android-supply/src/util"
"github.com/spf13/cobra"
"log"
"os"
"os/signal"
"time"
)

var perfmonCmd = &cobra.Command{
Use: "perfmon",
Short: "Get device performance",
Long: "Get device performance",
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
device := util.GetDevice(serial)
pid, err := perfmonUtil.GetPidOnPackageName(device, packageName)
if err != nil {
fmt.Println("no corresponding application PID found")
pid = ""
}
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt, os.Kill)

if !perfOptions.ProcMem && !perfOptions.ProcCPU &&
!perfOptions.ProcFPS && !perfOptions.SystemCPU &&
!perfOptions.SystemGPU && !perfOptions.SystemNetWorking &&
!perfOptions.SystemMem{
perfOptions.SystemCPU = true
perfOptions.SystemMem = true
}

timer := time.Tick(time.Duration(refreshTime * int(time.Millisecond)))
done := false
for !done {
select {
case <-sig:
done = true
fmt.Println()
case <-timer:
sysStatus,err1 := perfmonUtil.GetSystemStats(device,perfOptions)
if err1 != nil {
log.Panic(err1)
}
processInfo, err2 := perfmonUtil.GetProcessInfo(device, pid, packageName,perfOptions, 1)
if err2 != nil {
log.Panic(err2)
}
var perfData = &entity.PerfmonData{}
perfData.System = sysStatus
perfData.Process = processInfo
perfData.TimeStamp = time.Now().Unix()

data := util.ResultData(perfData)
fmt.Println(util.Format(data, isFormat, isJson))

}
}
},
}

var (
perfOptions entity.PerfOption
pid int
packageName string
refreshTime int
)

func init() {
rootCmd.AddCommand(perfmonCmd)
perfmon.InitPerfmon(perfmonCmd)
perfmonCmd.Flags().StringVarP(&serial, "serial", "s", "", "device serial")
perfmonCmd.Flags().IntVarP(&pid, "pid", "d", -1, "get PID data")
perfmonCmd.Flags().StringVarP(&packageName, "", "p", "", "app package name")
perfmonCmd.Flags().BoolVar(&perfOptions.SystemCPU, "sys-cpu", false, "get system cpu data")
perfmonCmd.Flags().BoolVar(&perfOptions.SystemMem, "sys-mem", false, "get system memory data")
//perfmonCmd.Flags().BoolVar(&sysDisk, "sys-disk", false, "get system disk data")
perfmonCmd.Flags().BoolVar(&perfOptions.SystemNetWorking, "sys-network", false, "get system networking data")
perfmonCmd.Flags().BoolVar(&perfOptions.SystemGPU, "gpu", false, "get gpu data")
perfmonCmd.Flags().BoolVar(&perfOptions.ProcFPS, "proc-fps", false, "get fps data")
perfmonCmd.Flags().BoolVar(&perfOptions.ProcThreads,"proc-threads",false,"get process threads")
//perfmonCmd.Flags().BoolVar(&, "proc-network", false, "get process network data")
perfmonCmd.Flags().BoolVar(&perfOptions.ProcCPU, "proc-cpu", false, "get process cpu data")
perfmonCmd.Flags().BoolVar(&perfOptions.SystemMem, "proc-mem", false, "get process mem data")
perfmonCmd.Flags().IntVarP(&refreshTime, "refresh", "r", 1000, "data refresh time(millisecond)")
perfmonCmd.Flags().BoolVarP(&isFormat, "format", "f", false, "convert to JSON string and format")
perfmonCmd.Flags().BoolVarP(&isJson, "json", "j", false, "convert to JSON string")
}
77 changes: 0 additions & 77 deletions cmd/perfmon/process.go

This file was deleted.

62 changes: 0 additions & 62 deletions cmd/perfmon/system.go

This file was deleted.

6 changes: 5 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ https://github.com/SonicCloudOrg/sonic-android-supply
`,
}

var serial string
var (
serial string
isFormat bool
isJson bool
)

// Execute error
func Execute() {
Expand Down
33 changes: 17 additions & 16 deletions cmd/perfmon/perfmonInit.go → src/entity/perfmon.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,24 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package perfmon
package entity

import (
"github.com/spf13/cobra"
)
import "encoding/json"

var perfmonRootCMD *cobra.Command

var (
interval int
isJson bool
isFormat bool
serial string
)
type PerfmonData struct {
System *SystemInfo `json:"system,omitempty"`
Process *ProcessInfo `json:"process,omitempty"`
TimeStamp int64 `json:"timeStamp"`
}

func InitPerfmon(perfmonCMD *cobra.Command) {
perfmonRootCMD = perfmonCMD
initProcessPerfmon()
initSystemPerfmon()
func (p *PerfmonData) ToJson() string {
str, _ := json.Marshal(p)
return string(str)
}
func (p *PerfmonData) ToString() string {
return p.ToJson()
}
func (p *PerfmonData) ToFormat() string {
str, _ := json.MarshalIndent(p, "", "\t")
return string(str)
}
76 changes: 34 additions & 42 deletions src/entity/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package entity

import (
"encoding/json"
"fmt"
)

type ProcessIO struct {
Expand Down Expand Up @@ -109,16 +108,11 @@ type ProcessStatus struct {
type ProcessInfo struct {
Name string `json:"name"`
Pid string `json:"pid"`
CpuUtilization float64 `json:"cpuUtilization"`
//ReadBytes int `json:"readBytes"`
//WriteBytes int `json:"writeBytes"`
PhyRSS int `json:"phyRSS"`
VmSize int `json:"vmRSS"`
Threads int `json:"threadCount"`
Rchar int `json:"readCharCount"`
Wchar int `json:"writeCharCount"`
FPS int `json:"fps"`
TimeStamp int64 `json:"timeStamp"`
CpuUtilization *float64 `json:"cpuUtilization"`
PhyRSS *int `json:"phyRSS"`
VmSize *int `json:"vmRSS"`
Threads *int `json:"threadCount"`
FPS *int `json:"fps"`
}

func (i *ProcessInfo) ToJson() string {
Expand All @@ -131,35 +125,33 @@ func (i *ProcessInfo) ToFormat() string {
return string(str)
}

func (i *ProcessInfo) ToString() string {

var result = fmt.Sprintf(
//%s%s%s%s up %s%s%s
`
PID:%s%s%s Name:%s%s%s
CPU:
%s%.2f%s%% cpuUtilizetion
%s%d%s ThreadCount
Memory:
physicalMemory = %s%d%s
virtualMemory = %s%d%s
R/W char:
Rchar = %s%d%s
Wchar = %s%d%s
`,
escBrightWhite, i.Name, escReset,
escBrightWhite, i.Pid, escReset,
escBrightWhite, i.CpuUtilization, escReset,
escBrightWhite, i.Threads, escReset,
escBrightWhite, i.PhyRSS, escReset,
escBrightWhite, i.VmSize, escReset,
escBrightWhite, i.Rchar, escReset,
escBrightWhite, i.Wchar, escReset,
)
return result
//return fmt.Sprintf("name:%s pid:%s cpuUtilizetion:%f phyRss:%d vmRss:%d threadCount:%d readCharCount:%d writeCharCount:%d timeStamp:%d", i.Name, i.Pid, i.CpuUtilization, i.PhyRSS, i.VmSize, i.Threads, i.Rchar, i.Wchar, time.Now().Unix())
func (i *ProcessInfo) ToString() string{
return i.ToJson()
}

//func (i *ProcessInfo) ToString() string {
//
// var result = fmt.Sprintf(
// //%s%s%s%s up %s%s%s
// `
//PID:%s%s%s Name:%s%s%s
//
//CPU:
// %s%.2f%s%% cpuUtilizetion
// %s%d%s ThreadCount
//
//Memory:
// physicalMemory = %s%d%s
// virtualMemory = %s%d%s
//
//`,
// escBrightWhite, i.Name, escReset,
// escBrightWhite, i.Pid, escReset,
// escBrightWhite, i.CpuUtilization, escReset,
// escBrightWhite, i.Threads, escReset,
// escBrightWhite, i.PhyRSS, escReset,
// escBrightWhite, i.VmSize, escReset,
// )
// return result
// //return fmt.Sprintf("name:%s pid:%s cpuUtilizetion:%f phyRss:%d vmRss:%d threadCount:%d readCharCount:%d writeCharCount:%d timeStamp:%d", i.Name, i.Pid, i.CpuUtilization, i.PhyRSS, i.VmSize, i.Threads, i.Rchar, i.Wchar, time.Now().Unix())
//}
Loading

0 comments on commit 7aaad2b

Please sign in to comment.