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

dg / dresscode / 42928610199

10 Nov 2026 11:53PM UTC coverage: 97.024% (-0.6%) from 97.596%
42928610199

push

github

dg
cli: the hidden option --profile writes where the time of a run went, the workers included

`--profile <file>` writes as JSON where the time of a run went, in nanoseconds: the phases (the start of the process,
the start of the container of PHPStan or of a derived one and its scopes, the parse, the passes, the gaps, the print,
the cycle collection, the run as a whole), every rule by the time of its callbacks, how often they ran and how often
they changed the tree, every gap rule by the time of its claims, every file with its size, rounds and passes, and the
peak memory of every process. A worker measures its own files and hands that over with every result and the parent
adds it up, so the totals do not depend on the number of workers. A run with the option leaves the result cache out,
a file served from the cache having nothing to measure.

It says where the time of a run goes, not only how much of it: a comparison of two revisions then names the rule or
the claim that moved. Without the option the engine only asks whether there is a profiler, and wraps a claim in
a measurement only when there is one.

137 of 148 new or added lines in 14 files covered. (92.57%)

147 existing lines in 29 files now uncovered.

30283 of 31212 relevant lines covered (97.02%)

0.97 hits per line

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

93.33
/src/Measuring/ProcessProbeRunner.php
1
<?php declare(strict_types=1);
2

3
/**
4
 * This file is part of the DressCode, a coding style and upgrade tool for PHP (https://dresscode.run)
5
 * Copyright (c) 2026 David Grudl (https://davidgrudl.com)
6
 */
7

8
namespace DressCode\Measuring;
9

10
use DressCode\Engine\{ProcessRunner, WorkerPool};
11
use Nette\Neon\Neon;
12
use Nette\Utils\FileSystem;
13
use function count, is_array;
14
use const PHP_OS_FAMILY;
15

16

17
/**
18
 * Runs the probes as processes of `check --format json`, as many at once as the machine has processors, the processors
19
 * shared among those running: a probe over a sample is cheaper alone in a process than with a pool of workers to start,
20
 * and a few probes over many files get the workers between them. The configurations lie in the system temp, never in
21
 * the project, and `--root` makes the project the root of the run. The script is always the one of this DressCode, and a
22
 * probe starts no probes of its own, so the processes never grow beyond one level, however the runner is called.
23
 * @internal
24
 */
