大家好,我是彬哥,本节给大家讲下LollipopGo游戏服务器robot相关,抛砖引玉了,主要是针对Go语言游戏服务器压力测试的前期准备。
package main
import (
"fmt"
"math/rand"
"strconv"
"Proto"
"Proto/Proto2"
"encoding/base64"
"encoding/json"
"flag"
"os"
"strings"
"sync"
"time"
"code.google.com/p/go.net/websocket"
)
var GW sync.WaitGroup
var Scount int
func main1() {
fmt.Println(os.Args[1:])
fmt.Println(flag.Arg(1))
fmt.Println("Entry server count:", os.Args[1]) // 人数
t1 := time.Now()
count := os.Args[1]
loops, _ := strconv.Atoi(os.Args[2]) // 并发次数
int1, _ := strconv.Atoi(count)
GW.Add(int1)
Scount = int1 * loops
for i := 0; i < int1; i++ {
go GoroutineFunc(loops)
}
GW.Wait()
elapsed := time.Since(t1)
fmt.Println("Total count:", int1*loops)
fmt.Println("Success count:", Scount)
fmt.Println("Cysle TPS:", float64(int1*loops)/elapsed.Seconds())
fmt.Println("Taken Time(s) :", elapsed)
fmt.Println("Average Latency time(ms):", elapsed.Seconds()*1000/(float64(int1*loops)))
//-------------------------------------------------------------------
}
func GoroutineFunc(loops int) {
fmt.Println("Robot 客户端模拟!")
url := "ws://" + *addr + "/GolangLtd"
ws, err := websocket.Dial(url, "", "test://golang/")
if err != nil {
fmt.Println("err:", err.Error())
return
}
// 数据的发送
for i := 0; i < loops; i++ {
Send(ws, "HeartBeat")
var content string
err := websocket.Message.Receive(ws, &content)
if err != nil {
fmt.Println(err.Error())
return
}
// decode
//fmt.Println(strings.Trim("", "\""))
//fmt.Println(content)
content = strings.Replace(content, "\"", "", -1)
contentstr, errr := base64Decode([]byte(content))
if errr != nil {
fmt.Println(errr)
}
// 解析数据
//fmt.Println(string(contentstr))
_ = contentstr
}
GW.Done()
}
/*
robot
1 模拟玩家的正常的“操作”,例如 行走 跳跃 开枪等等
2 做服务器的性能的测试,例如 并发量 内存 CPU 等等
3 压力测试
注意点:
1 模拟 ---> 多线程模拟 goroutine --- server !!!
首先:
1 net 网络使用websocket 进行连接
2 send 如何发送 ??
*/
var addr = flag.String("addr", "127.0.0.1:8888", "http service address")
var connbak *websocket.Conn
func main() {
fmt.Println("Robot 客户端模拟!")
url := "ws://" + *addr + "/GolangLtd"
ws, err := websocket.Dial(url, "", "test://golang/")
if err != nil {
fmt.Println("err:", err.Error())
return
}
// 数据的发送
for i := 0; i < 100; i++ {
// go Send(ws, "Login")
}
go Send(ws, "HeartBeat")
connbak = new(websocket.Conn)
connbak = ws
go Timer(ws)
// 数据处理
for {
var content string
err := websocket.Message.Receive(ws, &content)
if err != nil {
fmt.Println(err.Error())
return
}
// decode
fmt.Println(strings.Trim("", "\""))
fmt.Println(content)
content = strings.Replace(content, "\"", "", -1)
contentstr, errr := base64Decode([]byte(content))
if errr != nil {
fmt.Println(errr)
}
// 解析数据
fmt.Println(string(contentstr))
}
}
func base64Decode(src []byte) ([]byte, error) {
return base64.StdEncoding.DecodeString(string(src))
}
func Send(conn *websocket.Conn, Itype string) {
if Itype == "Login" {
// 1 组装
data := &Proto2.C2S_PlayerLogin{
Protocol: Proto.GameData_Proto,
Protocol2: Proto2.C2S_PlayerLoginProto2,
Itype: 1,
StrLoginName: "Golangltd" + Rand_LoginName(),
StrLoginPW: "1234556",
StrLoginEmail: "123455@qq.com",
}
PlayerSendToServer(conn, data)
} else if Itype == "HeartBeat" { // 心跳
data := &Proto2.Net_HeartBeat{
Protocol: Proto.GameNet_Proto,
Protocol2: Proto2.Net_HeartBeatProto2,
OpenID: "22323",
}
PlayerSendToServer(conn, data)
}
return
}
func PlayerSendToServer(conn *websocket.Conn, data interface{}) {
jsons, err := json.Marshal(data)
if err != nil {
fmt.Println("err:", err.Error())
return
}
///fmt.Println("jsons:", string(jsons))
errq := websocket.Message.Send(conn, jsons)
if errq != nil {
fmt.Println(errq)
}
return
}
func Rand_LoginName() string {
idata := rand.Intn(100000)
// 去重?
strdata := strconv.Itoa(idata)
return strdata
}
社区交流群:221273219
Golang语言社区论坛 :
www.Golang.Ltd
LollipopGo游戏服务器地址:
https://github.com/Golangltd/LollipopGo
社区视频课程课件GIT地址:
https://github.com/Golangltd/codeclass
有疑问加站长微信联系(非本文作者)