mirror of
https://github.com/kataras/iris.git
synced 2025-12-20 03:17:04 +00:00
improve client test, I think we are OK, both gorilla(websocket) and ws(websocket2) have the same API, it's time to combine them but first let's give a lower level of api available for users if they want to manage the routines by theirselves (i.e on unix they can use netpolls manually)
Former-commit-id: 3209a7490939bce913732c1375190b0771ba63ae
This commit is contained in:
@@ -2,8 +2,9 @@ package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"log"
|
||||
"math/rand"
|
||||
"net"
|
||||
"os"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
@@ -13,11 +14,11 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
url = "ws://localhost:8080/socket"
|
||||
url = "ws://localhost:8080"
|
||||
f *os.File
|
||||
)
|
||||
|
||||
const totalClients = 100000
|
||||
const totalClients = 16000 // max depends on the OS.
|
||||
|
||||
var connectionFailures uint64
|
||||
|
||||
@@ -41,6 +42,7 @@ func collectError(op string, err error) {
|
||||
}
|
||||
|
||||
func main() {
|
||||
log.Println("--------======Running tests...==========--------------")
|
||||
var err error
|
||||
f, err = os.Open("./test.data")
|
||||
if err != nil {
|
||||
@@ -67,27 +69,27 @@ func main() {
|
||||
wg.Add(1)
|
||||
waitTime := time.Duration(rand.Intn(10)) * time.Millisecond
|
||||
time.Sleep(waitTime)
|
||||
go connect(wg, 10*time.Second+waitTime)
|
||||
go connect(wg, 9*time.Second+waitTime)
|
||||
}
|
||||
|
||||
for i := 0; i < totalClients/4; i++ {
|
||||
wg.Add(1)
|
||||
waitTime := time.Duration(rand.Intn(20)) * time.Millisecond
|
||||
waitTime := time.Duration(rand.Intn(5)) * time.Millisecond
|
||||
time.Sleep(waitTime)
|
||||
go connect(wg, 25*time.Second+waitTime)
|
||||
go connect(wg, 14*time.Second+waitTime)
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
fmt.Println("--------================--------------")
|
||||
fmt.Printf("execution time [%s]", time.Since(start))
|
||||
fmt.Println()
|
||||
|
||||
log.Printf("execution time [%s]", time.Since(start))
|
||||
log.Println()
|
||||
|
||||
if connectionFailures > 0 {
|
||||
fmt.Printf("Finished with %d/%d connection failures. Please close the server-side manually.\n", connectionFailures, totalClients)
|
||||
log.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)
|
||||
log.Printf("Finished with %d connect errors:\n", n)
|
||||
var lastErr error
|
||||
var sameC int
|
||||
|
||||
@@ -96,36 +98,44 @@ func main() {
|
||||
if lastErr.Error() == err.Error() {
|
||||
sameC++
|
||||
continue
|
||||
} else {
|
||||
_, ok := lastErr.(*net.OpError)
|
||||
if ok {
|
||||
if _, ok = err.(*net.OpError); ok {
|
||||
sameC++
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if sameC > 0 {
|
||||
fmt.Printf("and %d more like this...\n", sameC)
|
||||
log.Printf("and %d more like this...\n", sameC)
|
||||
sameC = 0
|
||||
continue
|
||||
}
|
||||
|
||||
fmt.Printf("[%d] - %v\n", i+1, err)
|
||||
log.Printf("[%d] - %v\n", i+1, err)
|
||||
lastErr = err
|
||||
}
|
||||
}
|
||||
|
||||
if n := len(disconnectErrors); n > 0 {
|
||||
fmt.Printf("Finished with %d disconnect errors\n", n)
|
||||
log.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)
|
||||
log.Printf("[%d] - %v\n", i+1, err)
|
||||
}
|
||||
}
|
||||
|
||||
if connectionFailures == 0 && len(connectErrors) == 0 && len(disconnectErrors) == 0 {
|
||||
fmt.Println("ALL OK.")
|
||||
log.Println("ALL OK.")
|
||||
}
|
||||
|
||||
fmt.Println("--------================--------------")
|
||||
log.Println("--------================--------------")
|
||||
}
|
||||
|
||||
func connect(wg *sync.WaitGroup, alive time.Duration) {
|
||||
@@ -138,17 +148,17 @@ func connect(wg *sync.WaitGroup, alive time.Duration) {
|
||||
}
|
||||
|
||||
c.OnError(func(err error) {
|
||||
fmt.Printf("error: %v", err)
|
||||
log.Printf("error: %v", err)
|
||||
})
|
||||
|
||||
disconnected := false
|
||||
c.OnDisconnect(func() {
|
||||
fmt.Printf("I am disconnected after [%s].\n", alive)
|
||||
// log.Printf("I am disconnected after [%s].\n", alive)
|
||||
disconnected = true
|
||||
})
|
||||
|
||||
c.On("chat", func(message string) {
|
||||
fmt.Printf("\n%s\n", message)
|
||||
// log.Printf("\n%s\n", message)
|
||||
})
|
||||
|
||||
go func() {
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"runtime"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
@@ -10,38 +11,84 @@ import (
|
||||
"github.com/kataras/iris/websocket2"
|
||||
)
|
||||
|
||||
const totalClients = 100000
|
||||
const totalClients = 16000 // max depends on the OS.
|
||||
const http = true
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
// websocket.Config{PingPeriod: ((60 * time.Second) * 9) / 10}
|
||||
ws := websocket.New(websocket.Config{})
|
||||
ws.OnConnection(handleConnection)
|
||||
app.Get("/socket", ws.Handler())
|
||||
|
||||
// websocket.Config{PingPeriod: ((60 * time.Second) * 9) / 10}
|
||||
|
||||
go func() {
|
||||
t := time.NewTicker(2 * time.Second)
|
||||
dur := 8 * time.Second
|
||||
if totalClients >= 64000 {
|
||||
// if more than 64000 then let's no check every 8 seconds, let's do it every 24 seconds,
|
||||
// just for simplicity, either way works.
|
||||
dur = 24 * time.Second
|
||||
}
|
||||
t := time.NewTicker(dur)
|
||||
defer t.Stop()
|
||||
defer os.Exit(0)
|
||||
defer runtime.Goexit()
|
||||
|
||||
var started bool
|
||||
for {
|
||||
<-t.C
|
||||
|
||||
conns := ws.GetConnections()
|
||||
for _, conn := range conns {
|
||||
// fmt.Println(conn.ID())
|
||||
// Do nothing.
|
||||
_ = conn
|
||||
n := ws.GetTotalConnections()
|
||||
if n > 0 {
|
||||
started = true
|
||||
}
|
||||
|
||||
if atomic.LoadUint64(&count) == totalClients {
|
||||
fmt.Println("ALL CLIENTS DISCONNECTED SUCCESSFULLY.")
|
||||
t.Stop()
|
||||
os.Exit(0)
|
||||
return
|
||||
if started {
|
||||
totalConnected := atomic.LoadUint64(&count)
|
||||
|
||||
if totalConnected == totalClients {
|
||||
if n != 0 {
|
||||
log.Println("ALL CLIENTS DISCONNECTED BUT LEFTOVERS ON CONNECTIONS LIST.")
|
||||
} else {
|
||||
log.Println("ALL CLIENTS DISCONNECTED SUCCESSFULLY.")
|
||||
}
|
||||
return
|
||||
} else if n == 0 {
|
||||
log.Printf("%d/%d CLIENTS WERE NOT CONNECTED AT ALL. CHECK YOUR OS NET SETTINGS. ALL OTHER CONNECTED CLIENTS DISCONNECTED SUCCESSFULLY.\n",
|
||||
totalClients-totalConnected, totalClients)
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
app.Run(iris.Addr(":8080"))
|
||||
if http {
|
||||
app := iris.New()
|
||||
app.Get("/", ws.Handler())
|
||||
app.Run(iris.Addr(":8080"))
|
||||
return
|
||||
}
|
||||
|
||||
// ln, err := net.Listen("tcp", ":8080")
|
||||
// if err != nil {
|
||||
// panic(err)
|
||||
// }
|
||||
|
||||
// defer ln.Close()
|
||||
// for {
|
||||
// conn, err := ln.Accept()
|
||||
// if err != nil {
|
||||
// panic(err)
|
||||
// }
|
||||
|
||||
// go func() {
|
||||
// err = ws.HandleConn(conn)
|
||||
// if err != nil {
|
||||
// panic(err)
|
||||
// }
|
||||
// }()
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
func handleConnection(c websocket.Connection) {
|
||||
@@ -56,9 +103,9 @@ var count uint64
|
||||
|
||||
func handleDisconnect(c websocket.Connection) {
|
||||
atomic.AddUint64(&count, 1)
|
||||
fmt.Printf("client [%s] disconnected!\n", c.ID())
|
||||
// log.Printf("client [%s] disconnected!\n", c.ID())
|
||||
}
|
||||
|
||||
func handleErr(c websocket.Connection, err error) {
|
||||
fmt.Printf("client [%s] errored: %v\n", c.ID(), err)
|
||||
log.Printf("client [%s] errored: %v\n", c.ID(), err)
|
||||
}
|
||||
|
||||
@@ -21,8 +21,7 @@ $ go run main.go
|
||||
>> hi!
|
||||
*/
|
||||
func main() {
|
||||
// `websocket.DialContext` is also available.
|
||||
c, err := websocket.Dial(url, websocket.ConnectionConfig{})
|
||||
c, err := websocket.Dial(nil, url, websocket.ConnectionConfig{})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user