From 0005a425f53b3e9e1fb38141bf8c7b16825cfc5d Mon Sep 17 00:00:00 2001 From: meixiongfeng <766881921@qq.com> Date: Fri, 2 Dec 2022 18:06:04 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8E=8B=E6=B5=8B=E5=B7=A5=E5=85=B7=E8=BE=93?= =?UTF-8?q?=E5=87=BA=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/gateway/module.go | 1 + services/gateway/main.go | 1 + stress/server/statistics/statistics.go | 70 +++++++++++++++++++------- stress/stress.go | 40 +++++++++++++-- 4 files changed, 92 insertions(+), 20 deletions(-) diff --git a/modules/gateway/module.go b/modules/gateway/module.go index ac379b560..d3b11cba2 100644 --- a/modules/gateway/module.go +++ b/modules/gateway/module.go @@ -62,6 +62,7 @@ func (this *Gateway) Init(service core.IService, module core.IModule, options co // Start 模块启动函数 注册rpc服务接口提供用户相关的rpc接口服务 func (this *Gateway) Start() (err error) { + log.Debugf("dddd") _name2Func := map[string]any{ // 注册用户绑定uid接口 登录成功后触发 string(comm.Rpc_GatewayAgentBind): this.agentMgr.Bind, diff --git a/services/gateway/main.go b/services/gateway/main.go index e110fb4ed..ecae5fe13 100644 --- a/services/gateway/main.go +++ b/services/gateway/main.go @@ -26,6 +26,7 @@ func main() { ) s.OnInstallComp( //装备组件 ) + lego.Run(s, //运行模块 gateway.NewModule(), ) diff --git a/stress/server/statistics/statistics.go b/stress/server/statistics/statistics.go index ed6fd101d..97b092dc5 100644 --- a/stress/server/statistics/statistics.go +++ b/stress/server/statistics/statistics.go @@ -3,17 +3,21 @@ package statistics import ( "fmt" + "io/ioutil" "sort" "strings" "sync" "time" + "go_dreamfactory/lego/core" + "go_dreamfactory/lego/sys/log" "go_dreamfactory/stress/tools" + "go_dreamfactory/stress/model" + "golang.org/x/text/language" "golang.org/x/text/message" - - "go_dreamfactory/stress/model" + "gopkg.in/yaml.v2" ) var ( @@ -23,6 +27,35 @@ var ( requestTimeList []uint64 // 所有请求响应时间 ) +type Options struct { + ConfPath string //配置文件路径 + Version string //服务版本 + Setting core.ServiceSttings //服务参数配置 +} + +func newOptions(path string) *Options { + options := &Options{} + + yamlFile, err := ioutil.ReadFile(path) + if err != nil { + panic(fmt.Sprintf("读取服务配置【%s】文件失败err:%v:", options.ConfPath, err)) + } + err = yaml.Unmarshal(yamlFile, &options.Setting) + if err != nil { + panic(fmt.Sprintf("读取服务配置【%s】文件失败err:%v:", options.ConfPath, err)) + } + return options +} + +func InitLog() { + op := newOptions("./conf/stress_1.yaml") + if err := log.OnInit(op.Setting.Sys["log"]); err != nil { + panic(fmt.Sprintf("Sys log Init err:%v", err)) + } else { + log.Infof("ddddd Sys log Init success !") + } +} + // ReceivingResults 接收结果并处理 // 统计的时间都是纳秒,显示的时间 都是毫秒 // concurrent 并发数 @@ -30,6 +63,7 @@ func ReceivingResults(concurrent uint64, ch <-chan *model.RequestResults, wg *sy defer func() { wg.Done() }() + var stopChan = make(chan bool) // 时间 var ( @@ -105,16 +139,17 @@ func ReceivingResults(concurrent uint64, ch <-chan *model.RequestResults, wg *sy calculateData(concurrent, processingTime, requestTime, maxTime, minTime, successNum, failureNum, chanIDLen, errCode, receivedBytes) - fmt.Printf("\n\n") - fmt.Println("************************* 结果 stat ****************************") - fmt.Println("处理协程数量:", concurrent) - // fmt.Println("处理协程数量:", concurrent, "程序处理总时长:", fmt.Sprintf("%.3f", float64(processingTime/concurrent)/1e9), "秒") - fmt.Println("请求总数(并发数*请求数 -c * -n):", successNum+failureNum, "总请求时间:", + log.Infof("\n\n") + log.Infof("************************* 结果 stat ****************************") + log.Infof("处理协程数量:", concurrent) + + // fmt.Println("处理协程数量:", concurrent, "程序处理总时长:", log("%.3f", float64(processingTime/concurrent)/1e9), "秒") + log.Infof("请求总数(并发数*请求数 -c * -n):", successNum+failureNum, "总请求时间:", fmt.Sprintf("%.3f", float64(requestTime)/1e9), "秒", "successNum:", successNum, "failureNum:", failureNum) printTop(requestTimeList) - fmt.Println("************************* 结果 end ****************************") - fmt.Printf("\n\n") + log.Infof("************************* 结果 end ****************************") + log.Infof("\n\n") } // printTop 排序后计算 top 90 95 99 @@ -125,9 +160,10 @@ func printTop(requestTimeList []uint64) { all := tools.MyUint64List{} all = requestTimeList sort.Sort(all) - fmt.Println("tp90:", fmt.Sprintf("%.3f", float64(all[int(float64(len(all))*0.90)]/1e6))) - fmt.Println("tp95:", fmt.Sprintf("%.3f", float64(all[int(float64(len(all))*0.95)]/1e6))) - fmt.Println("tp99:", fmt.Sprintf("%.3f", float64(all[int(float64(len(all))*0.99)]/1e6))) + log.Infof("tp90:", fmt.Sprintf("%.3f", float64(all[int(float64(len(all))*0.90)]/1e6))) + log.Infof("tp95:", fmt.Sprintf("%.3f", float64(all[int(float64(len(all))*0.95)]/1e6))) + log.Infof("tp99:", fmt.Sprintf("%.3f", float64(all[int(float64(len(all))*0.99)]/1e6))) + } // calculateData 计算数据 @@ -162,11 +198,11 @@ func calculateData(concurrent, processingTime, requestTime, maxTime, minTime, su // header 打印表头信息 func header() { - fmt.Printf("\n\n") + log.Infof("\n\n") // 打印的时长都为毫秒 总请数 - fmt.Println("─────┬───────┬───────┬───────┬────────┬────────┬────────┬────────┬────────┬────────┬────────") - fmt.Println(" 耗时│ 并发数│ 成功数│ 失败数│ qps │最长耗时│最短耗时│平均耗时│下载字节│字节每秒│ 状态码") - fmt.Println("─────┼───────┼───────┼───────┼────────┼────────┼────────┼────────┼────────┼────────┼────────") + log.Infof("─────┬───────┬───────┬───────┬────────┬────────┬────────┬────────┬────────┬────────┬────────") + log.Infof(" 耗时│ 并发数│ 成功数│ 失败数│ qps │最长耗时│最短耗时│平均耗时│下载字节│字节每秒│ 状态码") + log.Infof("─────┼───────┼───────┼───────┼────────┼────────┼────────┼────────┼────────┼────────┼────────") return } @@ -198,7 +234,7 @@ func table(successNum, failureNum uint64, errCode *sync.Map, requestTimeFloat, chanIDLen, successNum, failureNum, qps, maxTimeFloat, minTimeFloat, averageTime, receivedBytesStr, speedStr, printMap(errCode)) - fmt.Println(result) + log.Infof(result) return } diff --git a/stress/stress.go b/stress/stress.go index 5822ce0f3..3dfa4b5fb 100644 --- a/stress/stress.go +++ b/stress/stress.go @@ -5,11 +5,16 @@ import ( "context" "flag" "fmt" + "go_dreamfactory/lego/core" "go_dreamfactory/stress/model" "go_dreamfactory/stress/server" + "go_dreamfactory/stress/server/statistics" + "io/ioutil" "runtime" "strings" "time" + + "gopkg.in/yaml.v2" ) // array 自定义数组参数 @@ -66,14 +71,43 @@ func init() { // main go 实现的压测工具 // 编译可执行文件 // +type Options struct { + ConfPath string //配置文件路径 + Version string //服务版本 + Setting core.ServiceSttings //服务参数配置 +} + +func newOptions(path string) *Options { + options := &Options{} + + yamlFile, err := ioutil.ReadFile(path) + if err != nil { + panic(fmt.Sprintf("读取服务配置【%s】文件失败err:%v:", options.ConfPath, err)) + } + err = yaml.Unmarshal(yamlFile, &options.Setting) + if err != nil { + panic(fmt.Sprintf("读取服务配置【%s】文件失败err:%v:", options.ConfPath, err)) + } + return options +} + //go:generate go build main.go func main() { runtime.GOMAXPROCS(cpuNumber) + flag.Parse() + statistics.InitLog() + // op := newOptions("./conf/stress_1.yaml") + // if err := log.OnInit(op.Setting.Sys["log"]); err != nil { + // panic(fmt.Sprintf("Sys log Init err:%v", err)) + // } else { + // log.Infof("Sys log Init success !") + // } + // log.Debugf("ddddd") //go run .\main.go -c 10 -n 10 -u ws://10.0.5.101:7891/gateway - concurrency = 100 - totalNumber = 100 + concurrency = 1 + totalNumber = 1 debugStr = "false" - requestURL = "ws://10.0.0.85:7891/gateway" + requestURL = "ws://10.0.0.9:7891/gateway" verify = "pb" if concurrency == 0 || totalNumber == 0 || (requestURL == "" && path == "") { fmt.Printf("示例: go run main.go -c 1 -n 1 -u https://www.baidu.com/ \n")