Skip to content

Profiling

nvs run --profile and nvs test --profile measure how long each function call takes. You do not change your program for this.

When the run ends, Novis writes a profile file in the current folder. --profile callgrind writes callgrind.out.<pid>, with a total time for each function and a list of who called it. You open it in KCachegrind or Webgrind. --profile speedscope writes profile.<pid>.speedscope.json, with every call on a timeline. You open it in speedscope or in VS Code. --profile-out <file> writes the profile to another file.

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 one part of a program with Core\Debug::startProfile, which writes the same speedscope file. They read the file back and print which functions ran, how often, and in what order.

Examples

The examples below run with this configuration file.

nvs.toml
# 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 = ["."]

Find the function that runs most

<?nvs
// Counts how many times each function runs in an order import, to find the one that runs most.
class Import
{
// Each order is the country it ships to.
public static function run(array<string> $countries): int
{
int $shipping = 0;
foreach ($countries as string $country) {
$shipping = $shipping + Import::shippingFor($country);
}
return $shipping;
}
// This looks up the zone again for every order, even for the same country.
public static function shippingFor(string $country): int
{
return Import::zoneOf($country) == 'home' ? 490 : 1290;
}
public static function zoneOf(string $country): string
{
return $country == 'DE' ? 'home' : 'abroad';
}
}
string $file = Core\Path::thisDirectory('import.speedscope.json');
Core\Debug::startProfile($file);
int $shipping = Import::run(['DE', 'DE', 'FR', 'DE', 'AT']);
Core\Debug::stopProfile();
echo 'shipping: ', $shipping, "\n";
// The file is JSON. `shared.frames` has the function names.
// Each `"O"` event is one call, and `frame` is the index of its name.
var $profile = Core\Json::decode(Core\IO::read($file)) as array<mixed>;
var $frames = ($profile['shared'] as array<mixed>)['frames'] as array<mixed>;
var $events = (($profile['profiles'] as array<mixed>)[0] as array<mixed>)['events'] as array<mixed>;
// This counts the calls of each function. The call to `stopProfile` is in the profile too.
int $index = 0;
foreach ($frames as mixed $frame) {
int $calls = 0;
foreach ($events as mixed $event) {
var $row = $event as array<mixed>;
if ($row['type'] as string == 'O' && $row['frame'] as int == $index) {
$calls = $calls + 1;
}
}
echo ($frame as array<mixed>)['name'] as string, ': ', $calls, "\n";
$index = $index + 1;
}
// This deletes the profile file, so the next run starts with an empty folder.
Core\IO::remove($file);
Output
shipping: 4050
Import::run: 1
Import::shippingFor: 5
Import::zoneOf: 5
Core\Debug::stopProfile: 1

Print the calls as a tree

<?nvs
// Prints the calls of a checkout as a tree, in the order they ran.
// `nvs run --profile speedscope` writes the same kind of file for a whole program.
class Checkout
{
public static function total(array<int> $cart): int
{
return Checkout::subtotal($cart) + Checkout::shipping();
}
public static function subtotal(array<int> $cart): int
{
int $sum = 0;
foreach ($cart as int $cents) {
$sum = $sum + Checkout::withDeposit($cents);
}
return $sum;
}
// Each bottle in the cart has a deposit of 25 cents.
public static function withDeposit(int $cents): int
{
return $cents + 25;
}
public static function shipping(): int
{
return 490;
}
}
string $file = Core\Path::thisDirectory('checkout.speedscope.json');
Core\Debug::startProfile($file);
int $total = Checkout::total([1000, 2500]);
Core\Debug::stopProfile();
echo 'total: ', $total, "\n";
var $profile = Core\Json::decode(Core\IO::read($file)) as array<mixed>;
var $frames = ($profile['shared'] as array<mixed>)['frames'] as array<mixed>;
var $events = (($profile['profiles'] as array<mixed>)[0] as array<mixed>)['events'] as array<mixed>;
// An `"O"` event starts a call, and a `"C"` event ends it.
// A call that starts while another call runs is inside it, so it is indented one step more.
uint $depth = 0;
foreach ($events as mixed $event) {
var $row = $event as array<mixed>;
if ($row['type'] as string == 'O') {
var $name = ($frames[$row['frame'] as int] as array<mixed>)['name'] as string;
echo Core\Str::repeat(' ', $depth), $name, "\n";
$depth = $depth + 1;
} else {
$depth = $depth - 1;
}
}
// This deletes the profile file, so the next run starts with an empty folder.
Core\IO::remove($file);
Output
total: 4040
Checkout::total
Checkout::subtotal
Checkout::withDeposit
Checkout::withDeposit
Checkout::shipping
Core\Debug::stopProfile

Profile only the slow step

<?nvs
// Profiles only the step of a nightly job that you want to measure.
class Nightly
{
public static function loadSettings(): int
{
return 30;
}
public static function archiveOrders(int $days): int
{
int $moved = 0;
for (int $day = 0; $day < $days; $day = $day + 1) {
$moved = $moved + Nightly::ordersOn($day);
}
return $moved;
}
public static function ordersOn(int $day): int
{
return $day % 7 == 0 ? 0 : 12;
}
public static function sendReport(int $moved): string
{
return 'archived ' . $moved . ' orders';
}
}
string $file = Core\Path::thisDirectory('nightly.speedscope.json');
// `loadSettings` runs before the start, so it is not in the profile.
int $days = Nightly::loadSettings();
Core\Debug::startProfile($file);
int $moved = Nightly::archiveOrders($days);
Core\Debug::stopProfile();
// `sendReport` runs after the stop, so it is not in the profile either.
echo Nightly::sendReport($moved), "\n";
// This prints the name of each function in the profile, once.
var $profile = Core\Json::decode(Core\IO::read($file)) as array<mixed>;
var $frames = ($profile['shared'] as array<mixed>)['frames'] as array<mixed>;
foreach ($frames as mixed $frame) {
echo 'profiled: ', ($frame as array<mixed>)['name'] as string, "\n";
}
// This deletes the profile file, so the next run starts with an empty folder.
Core\IO::remove($file);
Output
archived 300 orders
profiled: Nightly::archiveOrders
profiled: Nightly::ordersOn
profiled: Core\Debug::stopProfile