packages/sql-semantics/bench/SemanticsBench.php
1<?php
2
3declare(strict_types=1);
4
5namespace Bench;
6
7use PhpBench\Attributes as Benchmark;
8use SqlSemantics\Core\Binder;
9use SqlSemantics\Core\SchemaBuilder;
10use SqlSemantics\Platform\PostgreSql\Dialect as PostgreSqlDialect;
11
12/**
13 * Measures the public SQL-to-bound-statement pipeline with a reusable schema.
14 */
15final class SemanticsBench
16{
17 private Binder $binder;
18
19 /**
20 * Builds declarations and loads parser resources before measurement.
21 */
22 public function setUp(): void
23 {
24 $schema = (new SchemaBuilder(PostgreSqlDialect::PostgreSql))->build('CREATE TABLE users (id INTEGER PRIMARY KEY, parent_id INTEGER, score INTEGER NOT NULL)');
25 $this->binder = new Binder($schema);
26 }
27
28 /**
29 * Parses and binds a self join against the same schema on each iteration.
30 */
31 #[Benchmark\BeforeMethods('setUp')]
32 #[Benchmark\Revs(100)]
33 #[Benchmark\Iterations(5)]
34 public function benchBind(): void
35 {
36 $this->binder->bind('SELECT child.id, parent.score, COALESCE(parent.score, 0) AS effective_score FROM users child LEFT JOIN users parent ON child.parent_id=parent.id WHERE child.score>0 ORDER BY child.id LIMIT 10');
37 }
38}
39