forked from taganaka/SpeedTest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
311 lines (265 loc) · 12.7 KB
/
main.cpp
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
#include <iostream>
#include <map>
#include <iomanip>
#include "SpeedTest.h"
#include "TestConfigTemplate.h"
#include "CmdOptions.h"
#include <csignal>
void banner(){
std::cout << "SpeedTest++ version " << SpeedTest_VERSION_MAJOR << "." << SpeedTest_VERSION_MINOR << std::endl;
std::cout << "Speedtest.net command line interface" << std::endl;
std::cout << "Info: " << SpeedTest_HOME_PAGE << std::endl;
std::cout << "Fixed: https://github.com/gglluukk/SpeedTest" << std::endl;
std::cout << "Author: " << SpeedTest_AUTHOR << std::endl;
}
void usage(const char* name){
std::cerr << "Usage: " << name << " ";
std::cerr << " [--latency] [--quality] [--download] [--upload] [--share] [--help]\n"
" [--test-server host:port] [--output verbose|text|json]\n";
std::cerr << "optional arguments:" << std::endl;
std::cerr << " --help Show this message and exit\n";
std::cerr << " --latency Perform latency test only\n";
std::cerr << " --quality Perform quality test only. It includes latency test\n";
std::cerr << " --download Perform download test only. It includes latency test\n";
std::cerr << " --upload Perform upload test only. It includes latency test\n";
std::cerr << " --share Generate and provide a URL to the speedtest.net share results image\n";
std::cerr << " --insecure Skip SSL certificate verify (Useful for Embedded devices)\n";
std::cerr << " --test-server host:port Run speed test against a specific server\n";
std::cerr << " --quality-server host:port Run line quality test against a specific server\n";
std::cerr << " --output verbose|text|json Set output type. Default: verbose\n";
}
int main(const int argc, const char **argv) {
ProgramOptions programOptions;
if (!ParseOptions(argc, argv, programOptions)){
usage(argv[0]);
return EXIT_FAILURE;
}
if (programOptions.output_type == OutputType::verbose){
banner();
std::cout << std::endl;
}
if (programOptions.help) {
usage(argv[0]);
return EXIT_SUCCESS;
}
signal(SIGPIPE, SIG_IGN);
auto sp = SpeedTest(SPEED_TEST_MIN_SERVER_VERSION);
IPInfo info;
ServerInfo serverInfo;
ServerInfo serverQualityInfo;
if (programOptions.insecure) {
sp.setInsecure(programOptions.insecure);
}
if (programOptions.output_type == OutputType::json)
std::cout << "{";
if (!sp.ipInfo(info)){
std::cerr << "Unable to retrieve your IP info. Try again later" << std::endl;
if (programOptions.output_type == OutputType::json)
std::cout << "\"error\":\"unable to retrieve your ip info\"}" << std::endl;
return EXIT_FAILURE;
}
if (programOptions.output_type == OutputType::verbose){
std::cout << "IP: \"" << info.ip_address << "\""
<< " (\"" << info.isp << "\") "
<< "Location: [" << info.lat << ", " << info.lon << "]" << std::endl;
} else if (programOptions.output_type == OutputType::text) {
std::cout << "IP=" << info.ip_address << std::endl;
std::cout << "IP_LAT=" << info.lat << std::endl;
std::cout << "IP_LON=" << info.lon << std::endl;
std::cout << "PROVIDER=" << info.isp << std::endl;
} else if (programOptions.output_type == OutputType::json) {
std::cout << "\"client\":{";
std::cout << "\"ip\":\"" << info.ip_address << "\",";
std::cout << "\"lat\":\"" << info.lat << "\",";
std::cout << "\"lon\":\"" << info.lon << "\",";
std::cout << "\"isp\":\"" << info.isp << "\"";
std::cout << "},";
}
auto serverList = sp.serverList();
if (programOptions.selected_server.empty()){
if (programOptions.output_type == OutputType::verbose)
std::cout << "Finding fastest server... " << std::flush;
if (serverList.empty()){
std::cerr << "Unable to download server list. Try again later" << std::endl;
if (programOptions.output_type == OutputType::json)
std::cout << "\"error\":\"unable to download server list\"}" << std::endl;
return EXIT_FAILURE;
}
if (programOptions.output_type == OutputType::verbose)
std::cout << serverList.size() << " Servers online" << std::endl;
else if (programOptions.output_type == OutputType::json)
std::cout << "\"servers_online\":\"" << serverList.size() << "\",";
serverInfo = sp.bestServer(10, [&programOptions](bool success) {
if (programOptions.output_type == OutputType::verbose)
std::cout << (success ? '.' : '*') << std::flush;
});
if (programOptions.output_type == OutputType::verbose){
std::cout << std::endl;
std::cout << "Server: " << serverInfo.name
<< " " << serverInfo.host
<< " by " << serverInfo.sponsor
<< " (" << serverInfo.distance << " km from you): "
<< sp.latency() << " ms" << std::endl;
} else if (programOptions.output_type == OutputType::text) {
std::cout << "TEST_SERVER_HOST=" << serverInfo.host << std::endl;
std::cout << "TEST_SERVER_DISTANCE=" << serverInfo.distance << std::endl;
}
else if (programOptions.output_type == OutputType::json) {
std::cout << "\"server\":{";
std::cout << "\"name\":\"" << serverInfo.name << "\",";
std::cout << "\"sponsor\":\"" << serverInfo.sponsor << "\",";
std::cout << "\"distance\":\"" << serverInfo.distance << "\",";
std::cout << "\"latency\":\"" << sp.latency() << "\",";
std::cout << "\"host\":\"" << serverInfo.host << "\"";
std::cout << "},";
}
} else {
serverInfo.host.append(programOptions.selected_server);
sp.setServer(serverInfo);
for (auto &s : serverList) {
if (s.host == serverInfo.host)
serverInfo.id = s.id;
}
if (programOptions.output_type == OutputType::verbose)
std::cout << "Selected server: " << serverInfo.host << std::endl;
else if (programOptions.output_type == OutputType::text) {
std::cout << "TEST_SERVER_HOST=" << serverInfo.host << std::endl;
}
else if (programOptions.output_type == OutputType::json) {
std::cout << "\"server\":{";
std::cout << "\"host\":\"" << serverInfo.host << "\"";
std::cout << "},";
}
}
if (programOptions.output_type == OutputType::verbose)
std::cout << "Ping: " << sp.latency() << " ms." << std::endl;
else if (programOptions.output_type == OutputType::text)
std::cout << "LATENCY=" << sp.latency() << std::endl;
else if (programOptions.output_type == OutputType::json) {
std::cout << "\"ping\":\"";
std::cout << std::fixed;
std::cout << sp.latency() << "\",";
}
long jitter = 0;
if (programOptions.output_type == OutputType::verbose)
std::cout << "Jitter: " << std::flush;
if (sp.jitter(serverInfo, jitter)){
if (programOptions.output_type == OutputType::verbose)
std::cout << jitter << " ms." << std::endl;
else if (programOptions.output_type == OutputType::text)
std::cout << "JITTER=" << jitter << std::endl;
else if (programOptions.output_type == OutputType::json) {
std::cout << "\"jitter\":\"";
std::cout << std::fixed;
std::cout << jitter << "\",";
}
} else {
std::cerr << "Jitter measurement is unavailable at this time." << std::endl;
}
if (programOptions.latency) {
if (programOptions.output_type == OutputType::json)
std::cout << "\"_\":\"only latency requested\"}" << std::endl;
return EXIT_SUCCESS;
}
if (programOptions.output_type == OutputType::verbose)
std::cout << "Determine line type (" << preflightConfigDownload.concurrency << ") " << std::flush;
double preSpeed = 0;
if (!sp.downloadSpeed(serverInfo, preflightConfigDownload, preSpeed, [&programOptions](bool success){
if (programOptions.output_type == OutputType::verbose)
std::cout << (success ? '.' : '*') << std::flush;
})){
std::cerr << "Pre-flight check failed." << std::endl;
if (programOptions.output_type == OutputType::json)
std::cout << "\"error\":\"pre-flight check failed\"}" << std::endl;
return EXIT_FAILURE;
}
if (programOptions.output_type == OutputType::verbose)
std::cout << std::endl;
TestConfig uploadConfig;
TestConfig downloadConfig;
testConfigSelector(preSpeed, uploadConfig, downloadConfig);
if (programOptions.output_type == OutputType::verbose)
std::cout << downloadConfig.label << std::endl;
if (!programOptions.upload){
if (programOptions.output_type == OutputType::verbose){
std::cout << std::endl;
std::cout << "Testing download speed (" << downloadConfig.concurrency << ") " << std::flush;
}
double downloadSpeed = 0;
if (sp.downloadSpeed(serverInfo, downloadConfig, downloadSpeed, [&programOptions](bool success){
if (programOptions.output_type == OutputType::verbose)
std::cout << (success ? '.' : '*') << std::flush;
})){
if (programOptions.output_type == OutputType::verbose){
std::cout << std::endl;
std::cout << "Download: ";
std::cout << std::fixed;
std::cout << std::setprecision(2);
std::cout << downloadSpeed << " Mbit/s" << std::endl;
} else if (programOptions.output_type == OutputType::text) {
std::cout << "DOWNLOAD_SPEED=";
std::cout << std::fixed;
std::cout << std::setprecision(2);
std::cout << downloadSpeed << std::endl;
} else if (programOptions.output_type == OutputType::json) {
std::cout << "\"download\":\"";
std::cout << std::fixed;
std::cout << (downloadSpeed*1000*1000) << "\",";
}
} else {
std::cerr << "Download test failed." << std::endl;
if (programOptions.output_type == OutputType::json)
std::cout << "\"error\":\"download test failed\"}" << std::endl;
return EXIT_FAILURE;
}
}
if (programOptions.download) {
if (programOptions.output_type == OutputType::json)
std::cout << "\"_\":\"only download requested\"}" << std::endl;
return EXIT_SUCCESS;
}
if (programOptions.output_type == OutputType::verbose)
std::cout << "Testing upload speed (" << uploadConfig.concurrency << ") " << std::flush;
double uploadSpeed = 0;
if (sp.uploadSpeed(serverInfo, uploadConfig, uploadSpeed, [&programOptions](bool success){
if (programOptions.output_type == OutputType::verbose)
std::cout << (success ? '.' : '*') << std::flush;
})){
if (programOptions.output_type == OutputType::verbose){
std::cout << std::endl;
std::cout << "Upload: ";
std::cout << std::fixed;
std::cout << std::setprecision(2);
std::cout << uploadSpeed << " Mbit/s" << std::endl;
} else if (programOptions.output_type == OutputType::text) {
std::cout << "UPLOAD_SPEED=";
std::cout << std::fixed;
std::cout << std::setprecision(2);
std::cout << uploadSpeed << std::endl;
} else if (programOptions.output_type == OutputType::json) {
std::cout << "\"upload\":\"";
std::cout << std::fixed;
std::cout << (uploadSpeed*1000*1000) << "\",";
}
} else {
std::cerr << "Upload test failed." << std::endl;
if (programOptions.output_type == OutputType::json)
std::cout << "\"error\":\"upload test failed\"}" << std::endl;
return EXIT_FAILURE;
}
if (programOptions.share){
std::string share_it;
if (sp.share(serverInfo, share_it)) {
if (programOptions.output_type == OutputType::verbose) {
std::cout << "Results image: " << share_it << std::endl;
} else if (programOptions.output_type == OutputType::text) {
std::cout << "IMAGE_URL=" << share_it << std::endl;
} else if (programOptions.output_type == OutputType::json) {
std::cout << "\"share\":\"" << share_it << "\",";
}
}
}
if (programOptions.output_type == OutputType::json)
std::cout << "\"_\":\"all ok\"}" << std::endl;
return EXIT_SUCCESS;
}