• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

eliashaeussler / cache-warmup / 28544825007

01 Jul 2026 08:12PM UTC coverage: 89.099% (-1.0%) from 90.11%
28544825007

Pull #676

github

eliashaeussler
[!!!][FEATURE] Introduce profiler to measure duration and memory usage
Pull Request #676: [!!!][FEATURE] Introduce profiler to measure duration and memory usage

90 of 116 new or added lines in 7 files covered. (77.59%)

1 existing line in 1 file now uncovered.

1839 of 2064 relevant lines covered (89.1%)

10.03 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

81.82
/src/Profiler/ScopeProfiler.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of the Composer package "eliashaeussler/cache-warmup".
7
 *
8
 * Copyright (C) 2020-2026 Elias Häußler <elias@haeussler.dev>
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation, either version 3 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22
 */
23

24
namespace EliasHaeussler\CacheWarmup\Profiler;
25

26
use Closure;
27

28
use function max;
29

30
/**
31
 * ScopeProfiler.
32
 *
33
 * @author Elias Häußler <elias@haeussler.dev>
34
 * @license GPL-3.0-or-later
35
 *
36
 * @internal
37
 */
38
final class ScopeProfiler
39
{
40
    /**
41
     * @var list<MeasurementSpan>
42
     */
43
    private static array $stack = [];
44

45
    private function __construct(
2✔
46
        private readonly ?string $action,
47
        private readonly MeasurementPoint $startPoint,
48
    ) {}
2✔
49

50
    public static function start(?string $action = null): self
2✔
51
    {
52
        return new self($action, MeasurementPoint::now());
2✔
53
    }
54

55
    /**
56
     * @template T
57
     *
58
     * @param Closure(): T $task
59
     *
60
     * @param-out T $result
61
     */
62
    public static function startAndExecute(string $action, Closure $task, mixed &$result = null): MeasurementSpan
1✔
63
    {
64
        $profiler = self::start($action);
1✔
65
        $result = (static fn () => $task())();
1✔
66

67
        return $profiler->stop();
1✔
68
    }
69

70
    public function stop(): MeasurementSpan
2✔
71
    {
72
        $endPoint = MeasurementPoint::now();
2✔
73
        $span = new MeasurementSpan(
2✔
74
            $this->action,
2✔
75
            $endPoint->time - $this->startPoint->time,
2✔
76
            $endPoint->memoryUsage - $this->startPoint->memoryUsage,
2✔
77
            max($endPoint->memoryPeak, $this->startPoint->memoryPeak),
2✔
78
        );
2✔
79

80
        self::$stack[] = $span;
2✔
81

82
        return $span;
2✔
83
    }
84

85
    /**
86
     * @return list<MeasurementSpan>
87
     */
NEW
88
    public static function releaseStack(): array
×
89
    {
NEW
90
        $stack = self::$stack;
×
NEW
91
        self::$stack = [];
×
92

NEW
93
        return $stack;
×
94
    }
95
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc