Skip to content

Commit 9fb2cd4

Browse files
authored
feat: update BroadcastMessage to avoid alloc (#40)
* add performance test * print test system info * update BroadcastMessage
1 parent c2b5f15 commit 9fb2cd4

File tree

15 files changed

+260
-118
lines changed

15 files changed

+260
-118
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
coverage.out
1+
coverage.out
2+
mem.pprof
3+
cpu.pprof
4+
trace.out

agent/agent.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/metagogs/gogs/acceptor"
1111
"github.com/metagogs/gogs/message"
12+
"github.com/metagogs/gogs/packet"
1213
"github.com/metagogs/gogs/proto"
1314
"github.com/metagogs/gogs/session"
1415
"github.com/metagogs/gogs/utils/bytebuffer"
@@ -183,6 +184,13 @@ func (a *Agent) Send(in interface{}, name ...string) error {
183184
return nil
184185
}
185186

187+
func (a *Agent) SendPacket(data *packet.Packet) {
188+
select {
189+
case a.chSend <- data.ToData():
190+
case <-a.chDie:
191+
}
192+
}
193+
186194
func (a *Agent) SendData(data []byte) {
187195
select {
188196
case a.chSendByte <- data:

app.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ type App struct {
4242
LatencyServer *latency.LatencyServer // 延时服务管理
4343
GroupServer *group.GroupServer // 组管理
4444

45-
helper *appHelper //一些便捷操作
4645
Config *config.Config
4746
}
4847

@@ -71,11 +70,6 @@ func NewApp(config *config.Config) *App {
7170
MessageServer: appBuidler.messageServer,
7271
webServer: appBuidler.webServer,
7372
}
74-
message.DefaultMessageServer = app.MessageServer
75-
76-
//初始化便捷服务
77-
helper := newAppHelper(app)
78-
app.helper = helper
7973

8074
system.RegisterSystemComponent(app.MessageServer, NewNetworkComponent(app))
8175

@@ -121,10 +115,6 @@ func (app *App) RegisterWebHandler(port int, f func(gin *gin.Engine)) {
121115
app.webServer.RegisterWebHandler(port, f)
122116
}
123117

124-
func (app *App) Helper() *appHelper {
125-
return app.helper
126-
}
127-
128118
func (app *App) Start() {
129119
deploymentFlag := flag.Bool("deployment", false, "deployment mode")
130120
deploymentScv := flag.Bool("svc", false, "use the k8s svc")

builder.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func NewBuilder(config *config.Config) *builder {
4040
func newSessionPool(config *config.Config) session.SessionPool {
4141
// session池管理
4242
appSessionPool := session.NewSessionPool(config)
43-
session.DefaultSessionPool = appSessionPool
43+
DefaultSessionPool = appSessionPool
4444
return appSessionPool
4545
}
4646

@@ -63,7 +63,9 @@ func newAdminServer(config *config.Config, sessionPool session.SessionPool, code
6363
}
6464

6565
func newMessageServer(config *config.Config, codecHelper *codec.CodecHelper, dispatchServer *dispatch.DispatchServer) *message.MessageServer {
66-
return message.NewMessageServer(codecHelper, dispatchServer)
66+
messageServer := message.NewMessageServer(codecHelper, dispatchServer)
67+
DefaultMessageServer = messageServer
68+
return messageServer
6769
}
6870

6971
func newWebServer(config *config.Config) *webserver.WebServer {

e2e/e2e_test.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@ import (
55
"time"
66

77
"github.com/gorilla/websocket"
8+
"github.com/metagogs/gogs"
89
"github.com/metagogs/gogs/config"
910
"github.com/metagogs/gogs/e2e/testdata"
1011
"github.com/metagogs/gogs/e2e/testdata/fakeinternal/logic/baseworld"
1112
"github.com/metagogs/gogs/e2e/testdata/game"
1213
"github.com/metagogs/gogs/global"
13-
"github.com/metagogs/gogs/session"
1414
"github.com/stretchr/testify/assert"
1515
)
1616

1717
var (
1818
defaultConfig = config.NewConfig("default.yaml")
19-
testClient *client
20-
testClient2 *client
19+
testClient *TestClient
20+
testClient2 *TestClient
2121
uid string
2222
uid2 string
2323
)
@@ -38,10 +38,10 @@ func TestSendConnectWS(t *testing.T) {
3838
// wait for started
3939
<-started
4040
t.Log("websocket testing")
41-
clients := []*client{}
41+
clients := []*TestClient{}
4242
// try to make 10 websocket client
4343
for i := 0; i < 10; i++ {
44-
client, err := newWSClinet("ws://127.0.0.1:8888/base")
44+
client, err := NewWSClinet("ws://127.0.0.1:8888/base")
4545
assert.Nil(t, err)
4646
clients = append(clients, client)
4747
defer client.Close()
@@ -73,22 +73,22 @@ func TestSendConnectWS(t *testing.T) {
7373
var err error
7474
t.Log("websocket message testing")
7575
t.Log("user1 login")
76-
testClient, err = newWSClinet("ws://127.0.0.1:8888/base")
76+
testClient, err = NewWSClinet("ws://127.0.0.1:8888/base")
7777
assert.Nil(t, err)
7878
go testClient.Start(t)
7979

8080
<-time.After(1 * time.Second)
8181

8282
// the session pool should only have one session
83-
sessions := session.ListSessions()
83+
sessions := gogs.ListSessions()
8484
assert.Equal(t, 1, len(sessions))
8585

8686
//test send data
8787
sessions[0].SendData([]byte("hello world"))
8888
select {
8989
case <-time.After(1 * time.Second):
9090
t.Fatal("server get the data timeout")
91-
case msg := <-testClient.datas:
91+
case msg := <-testClient.Datas:
9292
assert.Equal(t, "hello world", string(msg))
9393
}
9494

@@ -99,14 +99,14 @@ func TestSendConnectWS(t *testing.T) {
9999
joinWorld(t, testClient)
100100

101101
t.Log("user2 login")
102-
testClient2, err = newWSClinet("ws://127.0.0.1:8888/base")
102+
testClient2, err = NewWSClinet("ws://127.0.0.1:8888/base")
103103
assert.Nil(t, err)
104104
go testClient2.Start(t)
105105

106106
<-time.After(1 * time.Second)
107107

108108
// with the two users logined, we should have to sessions
109-
sessions = session.ListSessions()
109+
sessions = gogs.ListSessions()
110110
assert.Equal(t, 2, len(sessions))
111111

112112
uid2 = userLogin(t, "e2e2")
@@ -121,7 +121,7 @@ func TestSendConnectWS(t *testing.T) {
121121
select {
122122
case <-time.After(1 * time.Second):
123123
t.Fatal("client get the JoinWorldNotify message timeout")
124-
case msg := <-testClient.datas:
124+
case msg := <-testClient.Datas:
125125
t.Log("client get the JoinWorldNotify message")
126126
p, err := testdata.TestApp.MessageServer.DecodeMessage(msg)
127127
assert.Nil(t, err)
@@ -147,7 +147,7 @@ func TestSendConnectWS(t *testing.T) {
147147
select {
148148
case <-time.After(1 * time.Second):
149149
t.Fatal("client get t he UpdateUserInWorld message timeout")
150-
case msg := <-testClient2.datas:
150+
case msg := <-testClient2.Datas:
151151
t.Log("client get the UpdateUserInWorld message")
152152
p, err := testdata.TestApp.MessageServer.DecodeMessage(msg)
153153
assert.Nil(t, err)
@@ -165,7 +165,7 @@ func TestSendConnectWS(t *testing.T) {
165165

166166
// bindUser when we create a client, we should send the BindUser message to th server
167167
// to bind the user to the connection. The servet should send the BindUserSuccess message to the client
168-
func bindUser(t *testing.T, sendClient *client, id string) {
168+
func bindUser(t *testing.T, sendClient *TestClient, id string) {
169169
t.Helper()
170170
t.Log("client send BindUser message")
171171
err := sendClient.WriteMessage(websocket.BinaryMessage, encodeMessage(t, &game.BindUser{
@@ -183,7 +183,7 @@ func bindUser(t *testing.T, sendClient *client, id string) {
183183
select {
184184
case <-time.After(1 * time.Second):
185185
t.Fatal("client get the BindSuccess message timeout")
186-
case msg := <-sendClient.datas:
186+
case msg := <-sendClient.Datas:
187187
t.Log("client get the BindSuccess message success")
188188
p, err := testdata.TestApp.MessageServer.DecodeMessage(msg)
189189
assert.Nil(t, err)
@@ -194,7 +194,7 @@ func bindUser(t *testing.T, sendClient *client, id string) {
194194

195195
// joinWorld user can send the JoinWorld message to the server to join the world
196196
// the server should send the JoinWorldSuccess message to the client
197-
func joinWorld(t *testing.T, sendClient *client) {
197+
func joinWorld(t *testing.T, sendClient *TestClient) {
198198
t.Helper()
199199
t.Log("client send JoinWorld message")
200200
err := sendClient.WriteMessage(websocket.BinaryMessage, encodeMessage(t, &game.JoinWorld{
@@ -211,7 +211,7 @@ func joinWorld(t *testing.T, sendClient *client) {
211211
select {
212212
case <-time.After(1 * time.Second):
213213
t.Fatal("client get the JoinWorldSuccess message timeout")
214-
case msg := <-sendClient.datas:
214+
case msg := <-sendClient.Datas:
215215
t.Log("client get the JoinWorldSuccess message success")
216216
p, err := testdata.TestApp.MessageServer.DecodeMessage(msg)
217217
assert.Nil(t, err)

e2e/helper.go

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,50 +16,59 @@ import (
1616
"github.com/tidwall/gjson"
1717
)
1818

19-
func startServer(t *testing.T, config *config.Config) (context.CancelFunc, chan int) { //nolint
19+
func startServer(t *testing.T, config *config.Config) (context.CancelFunc, chan struct{}) { //nolint
2020
defer func() {
2121
if err := recover(); err != nil {
2222
t.Fatal("start error", err)
2323
}
2424
}()
25-
startTest := make(chan int)
25+
startTest := make(chan struct{})
2626
config.StaredCallback = func() {
27-
start := time.After(1 * time.Second)
28-
<-start
29-
startTest <- 1
27+
<-time.After(1 * time.Second)
28+
startTest <- struct{}{}
3029
}
3130
t.Helper()
3231
ctx, cancel := context.WithCancel(context.Background())
3332
go testdata.StartServer(ctx, config)
3433
return cancel, startTest
3534
}
3635

37-
type client struct {
36+
type TestClient struct {
3837
*websocket.Conn
39-
datas chan []byte
38+
Datas chan []byte
4039
}
4140

42-
func (c *client) Start(t *testing.T) {
41+
func (c *TestClient) Start(t *testing.T) {
4342
t.Helper()
4443
for {
4544
_, data, err := c.ReadMessage()
4645
if err != nil {
4746
break
4847
}
49-
c.datas <- data
48+
c.Datas <- data
49+
50+
}
51+
}
52+
func (c *TestClient) Start2() {
53+
for {
54+
_, data, err := c.ReadMessage()
55+
if err != nil {
56+
break
57+
}
58+
c.Datas <- data
5059

5160
}
5261
}
5362

54-
func newWSClinet(address string) (*client, error) { //nolint
63+
func NewWSClinet(address string) (*TestClient, error) { //nolint
5564
conn, _, err := websocket.DefaultDialer.Dial(address, nil) //nolint
5665
if err != nil {
5766
return nil, err
5867
}
5968

60-
return &client{
69+
return &TestClient{
6170
Conn: conn,
62-
datas: make(chan []byte),
71+
Datas: make(chan []byte),
6372
}, nil
6473

6574
}

0 commit comments

Comments
 (0)