Oihana PHP

BenchTrait uses trait:short

Provides lightweight benchmarking helpers to measure the execution time of a controller action.

When benchmarking is enabled (via the bench flag), a controller can mark the start of an operation, then later compute and store a human-readable elapsed time in its output options.

Tags
author

Marc Alcaraz (ekameleon)

since
1.0.0

Table of Contents

Properties

$bench  : bool
The bench flag to test the script execution time of a function.

Methods

endBench()  : string|null
Stop the bench and compute the elapsed time since the given start timestamp.
initializeBench()  : static
Initialize the `bench` property.
startBench()  : int|float|null
Start the bench process.
prepareBench()  : bool
Prepares and returns the `bench` value.

Properties

$bench

The bench flag to test the script execution time of a function.

public bool $bench = false

Methods

endBench()

Stop the bench and compute the elapsed time since the given start timestamp.

public endBench(int|float|null $timestamp[, array<string|int, mixed> &$options = [] ]) : string|null

When a valid positive $timestamp is supplied, the elapsed duration is humanized (e.g. "1.2 s") and stored in $options under the Output::TIME key.

Parameters
$timestamp : int|float|null

The start timestamp returned by self::startBench(), or null.

$options : array<string|int, mixed> = []

Reference to the output options array updated with the elapsed time.

Return values
string|null

The human-readable time interval of the bench, or null when no valid timestamp was given.

initializeBench()

Initialize the `bench` property.

public initializeBench([bool|array<string|int, mixed> $init = [] ]) : static

The flag is read directly when $init is a boolean, otherwise from the ControllerParam::BENCH key of the initialization array (defaulting to false).

Parameters
$init : bool|array<string|int, mixed> = []

Optional initialization array or the bench boolean value.

Return values
static

Returns the current instance for method chaining.

startBench()

Start the bench process.

public startBench(ServerRequestInterface|null $request[, array<string|int, mixed> $args = [] ][, array<string|int, mixed>|null &$params = null ]) : int|float|null

Benchmarking only starts when self::prepareBench() confirms it is enabled for the current request; otherwise the method returns 0.

Parameters
$request : ServerRequestInterface|null

The current PSR-7 request, used to decide whether benchmarking applies.

$args : array<string|int, mixed> = []

Optional route arguments forwarded to the preparation step.

$params : array<string|int, mixed>|null = null

Reference to the request parameters, possibly populated by the preparation step.

Return values
int|float|null

The start timestamp (from microtime(true)) when benchmarking is enabled, otherwise 0.

prepareBench()

Prepares and returns the `bench` value.

protected prepareBench(ServerRequestInterface|null $request[, array<string|int, mixed> $args = [] ][, array<string|int, mixed>|null &$params = null ]) : bool
Parameters
$request : ServerRequestInterface|null

The incoming PSR-7 server request, or null when no request context is available.

$args : array<string|int, mixed> = []

The route/controller arguments that may carry an initial bench value.

$params : array<string|int, mixed>|null = null

A reference to the parameter bag updated in place with the prepared value.

Return values
bool

The resolved bench flag.

On this page

Search results