Skip to content

Commit d6365d2

Browse files
martinsikdavidwdan
authored andcommitted
log memory usage
1 parent 83ea0cf commit d6365d2

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

benchmark/run.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
$files = array_merge($files, glob($fileOrDir . '/*.php'));
2929
} else {
3030
// Force absolute path
31-
$files[] = $file[0] === DIRECTORY_SEPARATOR ? $file : $_SERVER['PWD'] . DIRECTORY_SEPARATOR . $file;
31+
$files[] = $fileOrDir[0] === DIRECTORY_SEPARATOR ? $fileOrDir : $_SERVER['PWD'] . DIRECTORY_SEPARATOR . $fileOrDir;
3232
}
3333
}
3434
}
@@ -37,7 +37,7 @@
3737
Observable::just($files)
3838
->doOnNext(function(array $files) {
3939
printf("Benchmarking %d file/s (min %ds each)\n", count($files), MIN_TOTAL_DURATION);
40-
printf("script_name - total_runs (single_run_mean ±standard_deviation)\n");
40+
printf("script_name - total_runs (single_run_mean ±standard_deviation) - mem_start [mem_100_iter] mem_end\n");
4141
printf("==============================================================\n");
4242
})
4343
->concatMap(function($files) { // Flatten the array
@@ -63,6 +63,8 @@ function () { }
6363
throw new Exception("Unable to load file \"$file\"");
6464
}
6565

66+
$memoryUsage = [memory_get_usage()];
67+
6668
while ($totalDuration < MIN_TOTAL_DURATION) {
6769
$start = microtime(true);
6870

@@ -72,13 +74,20 @@ function () { }
7274

7375
$durations[] = $duration * 1000;
7476
$totalDuration += $duration;
77+
78+
if (count($durations) === 100) {
79+
$memoryUsage[] = memory_get_usage();
80+
}
7581
}
7682

83+
$memoryUsage[] = memory_get_usage();
84+
7785
ob_end_clean();
7886

7987
return [
8088
'file' => $file,
8189
'durations' => $durations,
90+
'memory_usage' => $memoryUsage,
8291
];
8392
})
8493
->doOnNext(function(array $result) { // Print the number of successful runs
@@ -94,13 +103,18 @@ function () { }
94103

95104
return [
96105
'file' => $result['file'],
106+
'memory_usage' => $result['memory_usage'],
97107
'mean' => $mean,
98108
'standard_deviation' => pow($variance / $count, 0.5),
99109
];
100110
})
101111
->subscribe(new CallbackObserver(
102112
function(array $result) {
103-
printf(" (%.2fms ±%.2fms)\n", $result['mean'], $result['standard_deviation']);
113+
printf(" (%.2fms ±%.2fms) - ", $result['mean'], $result['standard_deviation']);
114+
foreach ($result['memory_usage'] as $memory) {
115+
printf("%.2fMB ", $memory / pow(10, 6));
116+
}
117+
printf("\n");
104118
},
105119
function(\Exception $error) {
106120
printf("\nError: %s\n", $error->getMessage());

0 commit comments

Comments
 (0)