forked from showwin/speedtest-go
/
request.go
298 lines (255 loc) · 7.04 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
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
package speedtest
import (
"context"
"io"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"golang.org/x/sync/errgroup"
)
type downloadWarmUpFunc func(context.Context, *http.Client, string) error
type downloadFunc func(context.Context, *http.Client, string, int) error
type uploadWarmUpFunc func(context.Context, *http.Client, string) error
type uploadFunc func(context.Context, *http.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(savingMode bool) error {
return s.downloadTestContext(context.Background(), savingMode, dlWarmUp, downloadRequest)
}
// DownloadTestContext executes the test to measure download speed, observing the given context.
func (s *Server) DownloadTestContext(ctx context.Context, savingMode bool) error {
return s.downloadTestContext(ctx, savingMode, dlWarmUp, downloadRequest)
}
func (s *Server) downloadTestContext(
ctx context.Context,
savingMode bool,
dlWarmUp downloadWarmUpFunc,
downloadRequest downloadFunc,
) error {
dlURL := strings.Split(s.URL, "/upload.php")[0]
eg := errgroup.Group{}
// Warming up
sTime := time.Now()
for i := 0; i < 2; i++ {
eg.Go(func() error {
return dlWarmUp(ctx, s.doer, dlURL)
})
}
if err := eg.Wait(); err != nil {
return err
}
fTime := time.Now()
// If the bandwidth is too large, the download sometimes finish earlier than the latency.
// In this case, we ignore the the latency that is included server information.
// This is not affected to the final result since this is a warm up test.
timeToSpend := fTime.Sub(sTime.Add(s.Latency)).Seconds()
if timeToSpend < 0 {
timeToSpend = fTime.Sub(sTime).Seconds()
}
// 1.125MB for each request (750 * 750 * 2)
wuSpeed := 1.125 * 8 * 2 / timeToSpend
// Decide workload by warm up speed
workload := 0
weight := 0
skip := false
if savingMode {
workload = 6
weight = 3
} else if 50.0 < wuSpeed {
workload = 32
weight = 6
} else if 10.0 < wuSpeed {
workload = 16
weight = 4
} else if 4.0 < wuSpeed {
workload = 8
weight = 4
} else if 2.5 < wuSpeed {
workload = 4
weight = 4
} else {
skip = true
}
// Main speedtest
dlSpeed := wuSpeed
if !skip {
sTime = time.Now()
for i := 0; i < workload; i++ {
eg.Go(func() error {
return downloadRequest(ctx, s.doer, dlURL, weight)
})
}
if err := eg.Wait(); err != nil {
return err
}
fTime = time.Now()
reqMB := dlSizes[weight] * dlSizes[weight] * 2 / 1000 / 1000
dlSpeed = float64(reqMB) * 8 * float64(workload) / fTime.Sub(sTime).Seconds()
}
s.DLSpeed = dlSpeed
return nil
}
// UploadTest executes the test to measure upload speed
func (s *Server) UploadTest(savingMode bool) error {
return s.uploadTestContext(context.Background(), savingMode, ulWarmUp, uploadRequest)
}
// UploadTestContext executes the test to measure upload speed, observing the given context.
func (s *Server) UploadTestContext(ctx context.Context, savingMode bool) error {
return s.uploadTestContext(ctx, savingMode, ulWarmUp, uploadRequest)
}
func (s *Server) uploadTestContext(
ctx context.Context,
savingMode bool,
ulWarmUp uploadWarmUpFunc,
uploadRequest uploadFunc,
) error {
// Warm up
sTime := time.Now()
eg := errgroup.Group{}
for i := 0; i < 2; i++ {
eg.Go(func() error {
return ulWarmUp(ctx, s.doer, s.URL)
})
}
if err := eg.Wait(); err != nil {
return err
}
fTime := time.Now()
// 1.0 MB for each request
wuSpeed := 1.0 * 8 * 2 / fTime.Sub(sTime.Add(s.Latency)).Seconds()
// Decide workload by warm up speed
workload := 0
weight := 0
skip := false
if savingMode {
workload = 1
weight = 7
} else if 50.0 < wuSpeed {
workload = 40
weight = 9
} else if 10.0 < wuSpeed {
workload = 16
weight = 9
} else if 4.0 < wuSpeed {
workload = 8
weight = 9
} else if 2.5 < wuSpeed {
workload = 4
weight = 5
} else {
skip = true
}
// Main speedtest
ulSpeed := wuSpeed
if !skip {
sTime = time.Now()
for i := 0; i < workload; i++ {
eg.Go(func() error {
return uploadRequest(ctx, s.doer, s.URL, weight)
})
}
if err := eg.Wait(); err != nil {
return err
}
fTime = time.Now()
reqMB := float64(ulSizes[weight]) / 1000
ulSpeed = reqMB * 8 * float64(workload) / fTime.Sub(sTime).Seconds()
}
s.ULSpeed = ulSpeed
return nil
}
func dlWarmUp(ctx context.Context, doer *http.Client, dlURL string) error {
size := dlSizes[2]
xdlURL := dlURL + "/random" + strconv.Itoa(size) + "x" + strconv.Itoa(size) + ".jpg"
req, err := http.NewRequestWithContext(ctx, http.MethodGet, xdlURL, nil)
if err != nil {
return err
}
resp, err := doer.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
_, err = io.Copy(ioutil.Discard, resp.Body)
return err
}
func ulWarmUp(ctx context.Context, doer *http.Client, ulURL string) error {
size := ulSizes[4]
v := url.Values{}
v.Add("content", strings.Repeat("0123456789", size*100-51))
req, err := http.NewRequestWithContext(ctx, http.MethodPost, ulURL, strings.NewReader(v.Encode()))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := doer.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
_, err = io.Copy(ioutil.Discard, resp.Body)
return err
}
func downloadRequest(ctx context.Context, doer *http.Client, dlURL string, w int) error {
size := dlSizes[w]
xdlURL := dlURL + "/random" + strconv.Itoa(size) + "x" + strconv.Itoa(size) + ".jpg"
req, err := http.NewRequestWithContext(ctx, http.MethodGet, xdlURL, nil)
if err != nil {
return err
}
resp, err := doer.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
_, err = io.Copy(ioutil.Discard, resp.Body)
return err
}
func uploadRequest(ctx context.Context, doer *http.Client, ulURL string, w int) error {
size := ulSizes[w]
v := url.Values{}
v.Add("content", strings.Repeat("0123456789", size*100-51))
req, err := http.NewRequestWithContext(ctx, http.MethodPost, ulURL, strings.NewReader(v.Encode()))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := doer.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
_, err = io.Copy(ioutil.Discard, resp.Body)
return err
}
// PingTest executes test to measure latency
func (s *Server) PingTest() error {
return s.PingTestContext(context.Background())
}
// PingTestContext executes test to measure latency, observing the given context.
func (s *Server) PingTestContext(ctx context.Context) error {
pingURL := strings.Split(s.URL, "/upload.php")[0] + "/latency.txt"
l := time.Second * 10
for i := 0; i < 3; i++ {
sTime := time.Now()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, pingURL, nil)
if err != nil {
return err
}
resp, err := s.doer.Do(req)
if err != nil {
return err
}
fTime := time.Now()
if fTime.Sub(sTime) < l {
l = fTime.Sub(sTime)
}
resp.Body.Close()
}
s.Latency = time.Duration(int64(l.Nanoseconds() / 2))
return nil
}