-
Notifications
You must be signed in to change notification settings - Fork 5
/
client.go
152 lines (136 loc) · 3.59 KB
/
client.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
package perf
import (
"context"
"crypto/tls"
"encoding/binary"
"encoding/json"
"fmt"
"io"
"log"
"time"
"github.com/quic-go/quic-go"
)
type Result struct {
Type string `json:"type"`
TimeSeconds float64 `json:"timeSeconds"`
UploadBytes uint64 `json:"uploadBytes"`
DownloadBytes uint64 `json:"downloadBytes"`
}
func RunClient(addr string, uploadBytes, downloadBytes uint64, keyLogFile io.Writer) error {
start := time.Now()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
conn, err := quic.DialAddr(
ctx,
addr,
&tls.Config{
InsecureSkipVerify: true,
NextProtos: []string{ALPN},
KeyLogWriter: keyLogFile,
},
config,
)
if err != nil {
return err
}
str, err := conn.OpenStream()
if err != nil {
return err
}
uploadTook, downloadTook, err := handleClientStream(str, uploadBytes, downloadBytes)
if err != nil {
return err
}
log.Printf("uploaded %s: %.2fs (%s/s)", formatBytes(uploadBytes), uploadTook.Seconds(), formatBytes(bandwidth(uploadBytes, uploadTook)))
log.Printf("downloaded %s: %.2fs (%s/s)", formatBytes(downloadBytes), downloadTook.Seconds(), formatBytes(bandwidth(downloadBytes, downloadTook)))
json, err := json.Marshal(Result{
TimeSeconds: time.Since(start).Seconds(),
Type: "final",
UploadBytes: uploadBytes,
DownloadBytes: downloadBytes,
})
if err != nil {
return err
}
fmt.Println(string(json))
return nil
}
func handleClientStream(str io.ReadWriteCloser, uploadBytes, downloadBytes uint64) (uploadTook, downloadTook time.Duration, err error) {
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, downloadBytes)
if _, err := str.Write(b); err != nil {
return 0, 0, err
}
// upload data
b = make([]byte, 16*1024)
uploadStart := time.Now()
lastReportTime := time.Now()
lastReportWrite := uint64(0)
for uploadBytes > 0 {
now := time.Now()
if now.Sub(lastReportTime) >= time.Second {
jsonB, err := json.Marshal(Result{
TimeSeconds: now.Sub(lastReportTime).Seconds(),
UploadBytes: lastReportWrite,
Type: "intermediary",
})
if err != nil {
log.Fatalf("failed to marshal perf result: %s", err)
}
fmt.Println(string(jsonB))
lastReportTime = now
lastReportWrite = 0
}
if uploadBytes < uint64(len(b)) {
b = b[:uploadBytes]
}
n, err := str.Write(b)
if err != nil {
return 0, 0, err
}
uploadBytes -= uint64(n)
lastReportWrite += uint64(n)
}
if err := str.Close(); err != nil {
return 0, 0, err
}
uploadTook = time.Since(uploadStart)
// download data
b = b[:cap(b)]
remaining := downloadBytes
downloadStart := time.Now()
lastReportTime = time.Now()
lastReportRead := uint64(0)
for remaining > 0 {
now := time.Now()
if now.Sub(lastReportTime) >= time.Second {
jsonB, err := json.Marshal(Result{
TimeSeconds: now.Sub(lastReportTime).Seconds(),
DownloadBytes: lastReportRead,
Type: "intermediary",
})
if err != nil {
log.Fatalf("failed to marshal perf result: %s", err)
}
fmt.Println(string(jsonB))
lastReportTime = now
lastReportRead = 0
}
n, err := str.Read(b)
if uint64(n) > remaining {
return 0, 0, fmt.Errorf("server sent more data than expected, expected %d, got %d", downloadBytes, remaining+uint64(n))
}
remaining -= uint64(n)
lastReportRead += uint64(n)
if err != nil {
if err == io.EOF {
if remaining == 0 {
break
}
return 0, 0, fmt.Errorf("server didn't send enough data, expected %d, got %d", downloadBytes, downloadBytes-remaining)
}
return 0, 0, err
}
}
return uploadTook, time.Since(downloadStart), nil
}