创角
This commit is contained in:
parent
2c12e6ae26
commit
f4375f17b5
56
stress/robot/randomStr.go
Normal file
56
stress/robot/randomStr.go
Normal 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))
|
||||||
|
// }
|
@ -57,11 +57,10 @@ func NewRobot(url string) *Robot {
|
|||||||
zlog.Fatalf("websocket conn err:%v", err)
|
zlog.Fatalf("websocket conn err:%v", err)
|
||||||
}
|
}
|
||||||
r := &Robot{
|
r := &Robot{
|
||||||
ws: ws,
|
ws: ws,
|
||||||
account: "user001",
|
sid: "dfmxf",
|
||||||
sid: "dfmxf",
|
|
||||||
}
|
}
|
||||||
|
r.account = r.RandStr1(13)
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -191,13 +190,20 @@ func (r *Robot) MessageRsp(mainType, subType string) (bool, int64) {
|
|||||||
}
|
}
|
||||||
return false, byteLen
|
return false, byteLen
|
||||||
//fmt.Printf("接收消息=====resp:%v\n", resp.Chat)
|
//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{}
|
resp := &pb.UserLoginResp{}
|
||||||
if !comm.ProtoUnmarshal(msg, resp) {
|
if !comm.ProtoUnmarshal(msg, resp) {
|
||||||
return false, byteLen
|
return false, byteLen
|
||||||
}
|
}
|
||||||
//fmt.Printf("接收消息=====loginData:%v,userExpand:%v\n", resp.Data, resp.Ex)
|
//fmt.Printf("接收消息=====loginData:%v,userExpand:%v\n", resp.Data, resp.Ex)
|
||||||
return true, byteLen
|
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
|
return false, byteLen
|
||||||
}
|
}
|
||||||
|
@ -76,11 +76,8 @@ func webSocketRequest(chanID uint64, ch chan<- *model.RequestResults, i uint64,
|
|||||||
)
|
)
|
||||||
// 需要发送的数据
|
// 需要发送的数据
|
||||||
head := &pb.UserMessage{MainType: "user", SubType: "login"}
|
head := &pb.UserMessage{MainType: "user", SubType: "login"}
|
||||||
req := &pb.UserLoginReq{
|
|
||||||
Account: "user001",
|
r.SendToClient(head, &pb.UserLoginReq{})
|
||||||
Sid: "dfmxf",
|
|
||||||
}
|
|
||||||
r.SendToClient(head, req)
|
|
||||||
for {
|
for {
|
||||||
|
|
||||||
if b, len := r.MessageRsp("user", "login"); b {
|
if b, len := r.MessageRsp("user", "login"); b {
|
||||||
@ -90,7 +87,7 @@ func webSocketRequest(chanID uint64, ch chan<- *model.RequestResults, i uint64,
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
r.Create()
|
||||||
requestTime := uint64(helper.DiffNano(startTime))
|
requestTime := uint64(helper.DiffNano(startTime))
|
||||||
requestResults := &model.RequestResults{
|
requestResults := &model.RequestResults{
|
||||||
ID: "",
|
ID: "",
|
||||||
|
@ -70,10 +70,10 @@ func init() {
|
|||||||
func main() {
|
func main() {
|
||||||
runtime.GOMAXPROCS(cpuNumber)
|
runtime.GOMAXPROCS(cpuNumber)
|
||||||
//go run .\main.go -c 10 -n 10 -u ws://10.0.5.101:7891/gateway
|
//go run .\main.go -c 10 -n 10 -u ws://10.0.5.101:7891/gateway
|
||||||
concurrency = 10
|
concurrency = 100
|
||||||
totalNumber = 10
|
totalNumber = 100
|
||||||
debugStr = "false"
|
debugStr = "false"
|
||||||
requestURL = "ws://10.0.0.9:7891/gateway"
|
requestURL = "ws://10.0.0.85:7891/gateway"
|
||||||
verify = "pb"
|
verify = "pb"
|
||||||
if concurrency == 0 || totalNumber == 0 || (requestURL == "" && path == "") {
|
if concurrency == 0 || totalNumber == 0 || (requestURL == "" && path == "") {
|
||||||
fmt.Printf("示例: go run main.go -c 1 -n 1 -u https://www.baidu.com/ \n")
|
fmt.Printf("示例: go run main.go -c 1 -n 1 -u https://www.baidu.com/ \n")
|
||||||
|
Loading…
Reference in New Issue
Block a user