Skip to content

Commit

Permalink
Merge pull request #1 from aoliaoaoaojiao/dev-perfmon
Browse files Browse the repository at this point in the history
Dev perfmon
  • Loading branch information
ZhouYixun authored Aug 27, 2022
2 parents 4e3eb06 + 0585179 commit d5f8a03
Show file tree
Hide file tree
Showing 6 changed files with 633 additions and 10 deletions.
138 changes: 132 additions & 6 deletions device.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ import (
"context"
"errors"
"fmt"
"os"
"path"
"strings"
"time"

"github.com/electricbubble/gidevice/pkg/ipa"
"github.com/electricbubble/gidevice/pkg/libimobiledevice"
"github.com/electricbubble/gidevice/pkg/nskeyedarchiver"
uuid "github.com/satori/go.uuid"
"howett.net/plist"
"os"
"path"
"strings"
"time"
)

const LockdownPort = 62078
Expand Down Expand Up @@ -591,6 +590,133 @@ func (d *device) MoveCrashReport(hostDir string, opts ...CrashReportMoverOption)
return d.crashReportMover.Move(hostDir, opts...)
}

func (d *device) GetPerfmon(opts *PerfmonOption) (out chan interface{}, outCancel context.CancelFunc, perfErr error) {
if d.lockdown == nil {
if _, err := d.lockdownService(); err != nil {
return nil, nil, err
}
}
if opts==nil {
return nil, nil, fmt.Errorf("parameter is empty")
}
if !opts.OpenChanCPU &&!opts.OpenChanMEM &&!opts.OpenChanGPU &&!opts.OpenChanFPS &&!opts.OpenChanNetWork {
opts.OpenChanCPU = true
opts.OpenChanMEM = true
opts.OpenChanGPU = true
opts.OpenChanFPS = true
opts.OpenChanNetWork = true
}

var err error

var instruments Instruments
ctx, cancel := context.WithCancel(context.Background())

chanCPU := make(chan CPUInfo)
chanMEM := make(chan MEMInfo)
var cancelSysmontap context.CancelFunc

if opts.OpenChanCPU || opts.OpenChanMEM {
instruments, err = d.lockdown.InstrumentsService()
if err != nil {
return nil, cancel, err
}
chanCPU, chanMEM, cancelSysmontap, err = instruments.StartSysmontapServer(opts.PID, ctx)
if err != nil {
cancelSysmontap()
return nil, cancel, err
}
}

chanFPS := make(chan FPSInfo)
chanGPU := make(chan GPUInfo)
var cancelOpengl context.CancelFunc

if opts.OpenChanGPU || opts.OpenChanFPS {
instruments, err = d.lockdown.InstrumentsService()
if err != nil {
return nil, cancel, err
}
chanFPS, chanGPU, cancelOpengl, err = instruments.StartOpenglServer(ctx)
if err != nil {
cancelOpengl()
return nil, cancel, err
}
}

chanNetWork := make(chan NetWorkingInfo)
var cancelNetWork context.CancelFunc
if opts.OpenChanNetWork {
instruments, err = d.lockdown.InstrumentsService()
if err != nil {
return nil, cancel, err
}
chanNetWork, cancelNetWork, err = instruments.StartNetWorkingServer(ctx)
if err != nil {
cancelNetWork()
return nil, cancel, err
}
}
// 弃用之前的PerfMonData ,统一汇总到interface里,由用户自行决定处理数据
result := make(chan interface{})
go func() {
for {
select {
case v, ok := <-chanCPU:
if opts.OpenChanCPU && ok {
result<-v
}
case v, ok := <-chanMEM:
if opts.OpenChanMEM && ok {
result<-v
}
case v, ok := <-chanFPS:
if opts.OpenChanFPS && ok {
result<-v
}
case v, ok := <-chanGPU:
if opts.OpenChanGPU && ok {
result<-v
}
case v, ok := <-chanNetWork:
if opts.OpenChanNetWork && ok {
result<-v
}
case <-ctx.Done():
err:=d.stopPerfmon(opts)
if err!=nil {
fmt.Println(err)
}
close(result)
return
}
}
}()
return result, cancel, err
}

func (d *device)stopPerfmon(opts *PerfmonOption)(err error) {
if _, err = d.instrumentsService(); err != nil {
return err
}
if opts.OpenChanCPU || opts.OpenChanMEM {
if err = d.instruments.StopSysmontapServer(); err != nil {
return err
}
}
if opts.OpenChanGPU || opts.OpenChanFPS {
if err = d.instruments.StopOpenglServer(); err != nil {
return err
}
}
if opts.OpenChanNetWork {
if err = d.instruments.StopNetWorkingServer(); err != nil {
return err
}
}
return nil
}

