-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_speedtest.php
120 lines (114 loc) · 3.36 KB
/
my_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
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Workerman\Timer;
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http\Request;
use Workerman\Protocols\Http\Response;
$nodes = [
'ams.lg.v.ps',
'lon.lg.v.ps',
'fra.lg.v.ps',
'nrt.lg.v.ps',
'kix.lg.v.ps',
];
function Cron_Run()
{
$result = '';
global $global, $nodes;
for ($i = 1; $i < count($nodes); $i++) {
$crazy = new CrazySpeedTest($nodes[$i], 'ipv4');
$result .= $crazy->return;
$crazy = new CrazyMTRRouteTest($nodes[$i], 4);
$result .= $crazy->return;
}
$global->speedtest_last_result = $result;
}
$cron_worker = new Worker();
$cron_worker->name = 'speedtest_cron';
$cron_worker->onWorkerStart = function (Worker $worker) {
global $global;
$global = new \GlobalData\Client('127.0.0.1:2207');
Cron_Run();
Timer::add(
600,
function () {
Cron_Run();
}
);
echo "Cron started.\n";
};
$web_worker = new Worker('http://100.72.128.74:2401');
$web_worker->name = 'speedtest_web';
$web_worker->onWorkerStart = function (Worker $worker) {
global $global;
$global = new \GlobalData\Client('127.0.0.1:2207');
};
$web_worker->onMessage = function (TcpConnection $connection, Request $request) {
global $global;
switch ($request->path()) {
case '/metrics':
$connection->close($global->speedtest_last_result);
break;
default:
$connection->close(new Response(404, [], '404 not found'));
}
};
class CrazySpeedTest
{
private $url = 'https://%s/100MB.test';
public $result = array();
public $return = '';
public function __construct($node, $ip_version)
{
$this->url = sprintf($this->url, $node);
$cmd = sprintf('/usr/local/bin/my_speedtest %s %s', $this->url, $ip_version);
exec($cmd, $exec_result, $return);
//var_dump($cmd, $exec_result);
if ($return == 0) {
$temp = json_decode($exec_result[0], true);
//var_dump($temp);
if (!$temp['speed'])
$temp['speed'] = 0;
$this->return .= sprintf(
'CrazySpeedTest{node="%s", ip_version="%s"} %s' . "\n",
$node,
$ip_version,
$temp['speed']
);
var_dump($this->return);
}
}
}
class CrazyMTRRouteTest
{
private $cmd = 'mtr %s -%d -z -c5 -j';
public $result = array();
public $return = '';
public function __construct($hostname, $ip_version)
{
$cmd = sprintf($this->cmd, $hostname, $ip_version);
exec($cmd, $exec_result, $return);
$json = implode('', $exec_result);
$asn = [];
//var_dump($cmd, $exec_result, $json);
if ($return == 0) {
$temp = json_decode($json, true);
var_dump($temp);
foreach ($temp['report']['hubs'] as $arrnow) {
if ($arrnow['ASN'] != 'AS???') {
$asn[] = $arrnow['ASN'];
}
}
$asn = array_unique($asn);
$this->return = sprintf(
'CrazyASNTest{target="%s", ip_version="%s"} %s' . "\n",
$hostname,
$ip_version,
implode('->', $asn)
);
var_dump($this->return);
}
}
}
Worker::runAll();