-
Notifications
You must be signed in to change notification settings - Fork 0
/
speedtest.php
251 lines (221 loc) · 7.67 KB
/
speedtest.php
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
<?php
namespace RZFuhrmann;
if (!class_exists('RZFuhrmann\Speedtest')) {
class Speedtest {
private $source_address = null;
private $last_result;
private $debug = false;
public function __construct ($opts = array()){
$this->applyOpts($opts);
}
public function setOpt($opt, $value){
$this->applyOpts(array($opt => $value));
}
private function applyOpts($opts = array()){
if (isset($opts['source_address'])){
$this->source_address = $opts['source_address'];
}
if (isset($opts['debug']) && is_bool($opts['debug'])){
$this->debug = $opts['debug'];
}
}
private function getOpts(){
return array(
'source_address' => $this->source_address,
'debug' => $this->debug,
);
}
public function test(){
$result = array();
// getting configuration
$this->logtxt('Getting speedtest configuration...');
$result['config'] = $this->getConfig();
$result['options'] = $this->getOpts();
if (!$result['config']){
$this->logtxt('Error fetching speedtest configuration!', 'error');
} else {
// getting active servers
$this->logtxt('Getting active speedtest servers...');
$servers = $this->getServers();
if (!$servers){
$this->logtxt('Error fetching server list!', 'error');
} else {
// search nearest server
$this->logtxt('Calculating nearest server based on its position...');
$result['server'] = $this->getNearestServer($servers, array('lat' => $result['config']['client']['lat'], 'lon' => $result['config']['client']['lon']));
if (!$result['server']){
$this->logtxt('Error calculating nearest server!', 'error');
} else {
// latency
$this->logtxt("Testing latency...");
$latencies = array();
for ($i = 0; $i < 3; $i++){
$url = $result['server']['url'].'/latency.txt?x='.(microtime(true)*1000).'.'.$i;
$info = $this->getHTTPInfo($url);
if ($info && $info["connect_time"]){
$latencies[] = $info["connect_time"];
} else {
$i -= 1;
}
}
$result['latency'] = round((array_sum($latencies)/count($latencies))*1000, 2);
$this->logtxt('Latency: '.$result['latency'].' ms');
// download speed
// TODO: Threading!
$this->logtxt("Testing download speed...");
$speeds = array();
// testing sizes based on first results? These sizes are fine for VDSL 100 connections...
foreach (array(3000, 3500, 4000, 6000) as $size){
for ($i = 0; $i < $result['config']['download']['threadsperurl']; $i++){
// TODO: each $i in an own thread!
$url = str_replace('upload.php','',$result['server']['url']).'random'.$size.'x'.$size.'.jpg';
$info = $this->getHTTPInfo($url);
if ($info && $info['speed_download']){
$speeds[] = $info['speed_download'];
}
}
}
$result['download_max'] = round($this->bytes2mbit(max($speeds)), 2);
$result['download_min'] = round($this->bytes2mbit(min($speeds)), 2);
$result['download'] = round($this->bytes2mbit(array_sum($speeds)/count($speeds)), 2);
}
}
}
$this->last_result = $result;
return $result;
}
private function bytes2mbit($bytes){
return $bytes * 8 / 1024 / 1024;
}
public function getResult($type = "json"){
switch ($type){
case 'json':
return json_encode($this->last_result, JSON_PRETTY_PRINT);
break;
default:
$this->logtxt('Unknown result type!', 'error');
}
}
private function LatLon2Distance ($pos1, $pos2){
$R = 6371e3;
$phi1 = deg2rad($pos1["lat"]);
$phi2 = deg2rad($pos2["lat"]);
$dphi = deg2rad($pos2["lat"]-$pos1["lat"]);
$dlambda = deg2rad($pos2["lon"]-$pos1["lon"]);
$a = sin($dphi/2) * sin($dphi/2) +
cos($phi1) * cos($phi2) *
sin($dlambda/2) * sin($dlambda/2);
$c = 2 * atan2(sqrt($a), sqrt(1-$a));
return $R * $c;
}
private function getNearestServer($serverlist, $latlong){
$min_dist = null; $min_server = null;
foreach ($serverlist as $server){
$dist = $this->LatLon2Distance($server, $latlong);
if (!$min_dist || $dist < $min_dist){
$min_dist = $dist;
$min_server = $server;
}
}
return $min_server;
}
/**
* Get HTTP info
*/
private function getHTTPInfo($url){
return $this->getHTML($url, true);
}
/**
* Makes a simple GET request to a given URL.
*/
private function getHTML($url, $return_info = false){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
if ($this->source_address) curl_setopt($ch, CURLOPT_INTERFACE, $this->source_address);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36');
$raw = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) != 200){
return false;
}
$info = curl_getinfo($ch);
curl_close($ch);
if ($return_info) return $info;
return $raw;
}
/**
* Returns an DOMDocument of an downloaded XML
*/
private function getXML($url){
$raw = $this->getHTML($url);
$xml = new \DOMDocument();
$xml->loadXML($raw);
return $xml;
}
/**
* Get Speedtest configuration from http://www.speedtest.net/speedtest-config.php
*/
private function getConfig(){
$config = array(
'client' => null
);
$configxml = $this->getXML('http://www.speedtest.net/speedtest-config.php');
if ($client = $configxml->getElementsByTagName('client')[0]){
$config['client'] = array(
'ip' => $client->getAttribute('ip'),
'lat' => floatval($client->getAttribute('lat')),
'lon' => floatval($client->getAttribute('lon')),
'isp' => $client->getAttribute('isp'),
'isprating' => $client->getAttribute('isprating'),
'country' => $client->getAttribute('country'),
// 'rating' => $client->getAttribute('rating'),
// 'ispdlavg' => $client->getAttribute('ispdlavg'),
// 'ispulavg' => $client->getAttribute('ispulavg'),
);
}
if ($serverconfig = $configxml->getElementsByTagName('server-config')->item(0)){
$config['serverconfig'] = array();
foreach ($serverconfig->attributes as $att){
$config['serverconfig'][$att->nodeName] = $att->nodeValue;
}
}
if ($download = $configxml->getElementsByTagName('download')->item(0)){
$config['download'] = array();
foreach ($download->attributes as $att){
$config['download'][$att->nodeName] = $att->nodeValue;
}
}
return $config;
}
/**
* Get all servers available at http://www.speedtest.net/speedtest-servers-static.php
*/
private function getServers(){
$xml = $this->getXML("http://www.speedtest.net/speedtest-servers-static.php");
$servers = $xml->getElementsByTagName("server");
$serverlist = array();
foreach ($servers as $server){
$this_server = array(
'url' => $server->getAttribute("url"),
'lat' => floatval($server->getAttribute("lat")),
'lon' => floatval($server->getAttribute("lon")),
'name' => $server->getAttribute("name"),
'country' => $server->getAttribute("country"),
'cc' => $server->getAttribute("cc"),
'sponsor' => $server->getAttribute("sponsor"),
'id' => $server->getAttribute("id"),
'host' => $server->getAttribute("host"),
);
$serverlist[] = $this_server;
}
return $serverlist;
}
private function logtxt($txt, $lvl = "info"){
if ($this->debug || $lvl == 'error'){
echo '['.date("H:i:s").'] '.$txt."\n";
}
}
}
}
?>