go_dreamfactory/cmd/v2/ui/perf_pb.go
2022-12-08 11:36:30 +08:00

218 lines
5.5 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"
"strings"
"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.NavigateNextIcon(), nil)
nextBtn.OnTapped = func() {
defer closeApp3(perfWin.tabs, common.TOOLBAR_PERF_PB)
openApp3(perfWin.tabs, common.TOOLBAR_PERF_RES)
// 根据填写的用户数创建用户
userCount := perfWin.config.UserCount
// 遍历时通过sleep 控制增加的用户数
for i := int32(0); i < userCount; i++ {
handler, err := service.NewWsCli(perfWin.config.WsAddr, time.Duration(perfWin.config.Pressure.TimeoutMs)*time.Millisecond)
if err != nil {
continue
}
var login *UserLogin
login, err = app.loginReq(perfWin.config.SId)
handler.SetReq(login.req, false)
assist := app.createAssistantWithoutConf(handler)
assist.Start()
assist.ShowResult()
// 遍历测试的协议
for _, item := range app.itemList.CachedList.Items {
if data, ok := item.Data.(*cfg.GameTestFlowData); ok {
logrus.Debugf("%v %v", data.Route, data.Params)
var (
reqData []byte
err error
)
if login == nil {
continue
}
if v, ok := pbMap[data.Route]; ok {
routeStr := strings.SplitN(data.Route, ".", 2)
head := &pb.UserMessage{MainType: routeStr[0], SubType: routeStr[1]}
head.Sec = common.BuildSecStr(login.sid, login.account)
if err := common.Json2Pb(data.Params, v); err != nil {
logrus.Error(err)
continue
}
if comm.ProtoMarshal(v, head) {
reqData, err = proto.Marshal(head)
if err != nil {
logrus.Error(err)
continue
}
}
}
handler.SetReq(reqData, false)
assist := app.createAssistant(handler)
assist.Start()
assist.ShowResult()
}
}
// time.Sleep(time.Second)
}
obs.Notify(observer.EVENT_FINISH, true)
}
preBtn := widget.NewButtonWithIcon("上一步", theme.NavigateBackIcon(), nil)
preBtn.OnTapped = func() {
defer closeApp3(perfWin.tabs, common.TOOLBAR_PERF_PB)
openApp3(perfWin.tabs, common.TOOLBAR_PERF_CONF)
}
//layout
c := container.NewBorder(container.NewHBox(refeshBtn), container.NewHBox(layout.NewSpacer(), preBtn, 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) OnClose() bool {
return false
}
func (a *perfPb) createAssistantWithoutConf(handler lib.Handler) lib.Aiassistant {
param := lib.ParamMgr{
Caller: handler,
Timeout: time.Duration(a.conf.Pressure.TimeoutMs) * time.Millisecond,
Lps: 1,
Duration: 1,
ResultCh: make(chan *lib.CallResult, 50),
}
assist, err := lib.NewAssistant(a.obs, param)
if err != nil {
logrus.Errorf("AI助手初始化错误: %v", err)
return nil
}
return assist
}
//
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(a.obs, param)
if err != nil {
logrus.Errorf("AI助手初始化错误: %v", err)
return nil
}
return assist
}
type UserLogin struct {
req []byte
sid string
account string
}
func (a *perfPb) loginReq(sid string) (*UserLogin, error) {
login := &UserLogin{sid: sid}
head := &pb.UserMessage{MainType: "user", SubType: "login"}
account := randomdata.SillyName()
login.account = account
head.Sec = common.BuildSecStr(login.sid, login.account)
if comm.ProtoMarshal(&pb.UserLoginReq{
Sid: login.sid,
Account: login.account,
}, head) {
logrus.WithField("账号", login.account).Info("登录")
data, err := proto.Marshal(head)
if err != nil {
return nil, err
}
login.req = data
return login, nil
}
return nil, nil
}