mirror of
https://github.com/kataras/iris.git
synced 2025-12-28 07:17:06 +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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,9 +11,3 @@ Far curiosity incommode now led smallness allowance. Favour bed assure son thing
|
||||
Windows talking painted pasture yet its express parties use. Sure last upon he same as knew next. Of believed or diverted no rejoiced. End friendship sufficient assistance can prosperous met. As game he show it park do. Was has unknown few certain ten promise. No finished my an likewise cheerful packages we. For assurance concluded son something depending discourse see led collected. Packages oh no denoting my advanced humoured. Pressed be so thought natural.
|
||||
|
||||
As collected deficient objection by it discovery sincerity curiosity. Quiet decay who round three world whole has mrs man. Built the china there tried jokes which gay why. Assure in adieus wicket it is. But spoke round point and one joy. Offending her moonlight men sweetness see unwilling. Often of it tears whole oh balls share an.
|
||||
|
||||
Lose eyes get fat shew. Winter can indeed letter oppose way change tended now. So is improve my charmed picture exposed adapted demands. Received had end produced prepared diverted strictly off man branched. Known ye money so large decay voice there to. Preserved be mr cordially incommode as an. He doors quick child an point at. Had share vexed front least style off why him.
|
||||
|
||||
He unaffected sympathize discovered at no am conviction principles. Girl ham very how yet hill four show. Meet lain on he only size. Branched learning so subjects mistress do appetite jennings be in. Esteems up lasting no village morning do offices. Settled wishing ability musical may another set age. Diminution my apartments he attachment is entreaties announcing estimating. And total least her two whose great has which. Neat pain form eat sent sex good week. Led instrument sentiments she simplicity.
|
||||
|
||||
Months on ye at by esteem desire warmth former. Sure that that way gave any fond now. His boy middleton sir nor engrossed affection excellent. Dissimilar compliment cultivated preference eat sufficient may. Well next door soon we mr he four. Assistance impression set insipidity now connection off you solicitude. Under as seems we me stuff those style at. Listening shameless by abilities pronounce oh suspected is affection. Next it draw in draw much bred.
|
||||
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
"github.com/kataras/iris/websocket2"
|
||||
)
|
||||
|
||||
const totalClients = 1200
|
||||
const totalClients = 100000
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
Reference in New Issue
Block a user