func (d *device) XCTest(bundleID string, opts ...XCTestOption) (out <-chan string, cancel context.CancelFunc, err error) {
xcTestOpt := defaultXCTestOption()
for _, fn := range opts {
Expand Down Expand Up @@ -836,4 +962,4 @@ func (d *device) _uploadXCTestConfiguration(bundleID string, sessionId uuid.UUID
}

return
}
}
48 changes: 47 additions & 1 deletion device_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package giDevice

import (
"encoding/json"
"fmt"
"os"
"os/signal"
Expand Down Expand Up @@ -68,7 +69,7 @@ func Test_device_SavePairRecord(t *testing.T) {
func Test_device_XCTest(t *testing.T) {
setupLockdownSrv(t)

bundleID = "com.leixipaopao.WebDriverAgentRunner.xctrunner"
bundleID = "com.DataMesh.CheckList"
out, cancel, err := dev.XCTest(bundleID)
// out, cancel, err := dev.XCTest(bundleID, WithXCTestEnv(map[string]interface{}{"USE_PORT": 8222, "MJPEG_SERVER_PORT": 8333}))
if err != nil {
Expand Down Expand Up @@ -154,6 +155,51 @@ func Test_device_Shutdown(t *testing.T) {
dev.Shutdown()
}

func Test_device_Perf(t *testing.T) {
//setupDevice(t)
//SetDebug(true,true)
setupLockdownSrv(t)

c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, os.Kill)

var opts = &PerfmonOption{
//PID: "0",
//OpenChanNetWork: true,
//OpenChanFPS: true,
OpenChanMEM: true,
//OpenChanCPU: true,
//OpenChanGPU: true,
}

outData, cannel, err := dev.GetPerfmon(opts)
if err != nil {
fmt.Println(err)
os.Exit(0)
}
var timeLast = time.Now().Unix()
for {
select {
case <-c:
if cannel!=nil {
cannel()
}
default:
data ,ok := <-outData
if !ok {
fmt.Println("end get perfmon data")
return
}
result, _ := json.MarshalIndent(data, "", "\t")
fmt.Println(string(result))
if time.Now().Unix()-timeLast >60 {
cannel()
}
}
}

}

func Test_device_InstallationProxyBrowse(t *testing.T) {
setupDevice(t)

Expand Down
24 changes: 24 additions & 0 deletions idevice.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ type Device interface {
springBoardService() (springBoard SpringBoard, err error)
GetIconPNGData(bundleId string) (raw *bytes.Buffer, err error)
GetInterfaceOrientation() (orientation OrientationState, err error)

GetPerfmon(opts *PerfmonOption) (out chan interface{}, outCancel context.CancelFunc, perfErr error)
}

type DeviceProperties = libimobiledevice.DeviceProperties
Expand Down Expand Up @@ -153,6 +155,19 @@ type Instruments interface {
// SysMonStart(cfg ...interface{}) (_ interface{}, err error)

registerCallback(obj string, cb func(m libimobiledevice.DTXMessageResult))

StartOpenglServer(ctx context.Context) (chanFPS chan FPSInfo, chanGPU chan GPUInfo, cancel context.CancelFunc, err error)

StopOpenglServer() (err error)

StartSysmontapServer(pid string, ctx context.Context) (chanCPU chan CPUInfo, chanMem chan MEMInfo, cancel context.CancelFunc, err error)

StopSysmontapServer() (err error)
//ProcessNetwork(pid int) (out <-chan interface{}, cancel context.CancelFunc, err error)

StartNetWorkingServer(ctx context.Context) (chanNetWorking chan NetWorkingInfo, cancel context.CancelFunc, err error)

StopNetWorkingServer() (err error)
}

type Testmanagerd interface {
Expand Down Expand Up @@ -357,6 +372,15 @@ func WithUpdateToken(updateToken string) AppListOption {
}
}

type PerfmonOption struct {
PID string
OpenChanGPU bool
OpenChanFPS bool
OpenChanCPU bool
OpenChanMEM bool
OpenChanNetWork bool
}

type Process struct {
IsApplication bool `json:"isApplication"`
Name string `json:"name"`
Expand Down
Loading

0 comments on commit d5f8a03

Please sign in to comment.