mirror of
https://github.com/kataras/iris.git
synced 2025-12-20 03:17:04 +00:00
add websocket client stress test, passed and update the vendors (this commit fixes the https://github.com/kataras/iris/issues/1178 and https://github.com/kataras/iris/issues/1173)
Former-commit-id: 74ccd8f4bf60a71f1eb0e34149a6f19de95a9148
This commit is contained in:
85
_examples/websocket/go-client-stress-test/client/main.go
Normal file
85
_examples/websocket/go-client-stress-test/client/main.go
Normal file
@@ -0,0 +1,85 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris/websocket"
|
||||
)
|
||||
|
||||
var (
|
||||
url = "ws://localhost:8080/socket"
|
||||
f *os.File
|
||||
)
|
||||
|
||||
const totalClients = 1200
|
||||
|
||||
func main() {
|
||||
var err error
|
||||
f, err = os.Open("./test.data")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
wg := new(sync.WaitGroup)
|
||||
for i := 0; i < totalClients/2; i++ {
|
||||
wg.Add(1)
|
||||
go connect(wg, 5*time.Second)
|
||||
}
|
||||
|
||||
for i := 0; i < totalClients/2; i++ {
|
||||
wg.Add(1)
|
||||
waitTime := time.Duration(rand.Intn(10)) * time.Millisecond
|
||||
time.Sleep(waitTime)
|
||||
go connect(wg, 10*time.Second+waitTime)
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
fmt.Println("ALL OK.")
|
||||
time.Sleep(5 * time.Second)
|
||||
}
|
||||
|
||||
func connect(wg *sync.WaitGroup, alive time.Duration) {
|
||||
|
||||
c, err := websocket.Dial(url, websocket.ConnectionConfig{})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
c.OnError(func(err error) {
|
||||
fmt.Printf("error: %v", err)
|
||||
})
|
||||
|
||||
disconnected := false
|
||||
c.OnDisconnect(func() {
|
||||
fmt.Printf("I am disconnected after [%s].\n", alive)
|
||||
disconnected = true
|
||||
})
|
||||
|
||||
c.On("chat", func(message string) {
|
||||
fmt.Printf("\n%s\n", message)
|
||||
})
|
||||
|
||||
go func() {
|
||||
time.Sleep(alive)
|
||||
if err := c.Disconnect(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
wg.Done()
|
||||
}()
|
||||
|
||||
scanner := bufio.NewScanner(f)
|
||||
for !disconnected {
|
||||
if !scanner.Scan() || scanner.Err() != nil {
|
||||
break
|
||||
}
|
||||
|
||||
c.Emit("chat", scanner.Text())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user