/
request.go
243 lines (206 loc) · 5.47 KB
/
request.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
package speedtest
import (
"context"
"fmt"
"net/url"
"strconv"
"strings"
"time"
"github.com/go-resty/resty/v2"
"golang.org/x/sync/errgroup"
)
type downloadFunc func(context.Context, *resty.Client, string, int) error
type uploadFunc func(context.Context, *resty.Client, string, int) error
var dlSizes = [...]int{350, 500, 750, 1000, 1500, 2000, 2500, 3000, 3500, 4000}
var ulSizes = [...]int{100, 300, 500, 800, 1000, 1500, 2500, 3000, 3500, 4000} //kB
// DownloadTest executes the test to measure download speed
func (s *Server) DownloadTest(client *resty.Client) error {
return s.downloadTestContext(context.Background(), client, downloadRequest, downloadRequest)
}
func (s *Server) downloadTestContext(
ctx context.Context,
client *resty.Client,
dlWarmUp downloadFunc,
downloadRequest downloadFunc,
) error {
dlURL := strings.Split(s.URL, "/upload.php")[0]
eg := errgroup.Group{}
// Warming up
wuWeight := 2
sTime := time.Now()
for i := 0; i < 2; i++ {
eg.Go(func() error {
return dlWarmUp(ctx, client, dlURL, wuWeight)
})
}
if err := eg.Wait(); err != nil {
return err
}
fTime := time.Now()
// 1.125MB for each request (750 * 750 * 2 / 1000 / 1000)
reqMB := float64(dlSizes[wuWeight]) * float64(dlSizes[wuWeight]) * 2.0 / 1000.0 / 1000.0
// Calculate speed in Mbps
wuSpeed := float64(reqMB) * 8.0 * 2.0 / fTime.Sub(sTime.Add(s.Latency)).Seconds()
// Decide workload by warm up speed
workload := 0
weight := 0
skip := false
switch {
case wuSpeed > 50.0:
workload = 32
weight = 6
case wuSpeed > 10.0:
workload = 16
weight = 4
case wuSpeed > 4.0:
workload = 8
weight = 4
case wuSpeed > 2.5:
workload = 4
weight = 4
default:
skip = true
}
// Main speedtest
dlSpeed := wuSpeed
if !skip {
sTime = time.Now()
for i := 0; i < workload; i++ {
eg.Go(func() error {
return downloadRequest(ctx, client, dlURL, weight)
})
}
if err := eg.Wait(); err != nil {
return err
}
fTime = time.Now()
reqMB := float64(dlSizes[weight]) * float64(dlSizes[weight]) * 2.0 / 1000.0 / 1000.0
dlSpeed = float64(reqMB) * 8 * float64(workload) / fTime.Sub(sTime.Add(s.Latency)).Seconds()
}
s.DLSpeed = dlSpeed
return nil
}
// UploadTest executes the test to measure upload speed
func (s *Server) UploadTest(client *resty.Client) error {
return s.uploadTestContext(context.Background(), client, uploadRequest, uploadRequest)
}
func (s *Server) uploadTestContext(
ctx context.Context,
client *resty.Client,
ulWarmUp uploadFunc,
uploadRequest uploadFunc,
) error {
// Warm up
wuWeight := 4
sTime := time.Now()
eg := errgroup.Group{}
for i := 0; i < 2; i++ {
eg.Go(func() error {
return ulWarmUp(ctx, client, s.URL, wuWeight)
})
}
if err := eg.Wait(); err != nil {
return err
}
fTime := time.Now()
// 1.0 MB for each request
reqMB := float64(ulSizes[wuWeight]) / 1000.0
wuSpeed := reqMB * 8 * 2.0 / fTime.Sub(sTime.Add(s.Latency)).Seconds()
// Decide workload by warm up speed
workload := 0
weight := 0
skip := false
switch {
case wuSpeed > 50.0:
workload = 40
weight = 9
case wuSpeed > 10.0:
workload = 16
weight = 9
case wuSpeed > 4.0:
workload = 8
weight = 9
case wuSpeed > 2.5:
workload = 4
weight = 5
default:
skip = true
}
// Main speedtest
ulSpeed := wuSpeed
if !skip {
sTime = time.Now()
for i := 0; i < workload; i++ {
eg.Go(func() error {
return uploadRequest(ctx, client, s.URL, weight)
})
}
if err := eg.Wait(); err != nil {
return err
}
fTime = time.Now()
reqMB := float64(ulSizes[weight]) / 1000.0
ulSpeed = reqMB * 8 * float64(workload) / fTime.Sub(sTime.Add(s.Latency)).Seconds()
}
s.ULSpeed = ulSpeed
return nil
}
func downloadRequest(ctx context.Context, client *resty.Client, dlURL string, w int) error {
size := dlSizes[w]
xdlURL := dlURL + "/random" + strconv.Itoa(size) + "x" + strconv.Itoa(size) + ".jpg"
resp, err := client.R().
SetContext(ctx).
Get(xdlURL)
if err != nil {
return err
}
if resp.StatusCode() != 200 {
return fmt.Errorf("unexpected status code %v while downloading from %v", resp.StatusCode(), xdlURL)
}
return err
}
func uploadRequest(ctx context.Context, client *resty.Client, ulURL string, w int) error {
size := ulSizes[w]
v := url.Values{}
v.Add("content", strings.Repeat("0123456789", size*100-51))
resp, err := client.R().
SetContext(ctx).
SetBody(v.Encode()).
SetHeader("Content-Type", "application/x-www-form-urlencoded").
Post(ulURL)
if err != nil {
return err
}
if resp.StatusCode() != 200 {
return fmt.Errorf("unexpected status code %v while uploading to %v", resp.StatusCode(), ulURL)
}
return err
}
// PingTest executes test to measure latency
func (s *Server) PingTest(client *resty.Client) error {
return s.pingTestContext(context.Background(), client)
}
// pingTestContext executes test to measure latency, observing the given context.
func (s *Server) pingTestContext(ctx context.Context, client *resty.Client) error {
pingURL := strings.Split(s.URL, "/upload.php")[0] + "/latency.txt"
l := time.Duration(10000000000) // 10sec
for i := 0; i < 3; i++ {
sTime := time.Now()
resp, err := client.R().
SetContext(ctx).
Get(pingURL)
if err != nil {
return err
}
if resp.StatusCode() != 200 {
return fmt.Errorf("unexpected status code %v while pinging %v", resp.StatusCode(), pingURL)
}
fTime := time.Now()
if fTime.Sub(sTime) < l {
l = fTime.Sub(sTime)
}
}
// divide by 2 due to round trip time per request
s.Latency = time.Duration(int64(l.Nanoseconds() / 2))
return nil
}