Skip to content

profiling

The profile of a command of a model, with the sampling profiler of python 3.15.

--profile after the model package runs a command of the models under the statistical sampling profiler of python 3.15 (profiling.sampling, PEP 799) and writes the profile into the results of the model:

run_simulation pkdb_models.models.hctz --profile --action simulate --experiments all

The profiler samples the stack of the command at a fixed rate from a process of its own, so the command runs at nearly its normal speed. The command runs as a child of the profiler (python -m profiling.sampling run -m pkpdsim.cli ...), whose binary profile is converted afterwards (replay) into the files of the folder profiling/<command>_<time>/ of the results of the model:

  • flamegraph.html: the interactive flame graph of the sampled stacks;
  • heatmap/: the source code of the sampled lines with their sample counts;
  • profile.pstats: the statistics of the functions, for pstats and its viewers;
  • summary.txt: the functions with the most samples;
  • profile.bin: the samples, from which other formats are made and against which a later profile is compared (replay --diff-flamegraph profile.bin).

The profiler is part of python 3.15; with an older python --profile is an error and the command does not run.

profiler_available

profiler_available()

Check whether the python has the sampling profiler profiling.sampling.

record_exit_code

record_exit_code(code)

Write the exit code of a profiled command, see EXIT_CODE_FILE.

Parameters:

Name Type Description Default
code int | str | None

the code of SystemExit: None is 0, a message is 1.

required

profile_dir

profile_dir(model, command)

Get a new folder of the profile of a command in the results of a model.

Parameters:

Name Type Description Default
model PKPDModel

the model.

required
command str

the name of the command, e.g. run_simulation.

required

Returns:

Type Description
Path

profiling/<command>_<time> in the results of the model, not created.

run_profiled

run_profiled(model, command, args, output=None)

Run a command of a model under the sampling profiler, see the module.

Parameters:

Name Type Description Default
model PKPDModel

the model.

required
command str

the name of the command, a key of pkpdsim.cli.COMMANDS.

required
args Sequence[str]

the arguments of the command after the model package, without --profile.

required
output Path | None

the folder of the profile, profile_dir by default.

None

Returns:

Type Description
int

The exit code of the command.

Raises:

Type Description
RuntimeError

if the python has no sampling profiler.