go_dreamfactory/cmd/v2/ui/perf_pb.go
2022-12-06 17:39:25 +08:00

163 lines
3.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package ui
import (
"fmt"
cfg "go_dreamfactory/cmd/v2/configure/structs"
"go_dreamfactory/cmd/v2/lib"
"go_dreamfactory/cmd/v2/lib/common"
"go_dreamfactory/cmd/v2/lib/storage"
"go_dreamfactory/cmd/v2/service"
"go_dreamfactory/cmd/v2/service/observer"
"go_dreamfactory/comm"
"go_dreamfactory/pb"
"time"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
"github.com/Pallinder/go-randomdata"
"github.com/sirupsen/logrus"
"github.com/spf13/cast"
"google.golang.org/protobuf/proto"
)
type perfPb struct {
appAdapter
obs observer.Observer
itemList common.ItemList
pbList func() //协议列表
conf *storage.Config
}
func (app *perfPb) LazyInit(ptService service.PttService, obs observer.Observer) error {
app.obs = obs
app.conf = perfWin.UIImpl.config
app.tabItem = container.NewTabItemWithIcon(common.TOOLBAR_PERF_PB, theme.ContentCopyIcon(), nil)
content := container.NewMax()
content.Objects = []fyne.CanvasObject{}
app.itemList = *common.NewItemList()
app.itemList.ItemList = app.itemList.CreateList()
app.pbList = func() {
if tables, err := cfg.NewTables(common.Loader); err != nil {
println(err.Error())
} else {
for _, v := range tables.TestFlow.GetDataList() {
item := common.Item{
Id: cast.ToString(v.Id),
Text: fmt.Sprintf("%-6d %-20s %-20s %s", v.Id, v.Msg, v.Route, v.Params),
Data: v,
}
app.itemList.AddItem(item)
}
}
}
defer app.pbList()
// 刷新按钮
refeshBtn := widget.NewButtonWithIcon("", theme.ViewRefreshIcon(), func() {
app.itemList.Reset()
app.pbList()
})
// next按钮
nextBtn := widget.NewButtonWithIcon("执行", theme.ConfirmIcon(), func() {
// 根据填写的用户数创建用户
userCount := perfWin.config.UserCount
// 遍历时通过sleep 控制增加的用户数
for i := int32(0); i < userCount; i++ {
handler := service.NewWsCli(perfWin.config.WsAddr, time.Duration(perfWin.config.Pressure.TimeoutMs))
// 遍历测试的协议
for _, item := range app.itemList.CachedList.Items {
if data, ok := item.Data.(*cfg.GameTestFlowData); ok {
logrus.Infof("%v %v", data.Route, data.Params)
var (
reqData []byte
err error
)
if data.Route == "user.login" {
reqData, err = app.loginReq()
if err != nil {
logrus.Error(err)
continue
}
} else {
if pb, ok := pbMap[data.Route]; ok {
common.Json2Pb(data.Params, pb)
reqData, err = proto.Marshal(pb)
if err != nil {
logrus.Error(err)
continue
}
}
}
handler.SetReq(reqData)
assist := app.createAssistant(handler)
assist.Start()
assist.ShowResult()
}
}
// time.Sleep(time.Second)
}
})
//layout
c := container.NewBorder(container.NewHBox(refeshBtn), container.NewHBox(layout.NewSpacer(), nextBtn), nil, nil, app.itemList.ItemList)
content.Objects = append(content.Objects, c)
app.tabItem.Content = content
return nil
}
func (a *perfPb) GetAppName() string {
return common.TOOLBAR_PERF_PB
}
//
func (a *perfPb) createAssistant(handler lib.Handler) lib.Aiassistant {
param := lib.ParamMgr{
Caller: handler,
Timeout: time.Duration(a.conf.Pressure.TimeoutMs) * time.Millisecond,
Lps: uint32(a.conf.Pressure.Concurrency),
Duration: time.Duration(a.conf.Pressure.DurationS) * time.Second,
ResultCh: make(chan *lib.CallResult, 50),
}
assist, err := lib.NewAssistant(param)
if err != nil {
logrus.Errorf("AI助手初始化错误: %v", err)
return nil
}
return assist
}
func (a *perfPb) loginReq() ([]byte, error) {
head := &pb.UserMessage{MainType: "user", SubType: "login"}
sid := "dfz"
account := randomdata.SillyName()
head.Sec = common.BuildSecStr(sid, account)
if comm.ProtoMarshal(&pb.UserLoginReq{
Sid: sid,
Account: account,
}, head) {
data, err := proto.Marshal(head)
if err != nil {
return nil, err
}
return data, nil
}
return nil, nil
}