-
Notifications
You must be signed in to change notification settings - Fork 1
/
summarize.php
156 lines (135 loc) · 4.26 KB
/
summarize.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
<?php
$options = getopt('f:',['ratio:','stats:']);
$filename = array_key_exists('f', $options) ? $options['f'] : "php://stdin";
if (isset($options['stats'])) {
$column = $options['stats'];
$stats = new StatsCollection('stats', intval($column));
} else if (isset($options['ratio'])) {
$columns = explode(':',$options['ratio']);
$stats = new StatsCollection('ratio',
intval($columns[0]),
intval($columns[1]));
} else {
echo <<<HELP
Aggregate call-site stats recorded by CallSiteStats.php input columns
are expected to be space-delimited. Lines are grouped by column 1.
Usage:
summarize --stats=2 -f stats-input.log
summarize --ratio=3:4 -f stats-input.log
cat stats-input.log | summarize --stats=2
Options:
-f=filename Input filename (if not specified, STDIN is used)
--stats=x Calculate min/max/mean/std .. for a given column x
--ratio=x:y Total and calculate ratio between column x and column y
HELP;
exit(1);
}
$stats->readLines(fopen($filename,'r'));
$stats->printResults();
class StatsCollection {
public function __construct($type, $column1, $column2 = null) {
$this->stats = $type == 'stats';
$this->ratio = $type == 'ratio';
$this->column1 = $column1;
if ($this->ratio) {
$this->column2 = $column2;
}
}
public function readLines($inStream) {
while ($line = fgets($inStream)) {
$this->addLine($line);
}
fclose($inStream);
}
protected function addLine($line) {
$parts = explode(" ", $line);
$lineData = &$this->lines[$parts[0]];
if ($this->stats) {
if ($lineData === null){
$lineData = new StatsStream();
}
$lineData->write($parts[$this->column1]);
} else if ($this->ratio) {
if ($lineData === null){
$lineData = [0,0];
}
$lineData[0] += $parts[$this->column1];
$lineData[1] += $parts[$this->column2];
}
}
public function printResults() {
if ($this->stats) {
$this->printStats();
} else if ($this->ratio) {
$this->printRatios();
}
}
public function printRatios() {
foreach($this->lines as $key => $stats) {
$num = $stats[0];
$den = $stats[1];
$diff = $den - $num;
$pct = round(100 * ($num / $den), 2);
echo "$key diff:{$diff} $num / $den = $pct%\n";
}
}
public function printStats() {
foreach($this->lines as $key => $stats) {
$sum = $stats->sum();
$min = $stats->min();
$max = $stats->max();
$count = $stats->n();
$avg = $stats->mean();
$std = $stats->standard_deviation();
$min = round($min, 3);
$max = round($max, 3);
$sum = round($sum, 3);
$std = round($std, 3);
$avg = round($avg, 3);
echo "$key avg:$avg count:$count sum:$sum std:$std min:$min max:$max\n";
}
}
}
class StatsStream {
public function __construct() {
$this->_min = null;
$this->_max = null;
// number of items seen
$this->_n = 0;
// running mean
$this->_mean = 0;
// running sum of squares deviations from the mean
$this->_ss = 0;
// the running 'actual sum'
$this->_sum = 0;
$this->writable = true;
}
public function write($x) {
$x = floatval($x);
$old_n = $this->_n++;
if ($old_n === 0) {
$this->_min = $x;
$this->_max = $x;
$this->_mean = $x;
$this->_sum = $x;
} else {
if ($x < $this->_min) $this->_min = $x;
if ($x > $this->_max) $this->_max = $x;
$xdiff = $x - $this->_mean;
$this->_ss += $old_n * $xdiff * $xdiff / $this->_n;
$this->_mean += ($x - $this->_mean) / $this->_n;
$this->_sum += $x;
}
}
public function n() { return $this->_n; }
public function min() { return $this->_min; }
public function max() { return $this->_max; }
public function sum() { return $this->_sum; }
public function mean() { return $this->_mean; }
public function variance() {
return $this->_ss / $this->_n;
}
public function standard_deviation() {
return sqrt($this->variance());
}
}