mirror of
https://github.com/kataras/iris.git
synced 2025-12-22 20:37:05 +00:00
websocket: from 1k to 100k on a simple raspeberry pi 3 model b by using a bit lower level of the new ws lib api and restore the previous sync.Map for server's live connections, relative: https://github.com/kataras/iris/issues/1178
Former-commit-id: 40da148afb66a42d47285efce324269d66ed3b0e
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"math/rand"
|
||||
"os"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris/websocket2"
|
||||
@@ -16,7 +17,28 @@ var (
|
||||
f *os.File
|
||||
)
|
||||
|
||||
const totalClients = 1200
|
||||
const totalClients = 100000
|
||||
|
||||
var connectionFailures uint64
|
||||
|
||||
var (
|
||||
disconnectErrors []error
|
||||
connectErrors []error
|
||||
errMu sync.Mutex
|
||||
)
|
||||
|
||||
func collectError(op string, err error) {
|
||||
errMu.Lock()
|
||||
defer errMu.Unlock()
|
||||
|
||||
switch op {
|
||||
case "disconnect":
|
||||
disconnectErrors = append(disconnectErrors, err)
|
||||
case "connect":
|
||||
connectErrors = append(connectErrors, err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func main() {
|
||||
var err error
|
||||
@@ -26,29 +48,93 @@ func main() {
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
start := time.Now()
|
||||
|
||||
wg := new(sync.WaitGroup)
|
||||
for i := 0; i < totalClients/2; i++ {
|
||||
for i := 0; i < totalClients/4; i++ {
|
||||
wg.Add(1)
|
||||
go connect(wg, 5*time.Second)
|
||||
}
|
||||
|
||||
for i := 0; i < totalClients/2; i++ {
|
||||
for i := 0; i < totalClients/4; i++ {
|
||||
wg.Add(1)
|
||||
waitTime := time.Duration(rand.Intn(5)) * time.Millisecond
|
||||
time.Sleep(waitTime)
|
||||
go connect(wg, 7*time.Second+waitTime)
|
||||
}
|
||||
|
||||
for i := 0; i < totalClients/4; i++ {
|
||||
wg.Add(1)
|
||||
waitTime := time.Duration(rand.Intn(10)) * time.Millisecond
|
||||
time.Sleep(waitTime)
|
||||
go connect(wg, 10*time.Second+waitTime)
|
||||
}
|
||||
|
||||
for i := 0; i < totalClients/4; i++ {
|
||||
wg.Add(1)
|
||||
waitTime := time.Duration(rand.Intn(20)) * time.Millisecond
|
||||
time.Sleep(waitTime)
|
||||
go connect(wg, 25*time.Second+waitTime)
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
fmt.Println("ALL OK.")
|
||||
time.Sleep(5 * time.Second)
|
||||
fmt.Println("--------================--------------")
|
||||
fmt.Printf("execution time [%s]", time.Since(start))
|
||||
fmt.Println()
|
||||
|
||||
if connectionFailures > 0 {
|
||||
fmt.Printf("Finished with %d/%d connection failures. Please close the server-side manually.\n", connectionFailures, totalClients)
|
||||
}
|
||||
|
||||
if n := len(connectErrors); n > 0 {
|
||||
fmt.Printf("Finished with %d connect errors:\n", n)
|
||||
var lastErr error
|
||||
var sameC int
|
||||
|
||||
for i, err := range connectErrors {
|
||||
if lastErr != nil {
|
||||
if lastErr.Error() == err.Error() {
|
||||
sameC++
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
if sameC > 0 {
|
||||
fmt.Printf("and %d more like this...\n", sameC)
|
||||
sameC = 0
|
||||
continue
|
||||
}
|
||||
|
||||
fmt.Printf("[%d] - %v\n", i+1, err)
|
||||
lastErr = err
|
||||
}
|
||||
}
|
||||
|
||||
if n := len(disconnectErrors); n > 0 {
|
||||
fmt.Printf("Finished with %d disconnect errors\n", n)
|
||||
for i, err := range disconnectErrors {
|
||||
if err == websocket.ErrAlreadyDisconnected {
|
||||
continue
|
||||
}
|
||||
|
||||
fmt.Printf("[%d] - %v\n", i+1, err)
|
||||
}
|
||||
}
|
||||
|
||||
if connectionFailures == 0 && len(connectErrors) == 0 && len(disconnectErrors) == 0 {
|
||||
fmt.Println("ALL OK.")
|
||||
}
|
||||
|
||||
fmt.Println("--------================--------------")
|
||||
}
|
||||
|
||||
func connect(wg *sync.WaitGroup, alive time.Duration) {
|
||||
|
||||
c, err := websocket.Dial(nil, url, websocket.ConnectionConfig{})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
atomic.AddUint64(&connectionFailures, 1)
|
||||
collectError("connect", err)
|
||||
wg.Done()
|
||||
return
|
||||
}
|
||||
|
||||
c.OnError(func(err error) {
|
||||
@@ -68,7 +154,7 @@ func connect(wg *sync.WaitGroup, alive time.Duration) {
|
||||
go func() {
|
||||
time.Sleep(alive)
|
||||
if err := c.Disconnect(); err != nil {
|
||||
panic(err)
|
||||
collectError("disconnect", err)
|
||||
}
|
||||
|
||||
wg.Done()
|
||||
@@ -80,6 +166,8 @@ func connect(wg *sync.WaitGroup, alive time.Duration) {
|
||||
break
|
||||
}
|
||||
|
||||
c.Emit("chat", scanner.Text())
|
||||
if text := scanner.Text(); len(text) > 1 {
|
||||
c.Emit("chat", text)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user