Files
firefly-iii/app/Support/Debug/Timer.php

62 lines
1.7 KiB
PHP
Raw Normal View History

2025-03-09 09:53:52 +01:00
<?php
2025-03-09 09:53:52 +01:00
/*
* Timer.php
* Copyright (c) 2025 james@firefly-iii.org.
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/
declare(strict_types=1);
namespace FireflyIII\Support\Debug;
use Illuminate\Support\Facades\Log;
class Timer
{
2025-08-10 06:38:23 +02:00
private array $times = [];
private static ?Timer $instance = null;
2025-03-09 09:53:52 +01:00
2025-08-10 06:38:23 +02:00
private function __construct()
2025-03-09 09:53:52 +01:00
{
2025-08-10 06:38:23 +02:00
// Private constructor to prevent direct instantiation.
2025-03-09 09:53:52 +01:00
}
2025-08-10 06:38:23 +02:00
public static function getInstance(): self
2025-03-09 09:53:52 +01:00
{
2025-08-10 06:38:23 +02:00
if (null === self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
public function start(string $title): void
{
$this->times[$title] = microtime(true);
}
public function stop(string $title): void
{
$start = $this->times[$title] ?? 0;
2025-03-09 09:53:52 +01:00
$end = microtime(true);
$diff = $end - $start;
2025-08-10 06:38:23 +02:00
unset($this->times[$title]);
2025-03-09 09:53:52 +01:00
Log::debug(sprintf('Timer "%s" took %f seconds', $title, $diff));
}
}