-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix unit test * Improve eventloop performance * update benchmarks
- Loading branch information
Showing
13 changed files
with
273 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#!/bin/bash | ||
|
||
set -e | ||
|
||
echo "" | ||
echo "--- BENCH websocket PING PONG START ---" | ||
echo "" | ||
|
||
cd $(dirname "${BASH_SOURCE[0]}") | ||
function cleanup() { | ||
echo "--- BENCH websocket PING PONG DONE ---" | ||
kill -9 $(jobs -rp) | ||
wait $(jobs -rp) 2>/dev/null | ||
} | ||
trap cleanup EXIT | ||
|
||
mkdir -p bin | ||
$(pkill -9 websocket-server || printf "") | ||
|
||
function gobench() { | ||
echo "--- $1 ---" | ||
if [ "$3" != "" ]; then | ||
go build -o $2 $3 | ||
fi | ||
$2 --port $4 --loops 8 & | ||
|
||
sleep 1 | ||
go run websocket/client/main.go -c 3000 -t 5 -m 2048 -a 127.0.0.1:$4 | ||
pkill -9 $2 || printf "" | ||
echo "--- DONE ---" | ||
echo "" | ||
} | ||
|
||
gobench "Gev-websocket" bin/websocket-server websocket/server.go 6000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"flag" | ||
"fmt" | ||
"math/rand" | ||
"time" | ||
|
||
"golang.org/x/net/websocket" | ||
) | ||
|
||
var ( | ||
addr = flag.String("a", "localhost:1833", "address") | ||
num = flag.Int("c", 1, "connection number") | ||
timeOut = flag.Int("t", 2, "timeout second") | ||
msgLen = flag.Int("m", 1024, "message length") | ||
|
||
msg []byte | ||
) | ||
|
||
func main() { | ||
flag.Parse() | ||
fmt.Printf("*** %d connections, %d seconds, %d byte packets ***\n", *num, *timeOut, *msgLen) | ||
|
||
msg = make([]byte, *msgLen) | ||
rand.Read(msg) | ||
startC := make(chan interface{}) | ||
closeC := make(chan interface{}) | ||
result := make(chan int64, *num) | ||
req := make(chan int64, *num) | ||
|
||
for i := 0; i < *num; i++ { | ||
go startWebSocketClient(startC, closeC, result, req) | ||
} | ||
|
||
// start | ||
close(startC) | ||
time.Sleep(time.Duration(*timeOut) * time.Second) | ||
// stop | ||
close(closeC) | ||
|
||
var totalMessagesRead, reqCount int64 | ||
for i := 0; i < *num; i++ { | ||
totalMessagesRead += <-result | ||
reqCount += <-req | ||
} | ||
|
||
fmt.Println(totalMessagesRead/int64(*timeOut*1024*1024), " MiB/s throughput") | ||
fmt.Println(reqCount/int64(*timeOut), " qps") | ||
} | ||
|
||
func startWebSocketClient(startC chan interface{}, closeC chan interface{}, result, req chan int64) { | ||
var count, reqCount int64 | ||
buf := make([]byte, 2*(*msgLen)) | ||
|
||
address := "ws://" + *addr | ||
c, err := websocket.Dial(address, "", address) | ||
if err != nil { | ||
panic(err) | ||
} | ||
c.MaxPayloadBytes = *msgLen * 2 | ||
<-startC | ||
|
||
if n, err := c.Write(msg); err != nil || n != len(msg) { | ||
panic(err) | ||
} | ||
|
||
for { | ||
select { | ||
case <-closeC: | ||
result <- count | ||
req <- reqCount | ||
c.Close() | ||
return | ||
default: | ||
n, err := c.Read(buf) | ||
if err != nil || n != len(msg) { | ||
fmt.Printf("read error %v %d", err, n) | ||
panic(errors.New("read error")) | ||
} | ||
if !bytes.Equal(msg, buf[:n]) { | ||
panic("mismatch") | ||
} | ||
|
||
count += int64(n) | ||
reqCount++ | ||
|
||
_, err = c.Write(msg) | ||
if err != nil { | ||
fmt.Println("Error to send message because of ", err.Error()) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"strconv" | ||
|
||
"github.com/Allenxuxu/gev/log" | ||
|
||
"github.com/Allenxuxu/gev" | ||
"github.com/Allenxuxu/gev/connection" | ||
"github.com/Allenxuxu/gev/plugins/websocket" | ||
"github.com/Allenxuxu/gev/plugins/websocket/ws" | ||
) | ||
|
||
type example struct { | ||
} | ||
|
||
// connection lifecycle | ||
// OnConnect() -> OnRequest() -> OnHeader() -> OnMessage() -> OnClose() | ||
|
||
func (s *example) OnConnect(c *connection.Connection) { | ||
//log.Println("OnConnect: ", c.PeerAddr()) | ||
} | ||
|
||
func (s *example) OnMessage(c *connection.Connection, data []byte) (messageType ws.MessageType, out []byte) { | ||
//log.Println("OnMessage: ", string(data)) | ||
|
||
messageType = ws.MessageBinary | ||
out = data | ||
|
||
return | ||
} | ||
|
||
func (s *example) OnClose(c *connection.Connection) { | ||
//log.Println("123 OnClose", c.PeerAddr()) | ||
} | ||
|
||
// NewWebSocketServer 创建 WebSocket Server | ||
func NewWebSocketServer(handler websocket.WSHandler, u *ws.Upgrader, opts ...gev.Option) (server *gev.Server, err error) { | ||
opts = append(opts, gev.Protocol(websocket.New(u))) | ||
return gev.NewServer(websocket.NewHandlerWrap(u, handler), opts...) | ||
} | ||
|
||
func main() { | ||
log.SetLevel(log.LevelDebug) | ||
var ( | ||
port int | ||
loops int | ||
) | ||
|
||
flag.IntVar(&port, "port", 1833, "server port") | ||
flag.IntVar(&loops, "loops", -1, "num loops") | ||
flag.Parse() | ||
|
||
handler := &example{} | ||
wsUpgrader := &ws.Upgrader{} | ||
//wsUpgrader.OnRequest = func(c *connection.Connection, uri []byte) error { | ||
// log.Println("OnRequest: ", string(uri)) | ||
// | ||
// return nil | ||
//} | ||
|
||
s, err := NewWebSocketServer(handler, wsUpgrader, | ||
gev.Network("tcp"), | ||
gev.Address(":"+strconv.Itoa(port)), | ||
gev.NumLoops(loops)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
s.Start() | ||
} |
Oops, something went wrong.