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

aplus-framework / debug / 9591460565

24 Apr 2024 07:27PM UTC coverage: 99.634%. Remained the same
9591460565

push

github

natanfelles
Upgrade helpers library to version 4

544 of 546 relevant lines covered (99.63%)

2.78 hits per line

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

100.0
/src/Timer.php
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of Aplus Framework Debug Library.
4
 *
5
 * (c) Natan Felles <natanfelles@gmail.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace Framework\Debug;
11

12
use JetBrains\PhpStorm\ArrayShape;
13

14
/**
15
 * Class Timer.
16
 *
17
 * @package debug
18
 */
19
class Timer
20
{
21
    /**
22
     * @var array<string,array<string,mixed>>
23
     */
24
    protected array $marks = [];
25
    protected int $testsCount = 1;
26

27
    /**
28
     * Timer constructor.
29
     */
30
    public function __construct()
31
    {
32
        $this->addMark('debug[start]');
4✔
33
    }
34

35
    /**
36
     * @param int $times
37
     * @param callable $function
38
     * @param bool $flush
39
     *
40
     * @return array<string,string> Two keys - "memory" in MB and "time" in seconds
41
     */
42
    #[ArrayShape(['memory' => 'string', 'time' => 'string'])]
43
    public function test(int $times, callable $function, bool $flush = false) : array
44
    {
45
        if (!$flush) {
1✔
46
            \ob_start();
1✔
47
        }
48
        $this->testsCount++;
1✔
49
        $this->addMark('test[' . $this->testsCount . '][start]');
1✔
50
        for ($i = 0; $i < $times; $i++) {
1✔
51
            $function();
1✔
52
        }
53
        $this->addMark('test[' . ($this->testsCount) . '][end]');
1✔
54
        if (!$flush) {
1✔
55
            \ob_end_clean();
1✔
56
        }
57
        return $this->diff(
1✔
58
            'test[' . $this->testsCount . '][start]',
1✔
59
            'test[' . $this->testsCount . '][end]'
1✔
60
        );
1✔
61
    }
62

63
    /**
64
     * @param string $name
65
     *
66
     * @return static
67
     */
68
    public function addMark(string $name) : static
69
    {
70
        $this->marks[$name] = [
4✔
71
            'memory' => \memory_get_usage(),
4✔
72
            'time' => \microtime(true),
4✔
73
        ];
4✔
74
        return $this;
4✔
75
    }
76

77
    /**
78
     * @param string $name
79
     * @param int $memoryUsage
80
     * @param float $microtime
81
     *
82
     * @return static
83
     */
84
    public function setMark(string $name, int $memoryUsage, float $microtime) : static
85
    {
86
        $this->marks[$name] = [
1✔
87
            'memory' => $memoryUsage,
1✔
88
            'time' => $microtime,
1✔
89
        ];
1✔
90
        return $this;
1✔
91
    }
92

93
    /**
94
     * @param string $name
95
     *
96
     * @return array<string,string>|false
97
     */
98
    public function getMark(string $name) : array | false
99
    {
100
        return $this->marks[$name] ?? false;
1✔
101
    }
102

103
    /**
104
     * @param bool $format
105
     *
106
     * @return array<string,array<string,mixed>>
107
     */
108
    public function getMarks(bool $format = false) : array
109
    {
110
        $marks = $this->marks;
2✔
111
        if ($format) {
2✔
112
            foreach ($marks as &$mark) {
1✔
113
                $mark['memory'] = \number_format($mark['memory'] / 1024 / 1024, 3) . ' MB';
1✔
114
                $mark['time'] = \number_format($mark['time'], 3) . ' s';
1✔
115
            }
116
        }
117
        return $marks;
2✔
118
    }
119

120
    /**
121
     * @param string $from
122
     * @param string $to
123
     *
124
     * @return array<string,string> Two keys: memory in MB and time in seconds
125
     */
126
    #[ArrayShape(['memory' => 'string', 'time' => 'string'])]
127
    public function diff(string $from, string $to) : array
128
    {
129
        $number = $this->marks[$to]['memory'] - $this->marks[$from]['memory'];
2✔
130
        $number = \number_format($number / 1024 / 1024, 3);
2✔
131
        $diff = [];
2✔
132
        $diff['memory'] = $number . ' MB';
2✔
133
        $number = $this->marks[$to]['time'] - $this->marks[$from]['time'];
2✔
134
        $number = \number_format($number, 3);
2✔
135
        $diff['time'] = $number . ' s';
2✔
136
        return $diff;
2✔
137
    }
138
}
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

© 2025 Coveralls, Inc