Core\Debug::stopProfile
Core\Debug::stopProfile(): voidStops the profile that Core\Debug::startProfile started, and closes its file.
Calls that run after Core\Debug::stopProfile are not timed. So you can profile one slow step of a program and leave out the rest. After the stop, you can start a new profile with another file, for example one file for each step.
When no profile is running, stopProfile does nothing. So you can call it in a finally block, and the profile stops even when a step throws an error.
The examples below profile one step of an upload, stop a profile in a finally block, and write one profile for each step of a job.
Return value
Returns void. Nothing. It does nothing when no profile is running.
Examples
The examples below run with this configuration file.
# The examples write a profile of their calls to a file in this folder.# `mode` allows a program to start a profile.[debug]mode = ["profile"]
# The program may write a profile file in this folder, and nowhere else.# A path in `[capabilities]` is relative to this file.[capabilities.debug]profile = ["."]Profile only the slow step
<?nvs// Profiles only the step of an upload that resizes images, and stops before the rest.
class Upload{ // A real upload makes each image wider than 800 pixels smaller here. This one only counts them. public static function resize(array<int> $widths): int { int $done = 0; foreach ($widths as int $width) { if ($width > 800) { $done = $done + 1; } } return $done; }
public static function notify(int $count): string { return $count . ' images resized'; }}
string $file = Core\Path::thisDirectory('resize.speedscope.json');
// Only the calls between the start and the stop are timed. `notify` runs after the stop.Core\Debug::startProfile($file);int $resized = Upload::resize([1200, 640, 2048]);Core\Debug::stopProfile();
echo Upload::notify($resized), "\n";
// This deletes the profile file, so the next run starts with an empty folder.if (Core\IO::exists($file)) { Core\IO::remove($file);}2 images resizedStop even when a step fails
<?nvs// Stops a profile in a `finally` block, so the profile ends even when a step throws an error.
class Import{ public static function row(string $line): int { if ($line == '') { throw new RuntimeError('an empty row'); } return 1; }}
string $file = Core\Path::thisDirectory('import.speedscope.json');int $imported = 0;
Core\Debug::startProfile($file);try { $imported = $imported + Import::row('ada,london'); $imported = $imported + Import::row('');} catch (RuntimeError $error) { echo 'failed: ', $error->message, "\n";} finally { Core\Debug::stopProfile();}
echo 'imported: ', $imported, "\n";
// The profile stopped in `finally`, so a new profile can start now.Core\Debug::startProfile($file);echo "a new profile started\n";Core\Debug::stopProfile();
// This deletes the profile file, so the next run starts with an empty folder.if (Core\IO::exists($file)) { Core\IO::remove($file);}failed: an empty rowimported: 1a new profile startedOne profile for each step
<?nvs// Writes one profile for each step of a backup job, so you can compare the steps.
class Backup{ public static function collect(): int { return 40; }
public static function compress(int $files): int { return $files * 3; }}
string $collectFile = Core\Path::thisDirectory('collect.speedscope.json');string $compressFile = Core\Path::thisDirectory('compress.speedscope.json');
// Each step has its own start and stop, and its own file.Core\Debug::startProfile($collectFile);int $files = Backup::collect();Core\Debug::stopProfile();
Core\Debug::startProfile($compressFile);int $parts = Backup::compress($files);Core\Debug::stopProfile();
// No profile is running now, so this stop does nothing.Core\Debug::stopProfile();
echo 'collected ', $files, ' files into ', $parts, " parts\n";
// This deletes the profile files, so the next run starts with an empty folder.array<string> $profiles = [$collectFile, $compressFile];foreach ($profiles as string $profile) { if (Core\IO::exists($profile)) { Core\IO::remove($profile); }}collected 40 files into 120 parts