-
Notifications
You must be signed in to change notification settings - Fork 4
/
generator.php
180 lines (128 loc) · 4.35 KB
/
generator.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
<?php
date_default_timezone_set('UTC');
/**
* Allow benchmarking to use as much memory as it needs, if available.
*/
ini_set('memory_limit', -1);
if ( ! extension_loaded('ds')) {
die('ds extension either not installed or not enabled');
}
/**
*
*/
$config = require "config.php";
/**
* Benchmark schedule: comment out those that should not be run.
*/
$schedule = require "schedule.php";
/**
* Benchmarks the total time taken, where snapshots are taken at exponential
* intervals and averaged over multiple trials.
*/
function benchmark($type, $range, $setup, $tick, $reset)
{
$results = [
'm' => [], // Memory usage
't' => [], // Time taken
];
list($initial, $total) = $range;
$initial = max(1, $initial);
switch ($type) {
// Expontential benchmark performs a single task at an expontentially
// increasing checkpoint.
case EXPONENTIAL:
$checkpoints = [];
for ($i = $initial; $i <= $total; $i *= 2) {
$checkpoints[] = $i;
}
break;
// Incremental benchmark performs a task at every tick in the range.
case INCREMENTAL:
$checkpoints = range(
$initial, $total, max(1, 2 ** intval(round(log($total / SAMPLES, 2))))
);
break;
}
// Initialize results
foreach ($checkpoints as $checkpoint) {
$results['m'][$checkpoint] = 0;
$results['t'][$checkpoint] = 0;
}
switch ($type) {
case EXPONENTIAL:
foreach ($checkpoints as $checkpoint) {
echo '.';
for ($trial = 0; $trial < TRIALS; $trial++) {
// Just to make sure that resources are cleared beforehand.
$reset();
gc_collect_cycles();
srand(1);
$memory = memory_get_usage();
$setup($checkpoint);
$time = microtime(true);
$tick($checkpoint);
//
$results['t'][$checkpoint] += microtime(true) - $time;
$results['m'][$checkpoint] += memory_get_usage() - $memory;
}
$reset();
gc_collect_cycles();
}
break;
case INCREMENTAL:
for ($trial = 0; $trial < TRIALS; $trial++) {
echo '.';
$reset();
gc_collect_cycles();
srand(1);
$memory = memory_get_usage();
$setup($total);
$time = microtime(true);
for ($i = 1, $c = 0; $i <= $total; $i++) {
$tick($i - 1);
if ($i === $checkpoints[$c]) {
$results['t'][$i] += microtime(true) - $time;
$results['m'][$i] += memory_get_usage() - $memory;
if (++$c === count($checkpoints)) {
break;
}
}
}
}
break;
}
// Determine the average memory usage across all trials.
foreach ($results['m'] as &$result) {
$result /= TRIALS;
}
// Determine the average runtime across all trials.
foreach ($results['t'] as &$result) {
$result /= TRIALS;
}
return $results;
}
$results = [];
echo "Generating benchmarks...\n";
// Run each benchmark in the schedule.
foreach ($schedule as $name => $batches) {
$results[$name] = $results[$name] ?? [];
foreach ($batches as $batchId => $batch) {
$results[$name][$batchId] = [];
list($range, $candidates) = $batch;
if (count($candidates) === 0) {
continue;
}
echo "\n\t$name:";
foreach ($candidates as $candidate) {
echo "\n\t\t$candidate ";
list($type, $functions) = $config[$name];
$functions = $functions[$candidate];
// Benchmark this task according to its setup, tick and reset functions.
$results[$name][$batchId][$candidate] = benchmark($type, $range, ...$functions);
}
echo "\n";
}
}
// This will be the path that the reporter will use.
$results_path = __DIR__ . '/results.json';
file_put_contents($results_path, json_encode($results, JSON_PRETTY_PRINT));