Skip to content

Commit 3a1b9e9

Browse files
committed
Add the corpus benchmark
Measure the corpus parse rate and end-to-end (lex + parse) throughput, with warmup and timed passes. The parser accepts 99.76% of the ~69k corpus queries.
1 parent cf46532 commit 3a1b9e9

2 files changed

Lines changed: 158 additions & 0 deletions

File tree

‎packages/mysql-parser/composer.json‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
],
1818
"build-corpus": [
1919
"./bin/build-corpus"
20+
],
21+
"benchmark": [
22+
"@php tests/benchmark.php",
23+
"@php -d opcache.enable_cli=1 -d opcache.jit_buffer_size=64M -d opcache.jit=tracing tests/benchmark.php"
2024
]
2125
}
2226
}
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
<?php
2+
/**
3+
* Benchmark the MySQL parser over the MySQL server test corpus.
4+
*
5+
* Reports the corpus parse rate and end-to-end (lex + parse) throughput.
6+
*
7+
* Methodology: a few warmup passes (discarded — they heat opcache, the tracing
8+
* JIT, and the CPU caches) followed by N timed passes over the whole corpus. The
9+
* headline is the BEST pass: parsing is deterministic and CPU-bound, so outside
10+
* interference only ever makes a pass slower, which makes the fastest pass the
11+
* most reproducible estimate. A single cold pass badly under-reports the tracing
12+
* JIT (it pays compilation inside the timed run), so warmup is on by default.
13+
*
14+
* Options:
15+
* --json Machine-readable output.
16+
* --limit=N Only benchmark the first N queries.
17+
* --iterations=N Number of timed passes (default 5).
18+
* --warmup=N Number of discarded warmup passes (default 2).
19+
* --corpus=PATH Path to the queries CSV (default: the package corpus).
20+
*/
21+
22+
set_error_handler(
23+
function ( $severity, $message, $file, $line ) {
24+
throw new ErrorException( $message, 0, $severity, $file, $line );
25+
}
26+
);
27+
28+
$json = in_array( '--json', $argv, true );
29+
$limit = null;
30+
$iterations = 5;
31+
$warmup = 2;
32+
$corpus = __DIR__ . '/../data/mysql-server-query-corpus/mysql-latest.csv';
33+
foreach ( $argv as $arg ) {
34+
if ( 0 === strpos( $arg, '--limit=' ) ) {
35+
$limit = max( 1, (int) substr( $arg, strlen( '--limit=' ) ) );
36+
} elseif ( 0 === strpos( $arg, '--iterations=' ) ) {
37+
$iterations = max( 1, (int) substr( $arg, strlen( '--iterations=' ) ) );
38+
} elseif ( 0 === strpos( $arg, '--warmup=' ) ) {
39+
$warmup = max( 0, (int) substr( $arg, strlen( '--warmup=' ) ) );
40+
} elseif ( 0 === strpos( $arg, '--corpus=' ) ) {
41+
$corpus = substr( $arg, strlen( '--corpus=' ) );
42+
}
43+
}
44+
45+
require_once __DIR__ . '/../vendor/autoload.php';
46+
$parser = WP_MySQL_Parser_Factory::create_parser();
47+
48+
// Load the corpus before timing so file IO is excluded.
49+
if ( ! is_readable( $corpus ) ) {
50+
fwrite( STDERR, "error: corpus not found at $corpus (pass --corpus=PATH).\n" );
51+
exit( 1 );
52+
}
53+
$handle = fopen( $corpus, 'r' );
54+
$queries = array();
55+
while ( ( $record = fgetcsv( $handle, null, ',', '"', '\\' ) ) !== false ) {
56+
$query = $record[0] ?? null;
57+
if ( null === $query || '' === $query ) {
58+
continue;
59+
}
60+
$queries[] = $query;
61+
if ( null !== $limit && count( $queries ) >= $limit ) {
62+
break;
63+
}
64+
}
65+
fclose( $handle );
66+
$query_count = count( $queries );
67+
68+
// One end-to-end pass over the corpus (lex + parse), recording failures and
69+
// exceptions (deterministic across passes, so the last pass's counts are kept).
70+
$failures = 0;
71+
$exceptions = 0;
72+
$parse_corpus = function () use ( $queries, $parser, &$failures, &$exceptions ) {
73+
$failures = 0;
74+
$exceptions = 0;
75+
foreach ( $queries as $query ) {
76+
try {
77+
$tokens = ( new WP_MySQL_Lexer( $query ) )->remaining_tokens();
78+
if ( null === $parser->parse( $tokens ) ) {
79+
++$failures;
80+
}
81+
} catch ( Throwable $e ) {
82+
++$exceptions;
83+
}
84+
}
85+
};
86+
87+
for ( $i = 0; $i < $warmup; $i++ ) {
88+
$parse_corpus();
89+
}
90+
91+
$samples = array();
92+
for ( $i = 0; $i < $iterations; $i++ ) {
93+
$start = microtime( true );
94+
$parse_corpus();
95+
$samples[] = $query_count / ( microtime( true ) - $start );
96+
}
97+
sort( $samples );
98+
99+
$best = $samples[ count( $samples ) - 1 ];
100+
$worst = $samples[0];
101+
$mean = array_sum( $samples ) / count( $samples );
102+
$mid = intdiv( count( $samples ), 2 );
103+
$median = 0 === count( $samples ) % 2
104+
? ( $samples[ $mid - 1 ] + $samples[ $mid ] ) / 2
105+
: $samples[ $mid ];
106+
$spread = $best > 0 ? ( $best - $worst ) / $best : 0.0;
107+
108+
$opcache_status = function_exists( 'opcache_get_status' ) ? opcache_get_status( false ) : false;
109+
$opcache_on = is_array( $opcache_status );
110+
$jit_on = $opcache_on && ! empty( $opcache_status['jit']['on'] );
111+
112+
if ( $json ) {
113+
echo json_encode(
114+
array(
115+
'benchmark' => 'mysql-parser',
116+
'opcache' => $opcache_on,
117+
'jit' => $jit_on,
118+
'queries' => $query_count,
119+
'warmup' => $warmup,
120+
'iterations' => $iterations,
121+
'qps' => $best,
122+
'qps_best' => $best,
123+
'qps_median' => $median,
124+
'qps_mean' => $mean,
125+
'qps_worst' => $worst,
126+
'spread' => $spread,
127+
'failures' => $failures,
128+
'exceptions' => $exceptions,
129+
'php_version' => PHP_VERSION,
130+
),
131+
JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES
132+
), "\n";
133+
exit;
134+
}
135+
136+
$config = $jit_on ? 'opcache + tracing JIT' : ( $opcache_on ? 'opcache, no JIT' : 'no opcache' );
137+
printf( "MySQL parser (official grammar) — %s\n", $config );
138+
$jit_requested = ! in_array( strtolower( (string) ini_get( 'opcache.jit' ) ), array( '', '0', 'off', 'disable' ), true );
139+
if ( $jit_requested && ! $jit_on ) {
140+
printf( " warning: opcache.jit is set but the JIT is NOT active here — check that opcache is enabled and jit_buffer_size > 0.\n" );
141+
}
142+
printf( "%s queries, %d warmup + %d timed passes (end-to-end lex+parse)\n", number_format( $query_count ), $warmup, $iterations );
143+
printf( " best: %s QPS\n", number_format( $best ) );
144+
printf( " median: %s QPS\n", number_format( $median ) );
145+
printf( " spread: %.1f%% (best vs worst)\n", $spread * 100 );
146+
printf(
147+
" failures: %d (%.2f%%) | exceptions: %d\n",
148+
$failures,
149+
$query_count > 0 ? $failures / $query_count * 100 : 0.0,
150+
$exceptions
151+
);
152+
if ( $spread > 0.10 ) {
153+
printf( " note: >10%% spread — the machine is noisy; close other apps for a steadier number.\n" );
154+
}

0 commit comments

Comments
 (0)