. /** * Performance timing for one particular measure. */ class measure_timing { const STORAGE_FOLDER = '/behatmeasuretimings/'; /** * @var string */ public $measure; /** * @var int */ public $start; /** * @var int */ public $end; /** * @var int */ public $duration; public function __construct(string $measure) { $this->measure = $measure; } /** * Start timing. */ public function start(): void { $this->start = $this->now(); } /** * Stop timing. */ public function end(): void { $this->end = $this->now(); $this->duration = $this->end - $this->start; } /** * Persist measure timing in storage. */ public function store(): void { global $CFG; $storagefolderpath = $CFG->dirroot . static::STORAGE_FOLDER; if (!file_exists($storagefolderpath)) { mkdir($storagefolderpath); } $data = [ 'measure' => $this->measure, 'start' => $this->start, 'end' => $this->end, 'duration' => $this->duration, ]; file_put_contents($storagefolderpath . time() . '.json', json_encode($data)); } /** * Get current time. * * @return int Current time in milliseconds. */ private function now(): int { return round(microtime(true) * 1000); } }