-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathrequest.go
337 lines (309 loc) · 9.05 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
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
package speedtest
import (
"context"
"errors"
"fmt"
"math"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
type (
downloadFunc func(context.Context, *Server, int) error
uploadFunc func(context.Context, *Server, int) error
)
var (
dlSizes = [...]int{350, 500, 750, 1000, 1500, 2000, 2500, 3000, 3500, 4000}
ulSizes = [...]int{100, 300, 500, 800, 1000, 1500, 2500, 3000, 3500, 4000} // kB
)
func (s *Server) MultiDownloadTestContext(ctx context.Context, servers Servers) error {
if s.Context.config.NoDownload {
dbg.Println("Download test disabled")
return nil
}
ss := servers.Available()
if ss.Len() == 0 {
return errors.New("not found available servers")
}
mainIDIndex := 0
var fp *FuncGroup
_context, cancel := context.WithCancel(ctx)
for i, server := range *ss {
if server.ID == s.ID {
mainIDIndex = i
}
sp := server
dbg.Printf("Register Download Handler: %s\n", sp.URL)
fp = server.Context.RegisterDownloadHandler(func() {
_ = downloadRequest(_context, sp, 3)
})
}
fp.Start(cancel, mainIDIndex) // block here
s.DLSpeed = fp.manager.GetAvgDownloadRate()
return nil
}
func (s *Server) MultiUploadTestContext(ctx context.Context, servers Servers) error {
if s.Context.config.NoUpload {
dbg.Println("Upload test disabled")
return nil
}
ss := servers.Available()
if ss.Len() == 0 {
return errors.New("not found available servers")
}
mainIDIndex := 0
var fp *FuncGroup
_context, cancel := context.WithCancel(ctx)
for i, server := range *ss {
if server.ID == s.ID {
mainIDIndex = i
}
sp := server
dbg.Printf("Register Upload Handler: %s\n", sp.URL)
fp = server.Context.RegisterUploadHandler(func() {
_ = uploadRequest(_context, sp, 3)
})
}
fp.Start(cancel, mainIDIndex) // block here
s.ULSpeed = fp.manager.GetAvgUploadRate()
return nil
}
// DownloadTest executes the test to measure download speed
func (s *Server) DownloadTest() error {
return s.downloadTestContext(context.Background(), downloadRequest)
}
// DownloadTestContext executes the test to measure download speed, observing the given context.
func (s *Server) DownloadTestContext(ctx context.Context) error {
return s.downloadTestContext(ctx, downloadRequest)
}
func (s *Server) downloadTestContext(ctx context.Context, downloadRequest downloadFunc) error {
if s.Context.config.NoDownload {
dbg.Println("Download test disabled")
return nil
}
_context, cancel := context.WithCancel(ctx)
s.Context.RegisterDownloadHandler(func() {
_ = downloadRequest(_context, s, 3)
}).Start(cancel, 0)
s.DLSpeed = s.Context.GetAvgDownloadRate()
return nil
}
// UploadTest executes the test to measure upload speed
func (s *Server) UploadTest() error {
return s.uploadTestContext(context.Background(), uploadRequest)
}
// UploadTestContext executes the test to measure upload speed, observing the given context.
func (s *Server) UploadTestContext(ctx context.Context) error {
return s.uploadTestContext(ctx, uploadRequest)
}
func (s *Server) uploadTestContext(ctx context.Context, uploadRequest uploadFunc) error {
if s.Context.config.NoUpload {
dbg.Println("Upload test disabled")
return nil
}
_context, cancel := context.WithCancel(ctx)
s.Context.RegisterUploadHandler(func() {
_ = uploadRequest(_context, s, 4)
}).Start(cancel, 0)
s.ULSpeed = s.Context.GetAvgUploadRate()
return nil
}
func downloadRequest(ctx context.Context, s *Server, w int) error {
size := dlSizes[w]
xdlURL := strings.Split(s.URL, "/upload.php")[0] + "/random" + strconv.Itoa(size) + "x" + strconv.Itoa(size) + ".jpg"
dbg.Printf("XdlURL: %s\n", xdlURL)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, xdlURL, nil)
if err != nil {
return err
}
resp, err := s.Context.doer.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
return s.Context.NewChunk().DownloadHandler(resp.Body)
}
func uploadRequest(ctx context.Context, s *Server, w int) error {
size := ulSizes[w]
dc := s.Context.NewChunk().UploadHandler(int64(size*100-51) * 10)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, s.URL, dc)
req.ContentLength = dc.(*DataChunk).ContentLength
dbg.Printf("Len=%d, XulURL: %s\n", req.ContentLength, s.URL)
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/octet-stream")
resp, err := s.Context.doer.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
return err
}
// PingTest executes test to measure latency
func (s *Server) PingTest(callback func(latency time.Duration)) error {
return s.PingTestContext(context.Background(), callback)
}
// PingTestContext executes test to measure latency, observing the given context.
func (s *Server) PingTestContext(ctx context.Context, callback func(latency time.Duration)) (err error) {
var vectorPingResult []int64
if s.Context.config.ICMP {
vectorPingResult, err = s.ICMPPing(ctx, time.Second*4, 10, time.Millisecond*200, callback)
} else {
vectorPingResult, err = s.HTTPPing(ctx, 10, time.Millisecond*200, nil)
}
if err != nil || len(vectorPingResult) == 0 {
return err
}
dbg.Printf("Before StandardDeviation: %v\n", vectorPingResult)
mean, _, std, min, max := standardDeviation(vectorPingResult)
s.Latency = time.Duration(mean) * time.Nanosecond
s.Jitter = time.Duration(std) * time.Nanosecond
s.MinLatency = time.Duration(min) * time.Nanosecond
s.MaxLatency = time.Duration(max) * time.Nanosecond
return nil
}
func (s *Server) HTTPPing(
ctx context.Context,
echoTimes int,
echoFreq time.Duration,
callback func(latency time.Duration),
) (latencies []int64, err error) {
u, err := url.Parse(s.URL)
if err != nil || len(u.Host) == 0 {
return nil, err
}
pingDst := fmt.Sprintf("%s/latency.txt", s.URL)
dbg.Printf("Echo: %s\n", pingDst)
failTimes := 0
req, err := http.NewRequestWithContext(ctx, http.MethodGet, pingDst, nil)
if err != nil {
return nil, err
}
for i := 0; i < echoTimes; i++ {
sTime := time.Now()
_, err = s.Context.doer.Do(req)
endTime := time.Since(sTime)
if err != nil {
failTimes++
continue
}
latencies = append(latencies, endTime.Nanoseconds()/2)
dbg.Printf("2RTT: %s\n", endTime)
if callback != nil {
callback(endTime / 2)
}
time.Sleep(echoFreq)
}
if failTimes == echoTimes {
return nil, errors.New("server connect timeout")
}
return
}
const PingTimeout = -1
const echoOptionDataSize = 32 // `echoMessage` need to change at same time
// ICMPPing privileged method
func (s *Server) ICMPPing(
ctx context.Context,
readTimeout time.Duration,
echoTimes int,
echoFreq time.Duration,
callback func(latency time.Duration),
) (latencies []int64, err error) {
u, err := url.ParseRequestURI(s.URL)
if err != nil || len(u.Host) == 0 {
return nil, err
}
dbg.Printf("Echo: %s\n", strings.Split(u.Host, ":")[0])
dialContext, err := s.Context.ipDialer.DialContext(ctx, "ip:icmp", strings.Split(u.Host, ":")[0])
if err != nil {
return nil, err
}
defer dialContext.Close()
ICMPData := make([]byte, 8+echoOptionDataSize) // header + data
ICMPData[0] = 8 // echo
ICMPData[1] = 0 // code
ICMPData[2] = 0 // checksum
ICMPData[3] = 0 // checksum
ICMPData[4] = 0 // id
ICMPData[5] = 1 // id
ICMPData[6] = 0 // seq
ICMPData[7] = 1 // seq
var echoMessage = "Hi! SpeedTest-Go \\(●'◡'●)/"
for i := 0; i < len(echoMessage); i++ {
ICMPData[7+i] = echoMessage[i]
}
failTimes := 0
for i := 0; i < echoTimes; i++ {
ICMPData[2] = byte(0)
ICMPData[3] = byte(0)
ICMPData[6] = byte(1 >> 8)
ICMPData[7] = byte(1)
ICMPData[8+echoOptionDataSize-1] = 6
cs := checkSum(ICMPData)
ICMPData[2] = byte(cs >> 8)
ICMPData[3] = byte(cs)
sTime := time.Now()
_ = dialContext.SetDeadline(sTime.Add(readTimeout))
_, err = dialContext.Write(ICMPData)
if err != nil {
failTimes += echoTimes - i
break
}
buf := make([]byte, 20+echoOptionDataSize+8)
_, err = dialContext.Read(buf)
if err != nil {
failTimes++
continue
}
endTime := time.Since(sTime)
latencies = append(latencies, endTime.Nanoseconds())
dbg.Printf("1RTT: %s\n", endTime)
if callback != nil {
callback(endTime)
}
time.Sleep(echoFreq)
}
if failTimes == echoTimes {
return nil, errors.New("server connect timeout")
}
return
}
func checkSum(data []byte) uint16 {
var sum uint32
var length = len(data)
var index int
for length > 1 {
sum += uint32(data[index])<<8 + uint32(data[index+1])
index += 2
length -= 2
}
if length > 0 {
sum += uint32(data[index])
}
sum += sum >> 16
return uint16(^sum)
}
func standardDeviation(vector []int64) (mean, variance, stdDev, min, max int64) {
var sumNum, accumulate int64
min = math.MaxInt64
max = math.MinInt64
for _, value := range vector {
sumNum += value
if min > value {
min = value
}
if max < value {
max = value
}
}
mean = sumNum / int64(len(vector))
for _, value := range vector {
accumulate += (value - mean) * (value - mean)
}
variance = accumulate / int64(len(vector))
stdDev = int64(math.Sqrt(float64(variance)))
return
}