-
Notifications
You must be signed in to change notification settings - Fork 0
/
meter.php
124 lines (105 loc) · 3.2 KB
/
meter.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
<?php
include("config.inc.php");
$jj = new JJMeter($options);
$jj->run();
class JJMeter
{
function __construct($options)
{
$this->options = $options;
}
function run()
{
print "****\n";
foreach ($this->options["pages"] as $url) {
$this->benchPage($url);
print "****\n";
}
}
function benchPage($url)
{
$ch = $this->getCurl($url);
$linecount = $this->getLineCountLog();
$body = curl_exec($ch);
// test for redirects
$effUrl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
if ($effUrl == $this->options['loginGet']) {
curl_close($ch);
$this->login();
return $this->benchPage($url);
}
if ($effUrl != $url) {
//do it again to have a clean plate
curl_close($ch);
return $this->benchPage($effUrl);
}
$info = curl_getinfo($ch);
print "TEST for $effUrl\n";
print "HTTP Code : " . $info['http_code'] . "\n";
print "Body size : " . strlen($body) . " bytes\n";
print "Request time : " . round($info["total_time"] * 1000) . " ms\n";
print "Total JR Requests : " . ($this->getLineCountLog() - $linecount) . "\n";
$types = array();
foreach ($this->getLastLinesFromLog($linecount) as $line) {
$fields = explode(" ", $line);
$method = trim($fields[7], '"');
if (!isset($types[$method])) {
$types[$method] = 1;
} else {
$types[$method]++;
}
}
ksort($types);
foreach ($types as $name => $count) {
print " " . $name . str_repeat(" ", 17 - strlen($name)) . ": " . $count . "\n";
}
return $body;
}
function login()
{
$ch = $this->getCurl($this->options['loginGet']);
curl_exec($ch);
$ch = $this->getCurl($this->options['loginPost']);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($this->options['loginParams']));
curl_exec($ch);
curl_close($ch);
}
function getLogHandle()
{
return fopen($this->options['jackrabbitDir'] . "/log/access.log." . date_format(new DateTime(), "Y-m-d"), "r");
}
function getLineCountLog()
{
$log = $this->getLogHandle();
$count = 0;
while (fgets($log)) {
$count++;
}
fclose($log);
return $count;
}
function getLastLinesFromLog($from)
{
$log = $this->getLogHandle();
$count = 0;
while (fgets($log) && $count < $from - 1) {
$count++;
}
$lines = array();
while ($l = fgets($log)) {
$lines[] = $l;
}
return $lines;
}
function getCurl($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/cookieFileName");
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, "/tmp/cookieFileName");
return $ch;
}
}