-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
163 lines (145 loc) · 3.54 KB
/
main.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"flag"
"fmt"
"net"
"strings"
"time"
tea "github.com/charmbracelet/bubbletea"
)
// Command line flags.
var (
listenAddr string
bufSize uint
)
// A single client's benchmarking data.
type bench struct {
bytesPerSec int
remoteAddr string
connected bool
}
// Bubbletea model.
type model struct {
benches []bench
}
func (m model) Init() tea.Cmd { return nil }
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q", "esc": // Quit.
return m, tea.Quit
case "h": // Do not display disconnected clients.
m.benches = delDeadBenches(m.benches)
return m, nil
}
case bench: // Add a new bench, or update if already exists.
found := -1
for i, v := range m.benches {
if msg.remoteAddr == v.remoteAddr {
found = i // Already exits, save its index.
}
}
if found >= 0 { // Exists, replace.
m.benches[found] = msg
} else { // Such a client not found, append.
m.benches = append(m.benches, msg)
}
return m, nil
}
return m, nil
}
func (m model) View() string {
var status strings.Builder
// Status bar.
status.WriteString(fmt.Sprintf("[ Listening on %s | Chunk size %d ]\n\n",
listenAddr, bufSize))
// Clients...
for _, v := range m.benches {
if !v.connected {
status.WriteString("X ")
}
rateMiBPerSec := float64(v.bytesPerSec) / 1024.0 / 1024.0
status.WriteString(fmt.Sprintf("->%s %d_B/s %.2f_MiB/s\n",
v.remoteAddr, v.bytesPerSec, rateMiBPerSec))
}
// Help.
return status.String()
}
// Go over a slice of clients (benchmarks) and delete the disconnected ones.
func delDeadBenches(mixed []bench) []bench {
var active []bench
for _, v := range mixed {
if v.connected {
active = append(active, v)
}
}
return active
}
func handleIncoming(ln net.Listener, tui *tea.Program) {
for {
conn, err := ln.Accept() // Wait for a new incoming connection.
if err != nil {
panic(err)
}
ch := make(chan int, 10)
go connFlood(conn, ch) // Send data into it.
go floodBench(ch, tui, conn.RemoteAddr()) // Benchmark it.
}
}
// Continuously send data to the incoming connection, and report the amount
// sent to the benchmarking function.
func connFlood(conn net.Conn, bytesSent chan int) {
data := make([]byte, bufSize)
for i := range data {
data[i] = 'A'
}
for {
n, err := conn.Write(data)
if err != nil {
close(bytesSent)
break
}
bytesSent <- n
}
}
// Calculate data flow rate and report it to the user interface.
// Data flow rate is the ratio: (total bytes sent)/(total time taken).
func floodBench(bytesSent chan int, tui *tea.Program, remoteAddr net.Addr) {
startTime := time.Now()
totalBytesSent := 0
dataFlowRate := 0.0
for n := range bytesSent {
totalBytesSent += n
elapsedTime := time.Since(startTime).Seconds()
dataFlowRate = float64(totalBytesSent) / elapsedTime
tui.Send(bench{
bytesPerSec: int(dataFlowRate),
remoteAddr: remoteAddr.String(),
connected: true,
})
}
tui.Send(bench{
bytesPerSec: int(dataFlowRate),
remoteAddr: remoteAddr.String(),
connected: false,
})
}
func init() {
flag.StringVar(&listenAddr, "listen", ":8080", "listen address")
flag.UintVar(&bufSize, "buf", 1024, "message chunk size")
}
func main() {
flag.Parse()
ln, err := net.Listen("tcp", listenAddr)
if err != nil {
panic(err)
}
tui := tea.NewProgram(model{
benches: []bench{},
})
go handleIncoming(ln, tui) // Listen incoming requests.
if _, err := tui.Run(); err != nil { // Start user interface (TUI).
panic(err)
}
}