Skip to content

Commit b747efe

Browse files
authored
Run the lexer benchmark workflow on the MySQL parser package (#616)
## Summary The lexer benchmark workflow now measures **only the new lexer** in `packages/mysql-parser`, using a new `--lexer-only` flag in that package's benchmark. The job **fails** if the lexer throws errors or the benchmark crashes, so a bad run can't post misleading numbers. ## Why Lexer work now happens in `packages/mysql-parser`, but the workflow only ran for the older lexer in `packages/mysql-on-sqlite`. The old benchmark tool stays there for local use, because the driver still uses that lexer. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **New Features** * Added a lexer-only benchmark mode that measures tokenization without parsing. Benchmark results identify whether they measure lexer-only or end-to-end throughput, in both text and JSON formats. * **Documentation** * Updated benchmark examples to show how to run lexer-only measurements. * **Chores** * Updated automated benchmark checks to measure the MySQL parser package and report failure when results are missing or contain errors. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 6ca5528 commit b747efe

3 files changed

Lines changed: 52 additions & 27 deletions

File tree

‎.github/workflows/lexer-benchmark.yml‎

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ name: Lexer benchmark
33
on:
44
pull_request:
55
paths:
6-
- 'packages/mysql-on-sqlite/src/mysql/class-wp-mysql-lexer.php'
7-
- 'packages/mysql-on-sqlite/src/mysql/class-wp-mysql-token.php'
8-
- 'packages/mysql-on-sqlite/src/parser/class-wp-parser-token.php'
9-
- 'packages/mysql-on-sqlite/tests/tools/run-lexer-benchmark.php'
6+
- 'packages/mysql-parser/src/class-wp-mysql-lexer.php'
7+
- 'packages/mysql-parser/src/class-wp-mysql-token.php'
8+
- 'packages/mysql-parser/src/parser/class-wp-parser-token.php'
9+
- 'packages/mysql-parser/tests/benchmark.php'
1010
- '.github/workflows/lexer-benchmark.yml'
1111

1212
# A new push supersedes the previous run; the result comment is updated in place.
@@ -38,16 +38,29 @@ jobs:
3838
php-version: '8.4'
3939
coverage: none
4040

41+
- name: Generate the autoloader
42+
working-directory: packages/mysql-parser
43+
run: composer dump-autoload --no-interaction
44+
4145
- name: Benchmark base vs PR
4246
env:
4347
BASE_SHA: ${{ github.event.pull_request.base.sha }}
4448
run: |
45-
BENCH=packages/mysql-on-sqlite/tests/tools/run-lexer-benchmark.php
49+
BENCH=packages/mysql-parser/tests/benchmark.php
4650
47-
# Best-pass QPS for a given PHP flag set.
51+
# Best-pass QPS for a given PHP flag set. A lexer that throws looks fast, and a
52+
# crashed run prints no result, so both fail the job instead of posting a number.
4853
best() {
49-
php -d memory_limit=512M "$@" "$BENCH" --json \
50-
| php -r '$j = json_decode( stream_get_contents( STDIN ), true ); echo (int) $j["qps_best"];'
54+
php -d memory_limit=512M "$@" "$BENCH" --lexer-only --iterations=10 --warmup=3 --json \
55+
| php -r '
56+
$output = stream_get_contents( STDIN );
57+
$result = json_decode( $output, true );
58+
if ( ! $result || $result["exceptions"] > 0 ) {
59+
fwrite( STDERR, "The lexer benchmark failed:\n$output\n" );
60+
exit( 1 );
61+
}
62+
echo (int) $result["qps_best"];
63+
'
5164
}
5265
jit_flags="-d opcache.enable_cli=1 -d opcache.jit_buffer_size=64M -d opcache.jit=tracing"
5366
@@ -57,11 +70,13 @@ jobs:
5770
5871
# Swap only the source tree to the base commit and re-measure with the
5972
# same (PR) benchmark tool, so both sides are timed identically. The
60-
# benchmark tool itself (tests/tools/) is left at the PR version.
61-
git checkout "$BASE_SHA" -- packages/mysql-on-sqlite/src
73+
# benchmark tool itself (tests/) is left at the PR version. PR-only files
74+
# are removed and the classmap is rebuilt, so the base run can't load PR code.
75+
git checkout --no-overlay "$BASE_SHA" -- packages/mysql-parser/src
76+
composer dump-autoload --no-interaction --working-dir=packages/mysql-parser
6277
base_nojit=$( best )
6378
base_jit=$( best $jit_flags )
64-
git checkout HEAD -- packages/mysql-on-sqlite/src
79+
git checkout HEAD -- packages/mysql-parser/src
6580
6681
fmt() { php -r 'echo number_format( (int) $argv[1] );' "$1"; }
6782
ratio() { php -r 'printf( "%.2f", $argv[1] / max( 1, (int) $argv[2] ) );' "$1" "$2"; }
@@ -80,7 +95,7 @@ jobs:
8095
echo
8196
echo "To reproduce locally:"
8297
echo '```'
83-
echo "cd packages/mysql-on-sqlite && composer run bench-lexer"
98+
echo "cd packages/mysql-parser && composer dump-autoload && composer run benchmark -- --lexer-only"
8499
echo '```'
85100
} > "$RUNNER_TEMP/comment.md"
86101
echo "COMMENT_FILE=$RUNNER_TEMP/comment.md" >> "$GITHUB_ENV"

‎packages/mysql-parser/README.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ This requires `bash`, `git`, and `php`. It performs the following steps:
9797
To run lexer and parser tests and benchmarks, use:
9898

9999
```bash
100-
composer run test # PHPUnit suite, including the query corpus tests
101-
composer run benchmark # Query throughput with and without JIT on the whole corpus
100+
composer run test # PHPUnit suite, including the query corpus tests
101+
composer run benchmark # Query throughput with and without JIT on the whole corpus
102+
composer run benchmark -- --lexer-only # The same, measuring only the lexer
102103
```

‎packages/mysql-parser/tests/benchmark.php‎

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
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
@@ -13,6 +14,7 @@
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

4548
require_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.
4952
if ( ! is_readable( $corpus ) ) {
@@ -65,17 +68,17 @@ function ( $severity, $message, $file, $line ) {
6568
fclose( $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

8790
for ( $i = 0; $i < $warmup; $i++ ) {
88-
$parse_corpus();
91+
$run_pass();
8992
}
9093

9194
$samples = array();
9295
for ( $i = 0; $i < $iterations; $i++ ) {
9396
$start = microtime( true );
94-
$parse_corpus();
97+
$run_pass();
9598
$samples[] = $query_count / ( microtime( true ) - $start );
9699
}
97100
sort( $samples );
@@ -112,7 +115,7 @@ function ( $severity, $message, $file, $line ) {
112115
if ( $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 );
139142
if ( $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+
);
143152
printf( " best: %s QPS\n", number_format( $best ) );
144153
printf( " median: %s QPS\n", number_format( $median ) );
145154
printf( " spread: %.1f%% (best vs worst)\n", $spread * 100 );

0 commit comments

Comments
 (0)