forked from zpeters/speedtest
-
Notifications
You must be signed in to change notification settings - Fork 1
/
speedtest.go
279 lines (250 loc) · 7.15 KB
/
speedtest.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
package main
import (
"fmt"
"log"
"math/rand"
"net/url"
"os"
"strings"
"time"
)
import (
"github.com/codegangsta/cli"
)
import (
"github.com/labomatik/speedtest/debug"
"github.com/labomatik/speedtest/print"
"github.com/labomatik/speedtest/settings"
"github.com/labomatik/speedtest/sthttp"
"github.com/labomatik/speedtest/tests"
)
// VERSION is the version of our software
var Version string
func runTest(c *cli.Context) {
// create our server object and load initial config
var testServer sthttp.Server
//sthttp.CONFIG = sthttp.GetConfig()
// if we are *not* running a report then say hello to everyone
if !debug.REPORT {
fmt.Printf("Speedtest Cli\n")
}
// if we are in debug mode print outa an environment report
if debug.DEBUG {
print.EnvironmentReport(c)
}
// get all possible servers
//if debug.DEBUG {
// log.Printf("Getting all servers for our test list")
//}
var allServers []sthttp.Server
//if c.String("mini") == "" {
// allServers = sthttp.GetServers()
//}
// if a mini speedtest installation was specified, use that...
if c.String("mini") != "" {
//construct testserver object manually
u, err := url.Parse(c.String("mini"))
if err != nil {
log.Fatalf("Speedtest mini server URL is not a valid URL: %s", err)
}
if debug.DEBUG {
log.Printf("Using Mini Server '%s'", c.String("mini"))
}
testServer.URL = c.String("mini")
if !strings.HasSuffix(c.String("mini"), "/") {
testServer.URL += "/"
}
testServer.URL += "speedtest/upload.php"
testServer.Name = u.Host
testServer.Sponsor = "speedtest-mini"
testServer.ID = "0"
testServer.Latency = sthttp.GetLatency(testServer)
// if they specified a specific speedtest.net server, test against that...
} else if c.String("server") != "" {
if debug.DEBUG {
log.Printf("Server '%s' specified, getting info...", c.String("server"))
}
// find server and load latency report
testServer = tests.FindServer(c.String("server"), allServers)
// load latency
testServer.Latency = sthttp.GetLatency(testServer)
if !debug.REPORT {
fmt.Printf("Server: %s - %s (%s)\n", testServer.ID, testServer.Name, testServer.Sponsor)
}
// ...otherwise get a list of all servers sorted by distance...
} else {
if debug.DEBUG {
log.Printf("Getting closest servers...")
}
closestServers := sthttp.GetClosestServers(allServers)
if debug.DEBUG {
log.Printf("Getting the fastests of our closest servers...")
}
// ... and get the fastests NUMCLOSEST ones
testServer = sthttp.GetFastestServer(closestServers)
if !debug.REPORT {
fmt.Printf("Server: %s - %s (%s)\n", testServer.ID, testServer.Name, testServer.Sponsor)
}
}
// if ping only then just output latency results and exit nicely...
if c.Bool("ping") {
if c.Bool("report") {
if settings.ALGOTYPE == "max" {
fmt.Printf("%3.2f (Lowest)\n", testServer.Latency)
} else {
fmt.Printf("%3.2f (Avg)\n", testServer.Latency)
}
} else {
if settings.ALGOTYPE == "max" {
fmt.Printf("Ping (Lowest): %3.2f ms\n", testServer.Latency)
} else {
fmt.Printf("Ping (Avg): %3.2f ms\n", testServer.Latency)
}
}
os.Exit(0)
// ...otherwise run our full test
} else {
var dmbps float64
var umbps float64
if !debug.REPORT {
if c.Bool("downloadonly") {
dmbps = tests.DownloadTest(testServer)
} else if c.Bool("uploadonly") {
umbps = tests.UploadTest(testServer)
} else {
dmbps = tests.DownloadTest(testServer)
umbps = tests.UploadTest(testServer)
}
if settings.ALGOTYPE == "max" {
fmt.Printf("Ping (Lowest): %3.2f ms | Download (Max): %3.2f Mbps | Upload (Max): %3.2f Mbps\n", testServer.Latency, dmbps, umbps)
} else {
fmt.Printf("Ping (Avg): %3.2f ms | Download (Avg): %3.2f Mbps | Upload (Avg): %3.2f Mbps\n", testServer.Latency, dmbps, umbps)
}
} else {
fmt.Printf("%s%s%s%s%s(%s,%s)%s", time.Now().Format("2006-01-02 15:04:05 -0700"), settings.REPORTCHAR, testServer.ID, settings.REPORTCHAR, testServer.Sponsor, testServer.Name, testServer.Country, settings.REPORTCHAR)
fmt.Printf("%3.2f%s", testServer.Latency, settings.REPORTCHAR)
if c.Bool("downloadonly") {
dmbps = tests.DownloadTest(testServer)
dkbps := dmbps * 1000
fmt.Printf("%d\n", int(dkbps))
} else if c.Bool("uploadonly") {
umbps = tests.UploadTest(testServer)
ukbps := umbps * 1000
fmt.Printf("%d\n", int(ukbps))
} else {
dmbps = tests.DownloadTest(testServer)
dkbps := dmbps * 1000
fmt.Printf("%d%s", int(dkbps), settings.REPORTCHAR)
umbps = tests.UploadTest(testServer)
ukbps := umbps * 1000
fmt.Printf("%d\n", int(ukbps))
}
}
}
}
func main() {
// seeding randomness
rand.Seed(time.Now().UTC().UnixNano())
// set logging to stdout for global logger
log.SetOutput(os.Stdout)
// setting up cli settings
app := cli.NewApp()
app.Name = "speedtest"
app.Usage = "CLI for speedtest"
app.Author = "Original: Zach Peters - [email protected] - github.com/zpeters"
app.Version = Version
// setup cli flags
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "algo, a",
Usage: "Specify the measurement method to use ('max', 'avg')",
},
cli.BoolFlag{
Name: "debug, d",
Usage: "Turn on debugging",
},
cli.BoolFlag{
Name: "list, l",
Usage: "List available servers",
},
cli.BoolFlag{
Name: "ping, p",
Usage: "Ping only mode",
},
cli.BoolFlag{
Name: "quiet, q",
Usage: "Quiet mode",
},
cli.BoolFlag{
Name: "report, r",
Usage: "Reporting mode output, minimal output with '|' for separators, use '--rc' to change separator characters. Reports the following: Server ID, Server Name (Location), Ping time in ms, Download speed in kbps, Upload speed in kbps",
},
cli.BoolFlag{
Name: "downloadonly, do",
Usage: "Only perform download test",
},
cli.BoolFlag{
Name: "uploadonly, uo",
Usage: "Only perform upload test",
},
cli.StringFlag{
Name: "reportchar, rc",
Usage: "Set the report separator. Example: --rc=','",
},
cli.StringFlag{
Name: "server, s",
Usage: "Use a specific server",
},
cli.StringFlag{
Name: "mini, m",
Usage: "URL of speedtest mini server",
},
cli.IntFlag{
Name: "numclosest, nc",
Value: settings.NUMCLOSEST,
Usage: "Number of 'closest' servers to find",
},
cli.IntFlag{
Name: "numlatency, nl",
Value: settings.NUMLATENCYTESTS,
Usage: "Number of latency tests to perform",
},
}
// toggle our switches and setup variables
app.Action = func(c *cli.Context) {
// set our flags
if c.Bool("debug") {
debug.DEBUG = true
}
if c.Bool("quiet") {
debug.QUIET = true
}
if c.Bool("report") {
debug.REPORT = true
}
if c.String("algo") != "" {
if c.String("algo") == "max" {
settings.ALGOTYPE = "max"
} else if c.String("algo") == "avg" {
settings.ALGOTYPE = "avg"
} else {
fmt.Printf("** Invalid algorithm '%s'\n", c.String("algo"))
os.Exit(1)
}
}
settings.NUMCLOSEST = c.Int("numclosest")
settings.NUMLATENCYTESTS = c.Int("numlatency")
if c.String("reportchar") != "" {
settings.REPORTCHAR = c.String("reportchar")
}
// run a oneshot list
if c.Bool("list") {
tests.ListServers()
os.Exit(0)
}
// run our test
runTest(c)
}
// run the app
app.Run(os.Args)
}