-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
401 lines (344 loc) · 10.2 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
package main
import (
"context"
"crypto/tls"
"errors"
"flag"
"fmt"
"io"
"log"
"math/rand"
"net"
"net/http"
"os"
"os/signal"
"regexp"
"runtime"
"runtime/pprof"
"strconv"
"sync"
"syscall"
"time"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
)
// this program compares transfert speed of the Golang http server (and client) between HTTP1.1 and HTTP2
// to avoid encryption ovearhead it uses H2C mode
// usage: run with no argument to do a test with 10G of data
// use the "-s" option to be in server only mode and use another program like curl (or https://nspeed.app) to test
// build a 1MiB buffer of random data
const MaxChunkSize = 1024 * 1024 // warning : 1 MiB // this will be allocated in memory
var BigChunk [MaxChunkSize]byte
var bigbuff [16 * 1024 * 1024]byte
func InitBigChunk(seed int64) {
rng := rand.New(rand.NewSource(seed))
for i := int64(0); i < MaxChunkSize; i++ {
BigChunk[i] = byte(rng.Intn(256))
}
}
func init() {
InitBigChunk(time.Now().Unix())
}
// implements io.Discard
type Metrics struct {
mu sync.Mutex
StepSize int64
StartTime time.Time
ElapsedTime time.Duration
TotalRead int64
ReadCount int64
}
// ReadFrom version- performance sensitive, don't do much here
// it's basically a io.Discard with some metrics stored
func (wm *Metrics) ReadFrom(r io.Reader) (n int64, err error) {
wm.StartTime = time.Now()
wm.TotalRead = 0
wm.ReadCount = 0
wm.StepSize = 0
for {
read, err := r.Read(bigbuff[:])
s := int64(read)
wm.ReadCount++
wm.TotalRead += s
if wm.StepSize < s {
wm.StepSize = s
}
if err != nil {
break
}
}
wm.ElapsedTime = time.Since(wm.StartTime)
return wm.TotalRead, nil
}
// Write - performance sensitive, don't do much here
// it's basically a io.Discard with some metrics stored
func (wm *Metrics) Write(p []byte) (int, error) {
n := len(p)
s := int64(n)
wm.mu.Lock()
defer wm.mu.Unlock()
// store bigest step size
if s > wm.StepSize {
wm.StepSize = s
}
wm.TotalRead += s
// store elasped time
if wm.ReadCount == 0 {
wm.StartTime = time.Now()
} else {
wm.ElapsedTime = time.Since(wm.StartTime)
}
wm.ReadCount++
return n, nil
}
// regexp to parse url
var StreamPathRegexp *regexp.Regexp
func createHandler() http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("/", rootHandler)
var handler http.Handler = mux
return handler
}
// handle the only route: '/number' which send <number> bytes of random data
func rootHandler(w http.ResponseWriter, r *http.Request) {
//fmt.Printf("request from %s: %s\n", r.RemoteAddr, r.URL)
method := r.Method
if method == "" {
method = "GET"
}
if method == "GET" {
match := StreamPathRegexp.FindStringSubmatch(r.URL.Path[1:])
if len(match) == 2 {
n, err := strconv.ParseInt(match[1], 10, 64)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
streamBytes(w, r, n)
return
} else {
http.Error(w, "Not found (no regexp match)", http.StatusNotFound)
return
}
}
if method == "POST" {
startedAt := time.Now()
fmt.Printf("%s - starting Upload (%s) of %d bytes from %s\n", startedAt.Format("2006-01-02 15:04:05"), r.Proto, r.ContentLength, r.RemoteAddr)
n, err := io.CopyBuffer(io.Discard, r.Body, bigbuff[:])
endedAt := time.Now()
dur := endedAt.Sub(startedAt)
if err != nil {
http.Error(w, fmt.Sprintf("upload error %v", err), http.StatusInternalServerError)
}
report := fmt.Sprintf("%s - received %d bytes in %s (%s) with %s from %s (expected %d bytes)\n",
endedAt.Format("2006-01-02 15:04:05"),
n,
dur,
FormatBitperSecond(dur.Seconds(), n),
r.Proto, r.RemoteAddr, r.ContentLength)
fmt.Println(report)
w.Write([]byte(report))
return
}
http.Error(w, "unhandled method", http.StatusBadRequest)
}
// send 'size' bytes of random data
func streamBytes(w http.ResponseWriter, r *http.Request, size int64) {
// the buffer we use to send data
var chunkSize int64 = 256 * 1024 // 256KiB chunk (sweet spot value may depend on OS & hardware)
if chunkSize > MaxChunkSize {
log.Fatal("chunksize is too big")
}
chunk := BigChunk[:chunkSize]
w.Header().Set("Content-Type", "application/octet-stream")
w.Header().Set("Content-Length", strconv.FormatInt(size, 10))
//fmt.Printf("header sent to %s: %s\n", r.RemoteAddr, r.URL)
startTime := time.Now()
size_tx := int64(0)
hasEnded := false
var numChunk = size / chunkSize
for i := int64(0); i < numChunk; i++ {
n, err := w.Write(chunk)
size_tx = size_tx + int64(n)
if err != nil {
hasEnded = true
break
}
}
if size%chunkSize > 0 && !hasEnded {
n, _ := w.Write(chunk[:size%chunkSize])
size_tx = size_tx + int64(n)
}
f := w.(http.Flusher)
f.Flush()
duration := time.Since(startTime)
fmt.Printf("server sent %d bytes in %s = %s (%d chunks) to %s\n", size_tx, duration, FormatBitperSecond(duration.Seconds(), size_tx), chunkSize, r.RemoteAddr)
}
// create a HTTP server, wait for ctx.Done(), shutdown the server and signal the WaitGroup
func createServer(ctx context.Context, host string, port int, useH2C bool, wg *sync.WaitGroup, ready chan bool) {
wg.Add(1)
defer wg.Done()
listenAddr := net.JoinHostPort(host, strconv.Itoa(port))
server := &http.Server{
Addr: listenAddr,
Handler: createHandler(),
}
if useH2C {
server.Handler = h2c.NewHandler(server.Handler, &http2.Server{})
}
ln, err := net.Listen("tcp", server.Addr)
if err != nil {
log.Fatalf("cannot listen to %s: %s", server.Addr, err)
}
// this will wait for ctx.Done then shutdown the server
go func() {
<-ctx.Done()
fmt.Printf("server %s shuting down\n", listenAddr)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
server.Shutdown(ctx)
}()
// signal the server is listening
ready <- true
err = server.Serve(ln)
if err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatalf("cannot serve %s: %s", server.Addr, err)
}
}
// http client, download the url to 'null' (discard)
func Download(ctx context.Context, url string, useH2C bool) error {
var dialer = &net.Dialer{
Timeout: 5 * time.Second, // fail quick
FallbackDelay: -1, // don't use Happy Eyeballs
}
var netTransport = http.DefaultTransport.(*http.Transport).Clone()
netTransport.DialContext = dialer.DialContext
var rt http.RoundTripper = netTransport
if useH2C {
rt = &http2.Transport{
AllowHTTP: true,
DialTLS: func(network, addr string, cfg *tls.Config) (net.Conn, error) {
return dialer.Dial(network, addr)
},
MaxReadFrameSize: 256 * 1024, // for perf fix since cl: https://go-review.googlesource.com/c/net/+/362834
}
}
var body io.ReadCloser = http.NoBody
req, err := http.NewRequestWithContext(ctx, "GET", url, body)
if err != nil {
return err
}
resp, err := rt.RoundTrip(req)
if err == nil && resp != nil {
fmt.Printf("receiving data with %s\n", resp.Proto)
wm := Metrics{}
_, err = io.CopyBuffer(&wm, resp.Body, bigbuff[:])
resp.Body.Close()
if err != nil {
if !errors.Is(err, io.EOF) {
return err
}
}
fmt.Printf("client received %d bytes in %v = %s, %d write ops, %d buff \n", wm.TotalRead, wm.ElapsedTime, FormatBitperSecond(wm.ElapsedTime.Seconds(), wm.TotalRead), wm.ReadCount, wm.StepSize)
}
return err
}
// client just like "curl -o /dev/null url"
func doClient(ctx context.Context, url string, h2c bool) error {
fmt.Printf("downloading %s\n", url)
err := Download(ctx, url, h2c)
if err != nil {
fmt.Printf("client error for %s: %s\n", url, err)
}
return err
}
var optServer = flag.Bool("s", false, "server mode only")
var optClient = flag.String("c", "", "client only mode, connect to url")
var optH2C = flag.Bool("h2c", false, "force h2c")
var optCpuProfile = flag.String("cpuprof", "", "write cpu profile to file")
var optT1 = flag.Bool("t1", true, "do predifined test 1")
var optT2 = flag.Bool("t2", true, "do predifined test 2")
var optSize = flag.Uint64("b", 10000000000, "number of bytes to transfert")
func main() {
flag.Parse()
if *optCpuProfile != "" {
runtime.SetBlockProfileRate(1)
f, err := os.Create(*optCpuProfile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer func() {
//fmt.Println("StopCPUProfile")
pprof.StopCPUProfile()
}()
}
StreamPathRegexp = regexp.MustCompile("^(" + "[0-9]+" + ")$")
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer cancel()
var wg sync.WaitGroup
if *optClient == "" {
ready := make(chan bool)
//1. create a http server
go createServer(ctx, "", 8765, true, &wg, ready)
<-ready
fmt.Printf("server created and listening at %s (http1.1)\n", "8765")
//2. create a http/2 (h2c) server
go createServer(ctx, "", 9876, true, &wg, ready)
<-ready
fmt.Printf("server created and listening at %s (http/2 cleartext)\n", "9876")
// if server mode, just wait forever for something else to cancel
if *optServer {
fmt.Printf("server mode on\n")
<-ctx.Done()
return
}
} else {
doClient(ctx, *optClient, *optH2C)
return
}
if *optT1 {
doClient(ctx, fmt.Sprintf("http://localhost:8765/%d", *optSize), false)
}
if *optT2 {
doClient(ctx, fmt.Sprintf("http://localhost:9876/%d", *optSize), true)
}
cancel()
wg.Wait()
}
// human friendly formatting stuff
// FormatBitperSecond format bit per seconds in human readable format
func FormatBitperSecond(elapsedSeconds float64, totalBytes int64) string {
// nyi - fix me
defer func() {
if r := recover(); r != nil {
fmt.Println("recovered from divide by zero")
}
}()
speed := "(too fast)"
if elapsedSeconds > 0 {
speed = ByteCountDecimal((int64)(((float64)(totalBytes)*8.0)/elapsedSeconds)) + "bps"
}
return speed
}
// ByteCountDecimal format byte size to human readable format (decimal units)
// suitable to append the unit name after (B, bps, etc)
func ByteCountDecimal(b int64) string {
s, u := byteCount(b, 1000, "kMGTPE")
return s + " " + u
}
// copied from : https://programming.guide/go/formatting-byte-size-to-human-readable-format.html
func byteCount(b int64, unit int64, units string) (string, string) {
if b < unit {
return fmt.Sprintf("%d", b), ""
}
div, exp := int64(unit), 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
if exp >= len(units) {
return fmt.Sprintf("%d", b), ""
}
return fmt.Sprintf("%.1f", float64(b)/float64(div)), units[exp : exp+1]
}