22/**
33 * Benchmark the MySQL parser over the MySQL server test corpus.
44 *
5- * Reports the corpus parse rate and end-to-end (lex + parse) throughput.
5+ * Reports the corpus parse rate and end-to-end (lex + parse) throughput. With
6+ * --lexer-only, it measures only the lexer and parses nothing.
67 *
78 * Methodology: a few warmup passes (discarded — they heat opcache, the tracing
89 * JIT, and the CPU caches) followed by N timed passes over the whole corpus. The
1314 *
1415 * Options:
1516 * --json Machine-readable output.
17+ * --lexer-only Only lex the queries, without parsing them.
1618 * --limit=N Only benchmark the first N queries.
1719 * --iterations=N Number of timed passes (default 5).
1820 * --warmup=N Number of discarded warmup passes (default 2).
@@ -26,6 +28,7 @@ function ( $severity, $message, $file, $line ) {
2628);
2729
2830$ json = in_array ( '--json ' , $ argv , true );
31+ $ lexer_only = in_array ( '--lexer-only ' , $ argv , true );
2932$ limit = null ;
3033$ iterations = 5 ;
3134$ warmup = 2 ;
@@ -43,7 +46,7 @@ function ( $severity, $message, $file, $line ) {
4346}
4447
4548require_once __DIR__ . '/../vendor/autoload.php ' ;
46- $ parser = WP_MySQL_Parser_Factory ::create_parser ();
49+ $ parser = $ lexer_only ? null : WP_MySQL_Parser_Factory ::create_parser ();
4750
4851// Load the corpus before timing so file IO is excluded.
4952if ( ! is_readable ( $ corpus ) ) {
@@ -65,17 +68,20 @@ function ( $severity, $message, $file, $line ) {
6568fclose ( $ handle );
6669$ query_count = count ( $ queries );
6770
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 ) {
71+ // One pass over the corpus, recording failures and exceptions (deterministic
72+ // across passes, so the last pass's counts are kept). A failure is a query that
73+ // produces no tokens or doesn't parse.
74+ $ failures = 0 ;
75+ $ exceptions = 0 ;
76+ $ run_pass = function () use ( $ queries , $ parser , &$ failures , &$ exceptions ) {
7377 $ failures = 0 ;
7478 $ exceptions = 0 ;
7579 foreach ( $ queries as $ query ) {
7680 try {
7781 $ tokens = ( new WP_MySQL_Lexer ( $ query ) )->remaining_tokens ();
78- if ( null === $ parser ->parse ( $ tokens ) ) {
82+ if ( 0 === count ( $ tokens ) ) {
83+ ++$ failures ;
84+ } elseif ( null !== $ parser && null === $ parser ->parse ( $ tokens ) ) {
7985 ++$ failures ;
8086 }
8187 } catch ( Throwable $ e ) {
@@ -85,13 +91,13 @@ function ( $severity, $message, $file, $line ) {
8591};
8692
8793for ( $ i = 0 ; $ i < $ warmup ; $ i ++ ) {
88- $ parse_corpus ();
94+ $ run_pass ();
8995}
9096
9197$ samples = array ();
9298for ( $ i = 0 ; $ i < $ iterations ; $ i ++ ) {
9399 $ start = microtime ( true );
94- $ parse_corpus ();
100+ $ run_pass ();
95101 $ samples [] = $ query_count / ( microtime ( true ) - $ start );
96102}
97103sort ( $ samples );
@@ -112,7 +118,7 @@ function ( $severity, $message, $file, $line ) {
112118if ( $ json ) {
113119 echo json_encode (
114120 array (
115- 'benchmark ' => 'mysql-parser ' ,
121+ 'benchmark ' => $ lexer_only ? ' mysql-lexer ' : 'mysql-parser ' ,
116122 'opcache ' => $ opcache_on ,
117123 'jit ' => $ jit_on ,
118124 'queries ' => $ query_count ,
@@ -134,12 +140,18 @@ function ( $severity, $message, $file, $line ) {
134140}
135141
136142$ config = $ jit_on ? 'opcache + tracing JIT ' : ( $ opcache_on ? 'opcache, no JIT ' : 'no opcache ' );
137- printf ( "MySQL parser (official grammar) — %s \n" , $ config );
143+ printf ( "MySQL %s — %s \n" , $ lexer_only ? ' lexer ' : ' parser (official grammar) ' , $ config );
138144$ jit_requested = ! in_array ( strtolower ( (string ) ini_get ( 'opcache.jit ' ) ), array ( '' , '0 ' , 'off ' , 'disable ' ), true );
139145if ( $ jit_requested && ! $ jit_on ) {
140146 printf ( " warning: opcache.jit is set but the JIT is NOT active here — check that opcache is enabled and jit_buffer_size > 0. \n" );
141147}
142- printf ( "%s queries, %d warmup + %d timed passes (end-to-end lex+parse) \n" , number_format ( $ query_count ), $ warmup , $ iterations );
148+ printf (
149+ "%s queries, %d warmup + %d timed passes (%s) \n" ,
150+ number_format ( $ query_count ),
151+ $ warmup ,
152+ $ iterations ,
153+ $ lexer_only ? 'lex only ' : 'end-to-end lex+parse '
154+ );
143155printf ( " best: %s QPS \n" , number_format ( $ best ) );
144156printf ( " median: %s QPS \n" , number_format ( $ median ) );
145157printf ( " spread: %.1f%% (best vs worst) \n" , $ spread * 100 );
0 commit comments