forked from showwin/speedtest-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
191 lines (161 loc) · 3.91 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
package main
import (
"fmt"
"io/ioutil"
"net/url"
"strconv"
"strings"
"sync"
"time"
"github.com/logrusorgru/aurora"
)
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
func downloadTest(sURL string, latency time.Duration) float64 {
dlURL := strings.Split(sURL, "/upload")[0]
fmt.Printf("%-14s: ", "Download Test")
wg := new(sync.WaitGroup)
// Warming up
sTime := time.Now()
for i := 0; i < 2; i++ {
wg.Add(1)
go dlWarmUp(wg, dlURL)
}
wg.Wait()
fTime := time.Now()
// 1.125MB for each request (750 * 750 * 2)
wuSpeed := 1.125 * 8 * 2 / fTime.Sub(sTime.Add(latency)).Seconds()
// Decide workload by warm up speed
workload := 0
weight := 0
skip := false
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 == false {
sTime = time.Now()
for i := 0; i < workload; i++ {
wg.Add(1)
go downloadRequest(wg, dlURL, weight)
}
wg.Wait()
fTime = time.Now()
fmt.Printf("\n")
reqMB := dlSizes[weight] * dlSizes[weight] * 2 / 1000 / 1000
dlSpeed = float64(reqMB) * 8 * float64(workload) / fTime.Sub(sTime).Seconds()
}
return dlSpeed
}
func uploadTest(sURL string, latency time.Duration) float64 {
fmt.Printf("%-14s: ", "Upload Test")
wg := new(sync.WaitGroup)
// Warm up
sTime := time.Now()
wg = new(sync.WaitGroup)
for i := 0; i < 2; i++ {
wg.Add(1)
go ulWarmUp(wg, sURL)
}
wg.Wait()
fTime := time.Now()
// 1.0 MB for each request
wuSpeed := 1.0 * 8 * 2 / fTime.Sub(sTime.Add(latency)).Seconds()
// Decide workload by warm up speed
workload := 0
weight := 0
skip := false
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 == false {
sTime = time.Now()
for i := 0; i < workload; i++ {
wg.Add(1)
go uploadRequest(wg, sURL, weight)
}
wg.Wait()
fTime = time.Now()
fmt.Printf("\n")
reqMB := float64(ulSizes[weight]) / 1000
ulSpeed = reqMB * 8 * float64(workload) / fTime.Sub(sTime).Seconds()
}
return ulSpeed
}
func dlWarmUp(wg *sync.WaitGroup, dlURL string) {
size := dlSizes[2]
url := dlURL + "/random" + strconv.Itoa(size) + "x" + strconv.Itoa(size) + ".jpg"
resp, err := client.Get(url)
checkError(err)
defer resp.Body.Close()
ioutil.ReadAll(resp.Body)
wg.Done()
}
func ulWarmUp(wg *sync.WaitGroup, ulURL string) {
size := ulSizes[4]
v := url.Values{}
v.Add("content", strings.Repeat("0123456789", size*100-51))
resp, err := client.PostForm(ulURL, v)
checkError(err)
defer resp.Body.Close()
ioutil.ReadAll(resp.Body)
wg.Done()
}
func downloadRequest(wg *sync.WaitGroup, dlURL string, w int) {
size := dlSizes[w]
url := dlURL + "/random" + strconv.Itoa(size) + "x" + strconv.Itoa(size) + ".jpg"
resp, err := client.Get(url)
checkError(err)
defer resp.Body.Close()
ioutil.ReadAll(resp.Body)
fmt.Printf(".")
wg.Done()
}
func uploadRequest(wg *sync.WaitGroup, ulURL string, w int) {
size := ulSizes[9]
v := url.Values{}
v.Add("content", strings.Repeat("0123456789", size*100-51))
resp, err := client.PostForm(ulURL, v)
checkError(err)
defer resp.Body.Close()
ioutil.ReadAll(resp.Body)
fmt.Printf(".")
wg.Done()
}
func pingTest(sURL string) time.Duration {
pingURL := strings.Split(sURL, "/upload")[0] + "/latency.txt"
l := time.Duration(100000000000) // 10sec
for i := 0; i < 3; i++ {
sTime := time.Now()
resp, err := client.Get(pingURL)
fTime := time.Now()
checkError(err)
defer resp.Body.Close()
if fTime.Sub(sTime) < l {
l = fTime.Sub(sTime)
}
}
fmt.Printf("\n%-14s: %v\n", "Latency", aurora.Gray(24, (l/2.0).Truncate(time.Microsecond)))
return l / 2.0
}