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,17 @@ 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).
73+ $ failures = 0 ;
74+ $ exceptions = 0 ;
75+ $ run_pass = function () use ( $ queries , $ parser , &$ failures , &$ exceptions ) {
7376 $ failures = 0 ;
7477 $ exceptions = 0 ;
7578 foreach ( $ queries as $ query ) {
7679 try {
7780 $ tokens = ( new WP_MySQL_Lexer ( $ query ) )->remaining_tokens ();
78- if ( null === $ parser ->parse ( $ tokens ) ) {
81+ if ( null !== $ parser && null === $ parser ->parse ( $ tokens ) ) {
7982 ++$ failures ;
8083 }
8184 } catch ( Throwable $ e ) {
@@ -85,13 +88,13 @@ function ( $severity, $message, $file, $line ) {
8588};
8689
8790for ( $ i = 0 ; $ i < $ warmup ; $ i ++ ) {
88- $ parse_corpus ();
91+ $ run_pass ();
8992}
9093
9194$ samples = array ();
9295for ( $ i = 0 ; $ i < $ iterations ; $ i ++ ) {
9396 $ start = microtime ( true );
94- $ parse_corpus ();
97+ $ run_pass ();
9598 $ samples [] = $ query_count / ( microtime ( true ) - $ start );
9699}
97100sort ( $ samples );
@@ -112,7 +115,7 @@ function ( $severity, $message, $file, $line ) {
112115if ( $ json ) {
113116 echo json_encode (
114117 array (
115- 'benchmark ' => 'mysql-parser ' ,
118+ 'benchmark ' => $ lexer_only ? ' mysql-lexer ' : 'mysql-parser ' ,
116119 'opcache ' => $ opcache_on ,
117120 'jit ' => $ jit_on ,
118121 'queries ' => $ query_count ,
@@ -134,12 +137,18 @@ function ( $severity, $message, $file, $line ) {
134137}
135138
136139$ config = $ jit_on ? 'opcache + tracing JIT ' : ( $ opcache_on ? 'opcache, no JIT ' : 'no opcache ' );
137- printf ( "MySQL parser (official grammar) — %s \n" , $ config );
140+ printf ( "MySQL %s — %s \n" , $ lexer_only ? ' lexer ' : ' parser (official grammar) ' , $ config );
138141$ jit_requested = ! in_array ( strtolower ( (string ) ini_get ( 'opcache.jit ' ) ), array ( '' , '0 ' , 'off ' , 'disable ' ), true );
139142if ( $ jit_requested && ! $ jit_on ) {
140143 printf ( " warning: opcache.jit is set but the JIT is NOT active here — check that opcache is enabled and jit_buffer_size > 0. \n" );
141144}
142- printf ( "%s queries, %d warmup + %d timed passes (end-to-end lex+parse) \n" , number_format ( $ query_count ), $ warmup , $ iterations );
145+ printf (
146+ "%s queries, %d warmup + %d timed passes (%s) \n" ,
147+ number_format ( $ query_count ),
148+ $ warmup ,
149+ $ iterations ,
150+ $ lexer_only ? 'lex only ' : 'end-to-end lex+parse '
151+ );
143152printf ( " best: %s QPS \n" , number_format ( $ best ) );
144153printf ( " median: %s QPS \n" , number_format ( $ median ) );
145154printf ( " spread: %.1f%% (best vs worst) \n" , $ spread * 100 );
0 commit comments