diff --git a/android/perf/perf_tool.go b/android/perf/perf_tool.go index 1ddbc28..e6d0bd7 100644 --- a/android/perf/perf_tool.go +++ b/android/perf/perf_tool.go @@ -10,7 +10,6 @@ import ( "fionna/entity" "fmt" log "github.com/sirupsen/logrus" - "io" "math/rand" "net" "path" @@ -120,9 +119,9 @@ func (s *PerfTool) Init() { } func (s *PerfTool) runBinary(cid int) { - var output io.Reader + var output net.Conn - output, err := s.dev.RunShellLoopCommand(fmt.Sprintf( + output, err := s.dev.RunShellLoopCommandSock(fmt.Sprintf( "LD_LIBRARY_PATH=/system/lib64:/system_ext/lib64:%s "+ "CLASSPATH=%s "+ "app_process / com.omsage.PerfTool.Run 1.0 cid=%d", @@ -145,7 +144,6 @@ func (s *PerfTool) runBinary(cid int) { } if !strings.Contains(string(bytesOutput[:n]), "Device") { s.clientStop() - // todo log.Error("start fail! output: " + string(bytesOutput[:n])) return } @@ -154,6 +152,7 @@ func (s *PerfTool) runBinary(cid int) { for { select { case <-s.exitCtx.Done(): + output.Close() return default: n, err = output.Read(bytesOutput) @@ -183,6 +182,7 @@ func (s *PerfTool) startServer() { s.frameSocket, err = s.perfToolLn.Accept() if err != nil { // todo + log.Error(err) s.frameSocket = nil s.exitCallBackFunc() } diff --git a/fionna-web b/fionna-web index 34caeec..a4c8e14 160000 --- a/fionna-web +++ b/fionna-web @@ -1 +1 @@ -Subproject commit 34caeece8248e43fd56e400ec83fdf9f96945f99 +Subproject commit a4c8e149ec61a6432a80f321d5b3d68d50187d53 diff --git a/server/android/android_control_ws.go b/server/android/android_control_ws.go index 0298d6a..ebfa573 100644 --- a/server/android/android_control_ws.go +++ b/server/android/android_control_ws.go @@ -10,10 +10,13 @@ import ( log "github.com/sirupsen/logrus" ) -var touchMap map[string]*touch.Touch = make(map[string]*touch.Touch) - func AndroidControl(r *gin.Engine) { r.GET("/android/control", func(c *gin.Context) { + + var touchMap map[string]*touch.Touch = make(map[string]*touch.Touch) + + var perfMap map[string]context.CancelFunc = make(map[string]context.CancelFunc) + ws, err := upGrader.Upgrade(c.Writer, c.Request, nil) if err != nil { log.Print("Error during connection upgradation:", err) @@ -38,8 +41,6 @@ func AndroidControl(r *gin.Engine) { exitCtx, exitFn := context.WithCancel(context.Background()) - perfExitCtx, perfExitFn := context.WithCancel(exitCtx) - var message entity.PerfRecvMessage go func() { @@ -70,11 +71,18 @@ func AndroidControl(r *gin.Engine) { switch message.MessageType { case entity.ClosePerfType: log.Println("client send close perf info,close perf...") - perfExitFn() + if perfExitFn, ok := perfMap[device.Serial()]; ok { + perfExitFn() + } case entity.StartPerfType: var perfConfig = &entity.PerfConfig{ IntervalTime: 1, } + + perfExitCtx, perfExitFn := context.WithCancel(exitCtx) + + perfMap[device.Serial()] = perfExitFn + err1 = json.Unmarshal(data, perfConfig) if err1 != nil { break diff --git a/server/android/perf_server.go b/server/android/perf_server.go index b77e5b4..09e89d0 100644 --- a/server/android/perf_server.go +++ b/server/android/perf_server.go @@ -1,8 +1,6 @@ package android import ( - "context" - "encoding/json" "fionna/android/android_util" "fionna/android/gadb" "fionna/android/perf" @@ -10,7 +8,6 @@ import ( "fionna/server/db" "fionna/server/util" "fmt" - "github.com/gin-gonic/gin" "github.com/google/uuid" "github.com/gorilla/websocket" log "github.com/sirupsen/logrus" @@ -420,138 +417,6 @@ func startGetPerf(perfWsConn *websocket.Conn, device *gadb.Device, config entity } } -func WebSocketPerf(r *gin.Engine) { - r.GET("/android/perf", func(c *gin.Context) { - - ws, err := upGrader.Upgrade(c.Writer, c.Request, nil) - if err != nil { - log.Print("Error during connection upgradation:", err) - return - } - - serialInfo := &entity.SerialInfo{} - // todo add error - ws.ReadJSON(serialInfo) - - device, err := android_util.GetDevice(client, serialInfo.SerialName) - if err != nil { - ws.WriteJSON(entity.NewPerfDataError(err.Error())) - log.Error(err) - } - - exitCtx, exitFn := context.WithCancel(context.Background()) - - var message entity.PerfRecvMessage - - go func() { - for { - select { - case <-exitCtx.Done(): - return - default: - defer func() { - if r := recover(); r != nil { - log.Error("perf ws recovered:", r) - exitFn() - } - }() - err := ws.ReadJSON(&message) - if err != nil { - log.Error("perf read message steam err:", err) - ws.WriteJSON(entity.NewPerfDataError("perf read message steam err:" + err.Error())) - break - } else { - if message.MessageType == entity.StartPerfType { - - data, err1 := json.Marshal(message.Data) - if err1 != nil { - log.Error("perf the data sent is not json") - ws.WriteJSON(entity.NewPerfDataError("perf the data sent is not json")) - break - } - // todo uuid - var perfConfig = &entity.PerfConfig{ - IntervalTime: 1, - } - err1 = json.Unmarshal(data, perfConfig) - - if err1 == nil { - - id := uuid.New() - - //reportBase := &entity.BaseModel{ - // UUID: id.String(), - //} - currentTime := time.Now() - - // 格式化时间为字符串 - formattedTime := currentTime.Format("2006-01-02 15:04:05") - - var testName = "" - - if perfConfig.PackageName != "" && perfConfig.Pid != "" { - testName = fmt.Sprintf("%s_%s_%s_pid%s_%s", serialInfo.ProductDevice, serialInfo.Model, perfConfig.PackageName, perfConfig.Pid, formattedTime) - } else if perfConfig.PackageName != "" && perfConfig.Pid == "" { - - testName = fmt.Sprintf("%s_%s_%s_%s", serialInfo.ProductDevice, serialInfo.Model, perfConfig.PackageName, formattedTime) - - perfConfig.Pid, err = android_util.GetPidOnPackageName(device, perfConfig.PackageName) - - if err != nil { - log.Error("get pid err:", err) - ws.WriteJSON(entity.NewPerfDataError("get pid err:" + err.Error())) - break - } - } else if perfConfig.PackageName == "" && perfConfig.Pid != "" { - testName = fmt.Sprintf("%s_%s_pid%s_%s", serialInfo.ProductDevice, serialInfo.Model, perfConfig.Pid, formattedTime) - } else { - testName = fmt.Sprintf("%s_%s_%s", serialInfo.ProductDevice, serialInfo.Model, formattedTime) - } - - serialInfo.TestName = &testName - timestamp := time.Now().UnixMilli() - - serialInfo.Timestamp = ×tamp - serialInfo.PackageName = &perfConfig.PackageName - - if serialInfo.UUID == "" { - serialInfo.UUID = id.String() - } - - db.GetDB().Create(serialInfo) - - if perfConfig.IntervalTime == 0 { - perfConfig.IntervalTime = 1 - } - - perfConfig.Ctx = exitCtx - perfConfig.CancelFn = exitFn - - perfConfig.UUID = id.String() - db.GetDB().Create(perfConfig) - - startGetPerf(ws, device, *perfConfig) - - } else { - log.Error("conversion message error,", err1) - ws.WriteJSON(entity.NewPerfDataError(err1.Error())) - break - } - } - if message.MessageType == entity.ClosePerfType { - log.Println("client send close perf info,close perf...") - exitFn() - } - if message.MessageType == entity.PongPerfType { - continue - } - } - } - } - }() - }) -} - func initPerfAndStart(serialInfo *entity.SerialInfo, perfConfig *entity.PerfConfig, device *gadb.Device, ws *websocket.Conn) { id := uuid.New()