packages/sql-formatter/bench/FormatterBench.php
1<?php
2
3declare(strict_types=1);
4
5namespace Bench;
6
7use PhpBench\Attributes as Benchmark;
8use SqlFormatter\Core\FormatOptions;
9use SqlFormatter\Core\Style;
10use SqlFormatter\Facade\Formatter;
11use SqlParser\Sqlite\SqliteParser;
12
13/**
14 * Measures parsing, CST layout, and output verification with a reusable parser.
15 */
16final class FormatterBench
17{
18 private Formatter $formatter;
19
20 /**
21 * Loads parser tables before measurement.
22 */
23 public function setUp(): void
24 {
25 $this->formatter = new Formatter(new SqliteParser());
26 }
27
28 /**
29 * Formats a CTE, join, aggregate, and ordering through the public API.
30 */
31 #[Benchmark\BeforeMethods('setUp')]
32 #[Benchmark\Revs(100)]
33 #[Benchmark\Iterations(5)]
34 public function benchFormat(): void
35 {
36 $this->formatter->format('WITH q AS (SELECT id, score FROM users WHERE score>0) SELECT q.id, count(*) AS n FROM q JOIN events e ON e.user_id=q.id GROUP BY q.id HAVING count(*)>1 ORDER BY q.id;');
37 }
38 /**
39 * Loads the compact pipeline before measurement.
40 */
41 public function setUpCompact(): void
42 {
43 $this->formatter = new Formatter(new SqliteParser(), new FormatOptions(Style::Compact));
44 }
45
46 /**
47 * Measures canonicalization, including grouping and alias candidate verification.
48 */
49 #[Benchmark\BeforeMethods('setUpCompact')]
50 #[Benchmark\Revs(100)]
51 #[Benchmark\Iterations(5)]
52 public function benchCompact(): void
53 {
54 $this->formatter->format('SELECT ALL ((a+b))*c AS x,a+(b*c) AS y FROM t AS q LEFT OUTER JOIN u ON (q.a=u.a) WHERE (q.a != 1 AND q.b>2) ORDER BY q.a ASC;');
55 }
56}
57