This commit is contained in:
meixiongfeng 2022-12-01 15:41:50 +08:00
parent 2c12e6ae26
commit f4375f17b5
4 changed files with 73 additions and 14 deletions

56
stress/robot/randomStr.go Normal file
View File

@ -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))
// }

View File

@ -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
}

View File

@ -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: "",

View File

@ -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")