Core\Debug::startProfile
Core\Debug::startProfile(string $path): voidCore\Debug::startProfile( string $path, ): void
Starts timing every call your program makes, until Core\Debug::stopProfile.
Core\Debug::startProfile writes the result to the file you name, in the speedscope format. You can open that file in the speedscope viewer to see which calls took the most time.
The configuration must allow a profile. [debug] mode must include "profile", and [capabilities.debug] profile must include the folder of the file. Permission to write files is not enough. Otherwise startProfile throws a RuntimeError. A second startProfile before the stop throws a LogicError.
With [debug] store on, nvs debug opens a web page that lists each profile. It shows the file and line where startProfile was called. The profiles started at the same line are listed together.
Good to know: timing a call adds a small cost to it. So code with many small calls looks slower in the profile than it really is.
The examples below profile a report, show that only one profile runs at a time, and show the two settings that a profile needs.
Parameters
$pathstringThe file to write. The capabilitydebug.profilemust grant this path. Permission to write files is not enough.
Return value
Returns void. Nothing. The profile goes on until stopProfile.
Errors
RuntimeError[debug] modeinnvs.tomldoes not include the name this method needs.RuntimeErrorThe configuration does not grantdebug.profilefor this path.LogicErrorA profile started bystartProfileis already running.
Changelog
| Version | Change |
|---|---|
0.0.6 | With [debug] store = true, each profile and trace is recorded with the file and line that started it. The Debug page of the debug viewer lists them. |
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 a slow report
<?nvs// Times every call of a sales report. The profile shows which calls take the most time.
class Report{ public static function sales(array<int> $orders): int { int $sum = 0; foreach ($orders as int $cents) { $sum = $sum + Report::withShipping($cents); } return $sum; }
public static function withShipping(int $cents): int { return $cents + 490; }}
// The profile file goes in the folder of this program. `nvs.toml` allows a profile there.string $file = Core\Path::thisDirectory('report.speedscope.json');
// Every call between the start and the stop is timed.Core\Debug::startProfile($file);int $total = Report::sales([1000, 2500, 400]);Core\Debug::stopProfile();
echo 'total with shipping: ', $total, "\n";
// This deletes the profile file, so the next run starts with an empty folder.if (Core\IO::exists($file)) { Core\IO::remove($file);}total with shipping: 5370One profile at a time
<?nvs// Shows that only one profile can run at a time.
string $first = Core\Path::thisDirectory('import.speedscope.json');string $second = Core\Path::thisDirectory('export.speedscope.json');
Core\Debug::startProfile($first);echo "profile of the import started\n";
// A profile is already running, so this start throws a `LogicError`.try { Core\Debug::startProfile($second);} catch (LogicError $running) { echo "not allowed: stop the first profile before you start another\n";}
// After the stop, a new profile can start.Core\Debug::stopProfile();Core\Debug::startProfile($second);echo "profile of the export started\n";Core\Debug::stopProfile();
// This deletes the profile files, so the next run starts with an empty folder.array<string> $files = [$first, $second];foreach ($files as string $file) { if (Core\IO::exists($file)) { Core\IO::remove($file); }}profile of the import startednot allowed: stop the first profile before you start anotherprofile of the export startedWhat the configuration must allow
<?nvs// Shows the two settings in `nvs.toml` that a profile needs.
// A temporary folder is not in `[capabilities.debug] profile`, so this start throws a `RuntimeError`.// A program run with `nvs run` may write files there, but that is not enough for a profile.try { Core\Debug::startProfile(Core\Path::join(Core\IO::createTemporaryDirectory(), 'app.speedscope.json')); echo "profile started\n";} catch (RuntimeError $denied) { echo "not allowed: the folder is not in [capabilities.debug] profile\n";}
// `[debug] mode` here has `"profile"` and not `"trace"`. So a trace cannot start.try { Core\Debug::startTrace(Core\Path::thisDirectory('app.jsonl')); echo "trace started\n";} catch (RuntimeError $notInMode) { echo "not allowed: [debug] mode does not include \"trace\"\n";}
// The folder of this program is allowed, and the mode has `"profile"`. So this profile starts.string $file = Core\Path::thisDirectory('app.speedscope.json');Core\Debug::startProfile($file);echo "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);}not allowed: the folder is not in [capabilities.debug] profilenot allowed: [debug] mode does not include "trace"profile started