Skip to content

Commit e80ad46

Browse files
committed
measure run phases/iterations individually
1 parent e429641 commit e80ad46

File tree

2 files changed

+39
-34
lines changed

2 files changed

+39
-34
lines changed

benchmark/benchmark.php

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ function runValgrindPhpCgiCommand(
120120

121121
$profileOut = __DIR__ . "/profiles/callgrind.out.$name";
122122
if ($jit) {
123-
$profileOut .= '.jit';
123+
$profileOut .= '-jit';
124124
}
125125

126126
$process = runCommand([
@@ -138,12 +138,32 @@ function runValgrindPhpCgiCommand(
138138
'-d opcache.validate_timestamps=0',
139139
...$args,
140140
]);
141-
$totals = extractTotalsFromCallgrindFile($profileOut);
142-
$instructions = $totals['Ir'];
143-
if ($repeat > 1) {
144-
$instructions = gmp_strval(gmp_div_q($instructions, $repeat));
141+
142+
// collect metrics for startup, each benchmark run and shutdown
143+
$totalsAll = [];
144+
foreach (['startup' => 1, ...range(2, $repeat + 1), 'shutdown' => ''] as $phase => $fileSuffix) {
145+
$profileOutSpecific = $profileOut . '.' . $phase;
146+
if (!rename($profileOut . ($fileSuffix === '' ? '' : '.' . $fileSuffix), $profileOutSpecific)) {
147+
throw new \Exception('Expected callgrind file "' . $profileOutSpecific . '" does not exist');
148+
}
149+
150+
$totalsAll[$phase] = extractTotalsFromCallgrindFile($profileOutSpecific);
145151
}
146-
return ['instructions' => $instructions];
152+
153+
// mimic original logged "instructions" meaning:
154+
// - the startup was not counted (only if repeats > 1)
155+
// - repeats were counted without warmup, ie. only the last 50% repeats were counted
156+
// - the shutdown was never counted
157+
$warmTotals = array_map(static fn () => '0', array_first($totalsAll));
158+
foreach ($repeat === 1 ? ['startup', 0] : range(intdiv($repeat, 2), $repeat - 1) as $phase) {
159+
foreach ($totalsAll[$phase] as $kEvent => $v) {
160+
$warmTotals[$kEvent] = gmp_strval(gmp_add($warmTotals[$kEvent], $v));
161+
}
162+
}
163+
$instructions = gmp_strval(gmp_div_q($warmTotals['Ir'], intdiv($repeat + 1, 2)));
164+
$res = ['instructions' => $instructions];
165+
166+
return $res;
147167
}
148168

149169
/**

sapi/cgi/cgi_main.c

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2233,17 +2233,6 @@ consult the installation file that came with this distribution, or visit \n\
22332233
if (comma) {
22342234
warmup_repeats = atoi(php_optarg);
22352235
repeats = atoi(comma + 1);
2236-
#ifdef HAVE_VALGRIND
2237-
if (warmup_repeats > 0) {
2238-
CALLGRIND_STOP_INSTRUMENTATION;
2239-
/* We're not interested in measuring startup */
2240-
CALLGRIND_ZERO_STATS;
2241-
# ifdef HAVE_VALGRIND_CACHEGRIND_H
2242-
CACHEGRIND_STOP_INSTRUMENTATION;
2243-
/* Zeroing stats is not supported for cachegrind. */
2244-
# endif
2245-
}
2246-
#endif
22472236
} else {
22482237
repeats = atoi(php_optarg);
22492238
}
@@ -2285,6 +2274,13 @@ consult the installation file that came with this distribution, or visit \n\
22852274
}
22862275
#endif
22872276
while (!fastcgi || fcgi_accept_request(request) >= 0) {
2277+
#ifdef HAVE_VALGRIND
2278+
if (benchmark) {
2279+
/* measure startup and each benchmark run separately */
2280+
CALLGRIND_DUMP_STATS;
2281+
}
2282+
#endif
2283+
22882284
SG(server_context) = fastcgi ? (void *)request : (void *) 1;
22892285
init_request_info(request);
22902286

@@ -2443,15 +2439,6 @@ consult the installation file that came with this distribution, or visit \n\
24432439
}
24442440
} /* end !cgi && !fastcgi */
24452441

2446-
#ifdef HAVE_VALGRIND
2447-
if (warmup_repeats == 0) {
2448-
CALLGRIND_START_INSTRUMENTATION;
2449-
# ifdef HAVE_VALGRIND_CACHEGRIND_H
2450-
CACHEGRIND_START_INSTRUMENTATION;
2451-
# endif
2452-
}
2453-
#endif
2454-
24552442
/* request startup only after we've done all we can to
24562443
* get path_translated */
24572444
if (php_request_startup() == FAILURE) {
@@ -2570,14 +2557,6 @@ consult the installation file that came with this distribution, or visit \n\
25702557
SG(request_info).query_string = NULL;
25712558
}
25722559

2573-
#ifdef HAVE_VALGRIND
2574-
/* We're not interested in measuring shutdown */
2575-
CALLGRIND_STOP_INSTRUMENTATION;
2576-
# ifdef HAVE_VALGRIND_CACHEGRIND_H
2577-
CACHEGRIND_STOP_INSTRUMENTATION;
2578-
# endif
2579-
#endif
2580-
25812560
if (!fastcgi) {
25822561
if (benchmark) {
25832562
if (warmup_repeats) {
@@ -2605,6 +2584,12 @@ consult the installation file that came with this distribution, or visit \n\
26052584
script_file = NULL;
26062585
goto do_repeat;
26072586
}
2587+
2588+
#ifdef HAVE_VALGRIND
2589+
/* measure shutdown separately */
2590+
CALLGRIND_DUMP_STATS;
2591+
#endif
2592+
26082593
break;
26092594
}
26102595

0 commit comments

Comments
 (0)