|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Final multi-config benchmark for the parser exploration. |
| 4 | + */ |
| 5 | + |
| 6 | +set_error_handler( |
| 7 | + function ( $s, $m, $f, $l ) { |
| 8 | + throw new ErrorException( $m, 0, $s, $f, $l ); |
| 9 | + } |
| 10 | +); |
| 11 | + |
| 12 | +require_once __DIR__ . '/../../src/parser/class-wp-parser-grammar.php'; |
| 13 | +require_once __DIR__ . '/../../src/parser/class-wp-parser-node.php'; |
| 14 | +require_once __DIR__ . '/../../src/parser/class-wp-parser-token.php'; |
| 15 | +require_once __DIR__ . '/../../src/parser/class-wp-parser.php'; |
| 16 | +require_once __DIR__ . '/../../src/mysql/class-wp-mysql-token.php'; |
| 17 | +require_once __DIR__ . '/../../src/mysql/class-wp-mysql-lexer.php'; |
| 18 | +require_once __DIR__ . '/../../src/mysql/class-wp-mysql-parser.php'; |
| 19 | + |
| 20 | +$runs = (int) ( $argv[1] ?? 10 ); |
| 21 | + |
| 22 | +$grammar = new WP_Parser_Grammar( require __DIR__ . '/../../src/mysql/mysql-grammar.php' ); |
| 23 | +$handle = fopen( __DIR__ . '/../mysql/data/mysql-server-tests-queries.csv', 'r' ); |
| 24 | +$queries = array(); |
| 25 | +$header = true; |
| 26 | +while ( ( $r = fgetcsv( $handle, null, ',', '"', '\\' ) ) !== false ) { |
| 27 | + if ( $header ) { |
| 28 | + $header = false; |
| 29 | + continue; } |
| 30 | + if ( null !== $r[0] ) { |
| 31 | + $queries[] = $r[0]; |
| 32 | + } |
| 33 | +} |
| 34 | +fclose( $handle ); |
| 35 | + |
| 36 | +$all_tokens = array(); |
| 37 | +foreach ( $queries as $q ) { |
| 38 | + $all_tokens[] = ( new WP_MySQL_Lexer( $q ) )->remaining_tokens(); |
| 39 | +} |
| 40 | +$count = count( $queries ); |
| 41 | +printf( "Loaded %d queries\n", $count ); |
| 42 | + |
| 43 | +$durations = array(); |
| 44 | +for ( $i = 0; $i < $runs; $i++ ) { |
| 45 | + $start = microtime( true ); |
| 46 | + $fail = 0; |
| 47 | + foreach ( $all_tokens as $t ) { |
| 48 | + if ( null === ( new WP_MySQL_Parser( $grammar, $t ) )->parse() ) { |
| 49 | + ++$fail; |
| 50 | + } |
| 51 | + } |
| 52 | + $d = microtime( true ) - $start; |
| 53 | + $durations[] = $d; |
| 54 | +} |
| 55 | +sort( $durations ); |
| 56 | +$best = $durations[0]; |
| 57 | +$med = $durations[ (int) ( count( $durations ) / 2 ) ]; |
| 58 | +$avg = array_sum( $durations ) / count( $durations ); |
| 59 | +printf( "best %.4fs %6d QPS\n", $best, $count / $best ); |
| 60 | +printf( "med %.4fs %6d QPS\n", $med, $count / $med ); |
| 61 | +printf( "avg %.4fs %6d QPS\n", $avg, $count / $avg ); |
0 commit comments