forked from facebookarchive/oss-performance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_to_csv.php
128 lines (110 loc) · 3.08 KB
/
json_to_csv.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
<?hh
/*
* Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
newtype Sample = shape(
'framework' => string,
'runtime' => string,
'rps' => float,
);
newtype Row = shape(
'framework' => string,
'runtime' => string,
'rps_samples' => Vector<float>,
'rps_mean' => ?float,
'rps_sd' => ?float,
);
function load_file(string $file): Vector<Sample> {
$json = file_get_contents($file);
$data = json_decode($json, /* as associative array = */ true);
$results = Vector {};
foreach ($data as $framework => $framework_data) {
$results->addAll(load_framework($framework, $framework_data));
}
return $results;
}
function load_framework(string $framework, $data): Vector<Sample> {
$results = Vector {};
foreach ($data as $runtime => $runtime_data) {
$results[] = load_run($framework, $runtime, $runtime_data['Combined']);
}
return $results;
}
function load_run(string $framework, string $runtime, $data): Sample {
return shape(
'framework' => $framework,
'runtime' => $runtime,
'rps' => $data['Siege RPS'],
);
}
function load_files(Vector<string> $files): Vector<Row> {
$rows_by_key = Map {};
foreach ($files as $file) {
$samples = load_file($file);
foreach ($samples as $sample) {
$key = $sample['framework']."\0".$sample['runtime'];
if (!$rows_by_key->containsKey($key)) {
$rows_by_key[$key] = shape(
'framework' => $sample['framework'],
'runtime' => $sample['runtime'],
'rps_samples' => Vector { },
'rps_mean' => null,
'rps_sd' => null,
);
}
$rows_by_key[$key]['rps_samples'][] = $sample['rps'];
}
}
foreach ($rows_by_key as $key => $row) {
$samples = $row['rps_samples'];
$count = count($samples);
// toArray(): https://github.com/facebook/hhvm/issues/5454
$mean = (float) array_sum($samples->toArray()) / $count;
$variance = array_sum(
$samples->map($x ==> pow($mean - $x, 2))->toArray()
) / $count;
$sd = sqrt($variance);
$row['rps_mean'] = $mean;
$row['rps_sd'] = $sd;
$rows_by_key[$key] = $row;
}
return $rows_by_key->values();
}
function dump_csv(Vector<Row> $rows): void {
$header = Vector {
'Framework',
'Runtime',
'Mean RPS',
'RPS Standard Deviation',
};
$max_sample_count = max($rows->map($row ==> count($row['rps_samples'])));
for ($i = 1; $i <= $max_sample_count; ++$i) {
$header[] = 'Sample '.$i.' RPS';
}
fputcsv(STDOUT, $header);
foreach ($rows as $row) {
$out = Vector {
$row['framework'],
$row['runtime'],
$row['rps_mean'],
$row['rps_sd'],
};
$out->addAll($row['rps_samples']);
fputcsv(STDOUT, $out);
}
}
function main(Vector<string> $argv) {
$files = clone $argv;
$files->removeKey(0);
if ($files->isEmpty()) {
fprintf(STDERR, "Usage: %s results.json [results2.json ...]\n", $argv[0]);
exit(1);
}
dump_csv(load_files($files));
}
main(new Vector($argv));