25
final readonly class ProcessProbeRunner implements ProbeRunner
26
{
27
        /** how long the loop waits between two looks at the processes, in microseconds */
28
        private const PollInterval = 20_000;
29

30
        /** how long a probe may run, in seconds */
31
        private const Timeout = 1800;
32

33
        /** the variable of the environment a probe runs with */
34
        private const ProbeVariable = 'DRESSCODE_PROBE';
35

36

37
        public function __construct(
38
                private string $root,
1 ✔
39
                /** @var list<string>  the PHP and the script of DressCode */
40
                private array $command,
1 ✔
41
                private int $processors,
1 ✔
42
                /** @var ?\Closure(int, int): void  the probes done and all of them, told at every look while they run */
43
                private ?\Closure $onProgress = null,
1 ✔
44
        ) {
45
        }
1 ✔
46

47

48
        /**
49
         * @param  ?\Closure(int, int): void  $onProgress
50
         * @param  ?int  $processors  how many the probes share; all of the machine by default
51
         */
52
        public static function create(string $root, ?\Closure $onProgress = null, ?int $processors = null): self
53
        {
54
                $command = [...WorkerPool::buildPhpCommand() ?? [PHP_BINARY], WorkerPool::locateProgram()];
1 ✔
55
                return new self($root, $command, $processors ?? WorkerPool::detectCpuCount(), $onProgress);
1 ✔
56
        }
57

58

59
        public function run(array $probes): array
60
        {
61
                if (getenv(self::ProbeVariable) !== false) {
1 ✔
62
                        throw new \LogicException('A probe of `dresscode init` starts no probes of its own.');
1 ✔
63
                }
64

65
                $dir = sys_get_temp_dir() . '/dresscode-probes-' . uniqid(more_entropy: true);
1 ✔
66
                FileSystem::createDir($dir);
1 ✔
67
                $parallel = max(1, min($this->processors, count($probes)));
1 ✔
68
                $jobs = max(1, intdiv($this->processors, $parallel));
1 ✔
69
                $queue = array_keys($probes);
1 ✔
70
                $running = $results = [];
1 ✔
71
                try {
72
                        while ($queue || $running) {
1 ✔
73
                                while ($queue && count($running) < $parallel) {
1 ✔
74
                                        $key = array_shift($queue);
1 ✔
75
                                        $running[$key] = $this->start($probes[$key], $dir . '/' . md5((string) $key), $jobs);
1 ✔
76
                                }
77

78
                                foreach ($running as $key => [$process, $file, $started]) {
1 ✔
79
                                        // the exit code is told once, by the first look after the end, and proc_close() then has none
80
                                        $status = proc_get_status($process);
1 ✔
81
                                        if ($status['running'] && microtime(as_float: true) - $started > self::Timeout) {
1 ✔
UNCOV
82
                                                throw new \RuntimeException('A check of the measuring did not end within ' . self::Timeout / 60 . ' minutes.');
×
83
                                        } elseif ($status['running']) {
1 ✔
84
                                                continue;
1 ✔
85
                                        }
86

87
                                        proc_close($process);
1 ✔
88
                                        unset($running[$key]);
1 ✔
89
                                        $results[$key] = self::readResult($file, $status['exitcode']);
1 ✔
90
                                }
91

92
                                ($this->onProgress)?->__invoke(count($results), count($probes));
1 ✔
93
                                if ($running) {
1 ✔
94
                                        usleep(self::PollInterval);
1 ✔
95
                                }
96
                        }
97
                } finally {
1 ✔
98
                        foreach ($running as [$process]) {
1 ✔
UNCOV
99
                                ProcessRunner::killTree(proc_get_status($process)['pid']);
×
100
                                proc_close($process);
×
101
                        }
102

103
                        FileSystem::delete($dir);
1 ✔
104
                }
105

106
                return $results;
1 ✔
107
        }
108

109

110
        /**
111
         * @return array{resource, string, float}  the process, the base name of its files and when it started
112
         */
113
        private function start(Probe $probe, string $file, int $jobs): array
114
        {
115
                FileSystem::write("$file.neon", Neon::encode($probe->config, blockMode: true));
1 ✔
116
                $null = PHP_OS_FAMILY === 'Windows' ? 'NUL' : '/dev/null';
1 ✔
117
                $process = proc_open(
1 ✔
118
                        [
119
                                ...$this->command,
1 ✔
120
                                'check',
1 ✔
121
                                '--format=json',
1 ✔
122
                                "--jobs=$jobs",
1 ✔
123
                                '--no-cache',
1 ✔
124
                                "--root=$this->root",
1 ✔
125
                                "--config=$file.neon",
1 ✔
126
                                ...array_map(fn(string $path) => "--only=$path", $probe->only),
1 ✔
127
                        ],
128
                        [0 => ['file', $null, 'r'], 1 => ['file', "$file.json", 'w'], 2 => ['file', "$file.err", 'w']],
1 ✔
129
                        $pipes,
130
                        $this->root,
1 ✔
131
                        [...getenv(), self::ProbeVariable => '1'],
1 ✔
132
                );
133
                if ($process === false) {
1 ✔
UNCOV
134
                        throw new \RuntimeException('A process of `check` could not be started.');
×
135
                }
136

137
                return [$process, $file, microtime(as_float: true)];
1 ✔
138
        }
139

140

141
        private static function readResult(string $file, int $exit): ProbeResult
142
        {
143
                $report = json_decode((string) @file_get_contents("$file.json"), associative: true); // @ written by the process
1 ✔
144
                if ($exit > 2 || !is_array($report) || !is_array($report['files'] ?? null)) { // 2 is a file a rule failed in, which the report carries
1 ✔
145
                        $error = trim((string) @file_get_contents("$file.err")); // @ written by the process
1 ✔
146
                        throw new \RuntimeException("A check of the measuring ended with exit code $exit: " . ($error ?: 'no output'));
1 ✔
147
                }
148

149
                return ProbeResult::fromJson($report);
1 ✔
150
        }
151
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc