diff --git a/stress/robot/randomStr.go b/stress/robot/randomStr.go new file mode 100644 index 000000000..1cbcc7838 --- /dev/null +++ b/stress/robot/randomStr.go @@ -0,0 +1,56 @@ +package robot + +import ( + "encoding/hex" + "math/rand" + "testing" + "time" +) + +// 长度为62 +var nbytes []byte = []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890") + +func init() { + // 保证每次生成的随机数不一样 + rand.Seed(time.Now().UnixNano()) +} + +// 方法一 +func (r *Robot) RandStr1(n int) string { + result := make([]byte, n) + for i := 0; i < n; i++ { + result[i] = nbytes[rand.Int31()%62] + } + return string(result) +} + +// 方法二 +func (r *Robot) RandStr2(n int) string { + result := make([]byte, n/2) + rand.Read(result) + return hex.EncodeToString(result) +} + +// 对比一下两种方法的性能 +func (r *Robot) Benchmark1(b *testing.B) { + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + r.RandStr1(12) + } + }) + // 结果:539.1 ns/op +} + +func (r *Robot) Benchmark2(b *testing.B) { + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + r.RandStr2(12) + } + }) + // 结果: 157.2 ns/op +} + +// func TestOne(t *testing.T) { +// fmt.Println("方法一生成12位随机字符串: ", RandStr1(12)) +// fmt.Println("方法二生成12位随机字符串: ", RandStr2(12)) +// } diff --git a/stress/robot/robot.go b/stress/robot/robot.go index 4343f9ffd..3b61fb6c3 100644 --- a/stress/robot/robot.go +++ b/stress/robot/robot.go @@ -57,11 +57,10 @@ func NewRobot(url string) *Robot { zlog.Fatalf("websocket conn err:%v", err) } r := &Robot{ - ws: ws, - account: "user001", - sid: "dfmxf", + ws: ws, + sid: "dfmxf", } - + r.account = r.RandStr1(13) return r } @@ -191,13 +190,20 @@ func (r *Robot) MessageRsp(mainType, subType string) (bool, int64) { } return false, byteLen //fmt.Printf("接收消息=====resp:%v\n", resp.Chat) - } else if msg.MainType == mainType && msg.SubType == subType { + } else if msg.MainType == "user" && msg.SubType == "login" { resp := &pb.UserLoginResp{} if !comm.ProtoUnmarshal(msg, resp) { return false, byteLen } //fmt.Printf("接收消息=====loginData:%v,userExpand:%v\n", resp.Data, resp.Ex) return true, byteLen + } else if msg.MainType == "user" && msg.SubType == "create" { + resp := &pb.UserCreateResp{} + if !comm.ProtoUnmarshal(msg, resp) { + return false, byteLen + } + //fmt.Printf("接收消息=====loginData:%v,userExpand:%v\n", resp.Data, resp.Ex) + return true, byteLen } return false, byteLen } diff --git a/stress/server/golink/websocket_link.go b/stress/server/golink/websocket_link.go index a1db213f1..26fe27e5c 100644 --- a/stress/server/golink/websocket_link.go +++ b/stress/server/golink/websocket_link.go @@ -76,11 +76,8 @@ func webSocketRequest(chanID uint64, ch chan<- *model.RequestResults, i uint64, ) // 需要发送的数据 head := &pb.UserMessage{MainType: "user", SubType: "login"} - req := &pb.UserLoginReq{ - Account: "user001", - Sid: "dfmxf", - } - r.SendToClient(head, req) + + r.SendToClient(head, &pb.UserLoginReq{}) for { if b, len := r.MessageRsp("user", "login"); b { @@ -90,7 +87,7 @@ func webSocketRequest(chanID uint64, ch chan<- *model.RequestResults, i uint64, break } } - + r.Create() requestTime := uint64(helper.DiffNano(startTime)) requestResults := &model.RequestResults{ ID: "", diff --git a/stress/stress.go b/stress/stress.go index 88c2bfc16..5822ce0f3 100644 --- a/stress/stress.go +++ b/stress/stress.go @@ -70,10 +70,10 @@ func init() { func main() { runtime.GOMAXPROCS(cpuNumber) //go run .\main.go -c 10 -n 10 -u ws://10.0.5.101:7891/gateway - concurrency = 10 - totalNumber = 10 + concurrency = 100 + totalNumber = 100 debugStr = "false" - requestURL = "ws://10.0.0.9:7891/gateway" + requestURL = "ws://10.0.0.85: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")