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

codeigniter4 / CodeIgniter4 / 12955953600

24 Jan 2025 07:06PM UTC coverage: 84.558%. Remained the same
12955953600

Pull #9435

github

web-flow
Merge 47e36c967 into b5e971551
Pull Request #9435: chore: try a universal branch alias

20830 of 24634 relevant lines covered (84.56%)

191.21 hits per line

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

0.0
/system/Debug/Iterator.php
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This file is part of CodeIgniter 4 framework.
7
 *
8
 * (c) CodeIgniter Foundation <admin@codeigniter.com>
9
 *
10
 * For the full copyright and license information, please view
11
 * the LICENSE file that was distributed with this source code.
12
 */
13

14
namespace CodeIgniter\Debug;
15

16
use Closure;
17

18
/**
19
 * Iterator for debugging.
20
 */
21
class Iterator
22
{
23
    /**
24
     * Stores the tests that we are to run.
25
     *
26
     * @var array
27
     */
28
    protected $tests = [];
29

30
    /**
31
     * Stores the results of each of the tests.
32
     *
33
     * @var array
34
     */
35
    protected $results = [];
36

37
    /**
38
     * Adds a test to run.
39
     *
40
     * Tests are simply closures that the user can define any sequence of
41
     * things to happen during the test.
42
     *
43
     * @param Closure(): mixed $closure
44
     *
45
     * @return $this
46
     */
47
    public function add(string $name, Closure $closure)
48
    {
49
        $name = strtolower($name);
×
50

51
        $this->tests[$name] = $closure;
×
52

53
        return $this;
×
54
    }
55

56
    /**
57
     * Runs through all of the tests that have been added, recording
58
     * time to execute the desired number of iterations, and the approximate
59
     * memory usage used during those iterations.
60
     *
61
     * @return string|null
62
     */
63
    public function run(int $iterations = 1000, bool $output = true)
64
    {
65
        foreach ($this->tests as $name => $test) {
×
66
            // clear memory before start
67
            gc_collect_cycles();
×
68

69
            $start    = microtime(true);
×
70
            $startMem = $maxMemory = memory_get_usage(true);
×
71

72
            for ($i = 0; $i < $iterations; $i++) {
×
73
                $result    = $test();
×
74
                $maxMemory = max($maxMemory, memory_get_usage(true));
×
75

76
                unset($result);
×
77
            }
78

79
            $this->results[$name] = [
×
80
                'time'   => microtime(true) - $start,
×
81
                'memory' => $maxMemory - $startMem,
×
82
                'n'      => $iterations,
×
83
            ];
×
84
        }
85

86
        if ($output) {
×
87
            return $this->getReport();
×
88
        }
89

90
        return null;
×
91
    }
92

93
    /**
94
     * Get results.
95
     */
96
    public function getReport(): string
97
    {
98
        if ($this->results === []) {
×
99
            return 'No results to display.';
×
100
        }
101

102
        helper('number');
×
103

104
        // Template
105
        $tpl = '<table>
×
106
                        <thead>
107
                                <tr>
108
                                        <td>Test</td>
109
                                        <td>Time</td>
110
                                        <td>Memory</td>
111
                                </tr>
112
                        </thead>
113
                        <tbody>
114
                                {rows}
115
                        </tbody>
116
                </table>';
×
117

118
        $rows = '';
×
119

120
        foreach ($this->results as $name => $result) {
×
121
            $memory = number_to_size($result['memory'], 4);
×
122

123
            $rows .= "<tr>
×
124
                                <td>{$name}</td>
×
125
                                <td>" . number_format($result['time'], 4) . "</td>
×
126
                                <td>{$memory}</td>
×
127
                        </tr>";
×
128
        }
129

130
        $tpl = str_replace('{rows}', $rows, $tpl);
×
131

132
        return $tpl . '<br/>';
×
133
    }
134
}
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