forked from Mikubill/wsSocks
/
benchmark.go
66 lines (59 loc) · 1.27 KB
/
benchmark.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"fmt"
"github.com/gorilla/websocket"
"net/url"
"time"
)
type Benchmark struct {
Connections int
Block int
ServerAddr *url.URL
Dialer *websocket.Dialer
CreatedAt time.Time
}
func (client *Benchmark) Bench() (err error) {
for i := 0; i < client.Connections; i++ {
wsKeys = append(wsKeys, genRandBytes(wsAddrLen))
}
wsLen = client.Connections
mainServer = client.ServerAddr.String()
data := genRandBytes(client.Block)
for {
//log.Warn("initializing data package...")
c := &muxConn{
id: genRandBytes(connAddrLen),
ws: wsPool.getWs(),
}
_, err = c.bench(data)
if err != nil {
log.Warnf(err.Error())
}
_ = c.Close()
}
}
func benchStats() {
var ou, od int64
for {
time.Sleep(time.Second)
speedUp := uploaded - ou
speedDown := downloaded - od
log.Infof("stats: uploaded %s | %s/s, downloaded %s | %s/s",
ByteCountSI(uploaded), ByteCountSI(speedUp), ByteCountSI(downloaded), ByteCountSI(speedDown))
ou = uploaded
od = downloaded
}
}
func ByteCountSI(b int64) string {
const unit = 1000
if b < unit {
return fmt.Sprintf("%d B", b)
}
div, exp := int64(unit), 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %cB",
float64(b)/float64(div), "kMGTPE"[exp])
}