Skip to content

Commit

Permalink
fix restart perf ws bug
Browse files Browse the repository at this point in the history
  • Loading branch information
aoliaoaoaojiao committed Jul 14, 2024
1 parent de797e7 commit 0eb8331
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 145 deletions.
8 changes: 4 additions & 4 deletions android/perf/perf_tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"fionna/entity"
"fmt"
log "github.com/sirupsen/logrus"
"io"
"math/rand"
"net"
"path"
Expand Down Expand Up @@ -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",
Expand All @@ -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
}
Expand All @@ -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)
Expand Down Expand Up @@ -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()
}
Expand Down
18 changes: 13 additions & 5 deletions server/android/android_control_ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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() {
Expand Down Expand Up @@ -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
Expand Down
135 changes: 0 additions & 135 deletions server/android/perf_server.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package android

import (
"context"
"encoding/json"
"fionna/android/android_util"
"fionna/android/gadb"
"fionna/android/perf"
"fionna/entity"
"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"
Expand Down Expand Up @@ -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 = &timestamp
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()

Expand Down

0 comments on commit 0eb8331

Please sign in to comment.