Skip to content

Commit f834251

Browse files
committed
HTML API benchmark: widen the differential test and show per-round results.
difftrace.php gains a walk that mixes modifications with bookmarks and seeks, special-element edge cases (case, near-miss names, NUL bytes, leading newlines, virtual closers), and an HTML Processor pass that calls get_updated_html() and then sets an attribute while walking, so that an update is pending when the processor parses a virtual token or an error again. It now finds the eight inputs on which matching special elements by raw bytes, instead of through get_tag(), changes HTML Processor output. get_updated_html() calls are capped at 20 per input, since each one copies the whole document. proc_open() no longer receives STDOUT or STDERR: PHP seeks a redirected file's shared offset to the stream's own position before handing it on, and the outputs overwrote each other. The report prints each round's overall change under the overall line, so one slow round is visible instead of only widening the interval.
1 parent 1faa0a6 commit f834251

4 files changed

Lines changed: 84 additions & 20 deletions

File tree

‎tests/performance/html-api/README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,9 @@ A faster parser is only useful if it parses the same way. `difftrace.php` compar
153153
php tests/performance/html-api/difftrace.php --base ~/a8c/wordpress-develop/trunk --head .
154154
```
155155

156-
For the Tag Processor it loads both checkouts' `class-wp-html-tag-processor.php` into one process under renamed classes (the other HTML API files come from base), walks each input six ways (`next_token()`, `next_tag()`, `next_tag()` visiting closers, `next_tag()` with queries, with attribute, class and text modifications, and with bookmarks and seeks), applies the same action to both, and compares the return value, every public accessor and every `_doing_it_wrong()` call after every step, then the final `get_updated_html()`. For the HTML Processor it runs each checkout in its own process and compares a hash of the token stream (type, name, breadcrumbs, namespace, attributes, modifiable text, final error) per input, and shows the first differing token for up to five inputs.
156+
For the Tag Processor it loads both checkouts' `class-wp-html-tag-processor.php` into one process under renamed classes (the other HTML API files come from base), walks each input seven ways (`next_token()`, `next_tag()`, `next_tag()` visiting closers, `next_tag()` with queries, with attribute, class and text modifications, with bookmarks and seeks, and with both mixed), applies the same action to both, and compares the return value, every public accessor and every `_doing_it_wrong()` call after every step, then the final `get_updated_html()`. For the HTML Processor it runs each checkout in its own process and compares a hash of the token stream (type, name, breadcrumbs, namespace, attributes, modifiable text, final error) per input, walked once plain and once setting attributes and calling `get_updated_html()` along the way, and shows the first differing token for up to five inputs.
157157

158-
The inputs are the real corpus, the synthetic shapes at 5 KB and 100 KB, about 250 hand-written edge cases with every prefix of each (which exercises incomplete input), and 20,000 mutated slices of the corpus (`--mutations`). A run takes about a minute and a half and exits 1 when anything differs. Removing `\r` from the whitespace skipped around `=`, or the `XMP` case from the special-element switch, each produce hundreds of differing inputs.
158+
The inputs are the real corpus, the synthetic shapes at 5 KB and 100 KB, about 270 hand-written edge cases with every prefix of each (which exercises incomplete input), and 20,000 mutated slices of the corpus (`--mutations`). A run takes about a minute and a half and exits 1 when anything differs. Removing `\r` from the whitespace skipped around `=`, or the `XMP` case from the special-element switch, each produce hundreds of differing inputs.
159159

160160
## Timed runs take a lock
161161

‎tests/performance/html-api/difftrace.php‎

Lines changed: 66 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,35 @@ function difftrace_edge_cases(): array {
252252
'<p>' . str_repeat( ' ', 300 ) . '</p>',
253253
);
254254

255+
// Special elements: case, near-miss names, NUL bytes, and leading newlines.
256+
array_push(
257+
$cases,
258+
'<TITLE>a</title>',
259+
'<titlex>y</titlex>',
260+
"<listing>\nx</listing>",
261+
"<LISTING>\r\nx",
262+
'<svg><title>a</title></svg>',
263+
'<textarea/>a</textarea>',
264+
'</script>',
265+
"<scri\0pt>x</script>",
266+
"<title\0>x</title>",
267+
"<t\xC4\xB1tle>x</title>",
268+
'<IFRAME>x</iframe>',
269+
'<NoEmBeD>x</noembed>',
270+
'<NOFRAMES>x</noframes>',
271+
'<Xmp>x</xmp>',
272+
'<scripts>x</scripts>',
273+
'<styles>x',
274+
"<pre>\n\nx</pre>",
275+
"<pre a=b>\nx</pre>",
276+
"<textarea>\n\nx</textarea>",
277+
'<p><xmp>b</xmp>c',
278+
'<p><title>b</title>c',
279+
'<table data-a=1><textarea>x</textarea>',
280+
"<table><tr><td><pre><br><td>\ny</table>",
281+
"<pre>\nx</pre><listing>\ny</listing>"
282+
);
283+
255284
// Every whitespace byte at every place the tokenizer skips or ends on whitespace.
256285
foreach ( array( ' ', "\t", "\f", "\r", "\n", "\r\n" ) as $ws ) {
257286
$cases[] = "<a{$ws}b{$ws}={$ws}c{$ws}d{$ws}={$ws}\"e\"{$ws}f{$ws}={$ws}'g'{$ws}h{$ws}/{$ws}>";
@@ -438,13 +467,17 @@ function difftrace_tag_walk( string $id, string $html, string $walk, int $seed,
438467
$b = new WP_HTML_Tag_Processor_Head( $html );
439468
mt_srand( crc32( "{$seed} {$id} {$walk}" ), MT_RAND_MT19937 );
440469

441-
$limit = in_array( $walk, array( 'modify', 'bookmarks' ), true ) ? 20000 : PHP_INT_MAX;
470+
$limit = in_array( $walk, array( 'modify', 'bookmarks', 'mixed' ), true ) ? 20000 : PHP_INT_MAX;
442471
$snapshot = null;
443472
$bookmarks = array();
444473
$seeks = 0;
445474
for ( $step = 0; $step < $limit; $step++ ) {
446475
$action = array( 'next_token' );
447-
switch ( $walk ) {
476+
$kind = $walk;
477+
if ( 'mixed' === $walk ) {
478+
$kind = mt_rand( 0, 1 ) ? 'modify' : 'bookmarks';
479+
}
480+
switch ( $kind ) {
448481
case 'tag':
449482
$action = array( 'next_tag' );
450483
break;
@@ -551,16 +584,39 @@ function difftrace_tag_walk( string $id, string $html, string $walk, int $seed,
551584
/**
552585
* Serializes the HTML Processor's token stream for one input.
553586
*
554-
* @param string $html Input.
587+
* With `$modify`, it also sets an attribute on some opening tags and calls
588+
* get_updated_html() on some tokens (on every token of a small input, at most
589+
* 20 times per input), which makes the processor parse the current token
590+
* again, and records what those calls return.
591+
*
592+
* @param string $html Input.
593+
* @param bool $modify Whether to modify while walking.
555594
* @return string[] One line per token, then the final state.
556595
*/
557-
function difftrace_html_stream( string $html ): array {
596+
function difftrace_html_stream( string $html, bool $modify = false ): array {
558597
$lines = array();
559598
$p = WP_HTML_Processor::create_full_parser( $html );
560599
if ( null === $p ) {
561600
return array( 'create_full_parser returned null' );
562601
}
602+
mt_srand( crc32( $html ), MT_RAND_MT19937 );
603+
$flushes = 0;
604+
// Small inputs set an attribute on half the opening tags and flush on every token, so that a flush
605+
// lands on virtual tokens and after errors; large inputs do both rarely.
606+
$small = strlen( $html ) < 2000;
607+
$set_below = $small ? 50 : 20;
563608
while ( $p->next_token() ) {
609+
if ( $modify ) {
610+
// Flush before modifying, so that an update set on one token is still pending at the next.
611+
$roll = mt_rand( 0, 99 );
612+
if ( ( $small || ( $roll >= 20 && $roll < 25 ) ) && ++$flushes <= 20 ) {
613+
// Each call copies and reparses the whole document; a cap keeps large inputs linear.
614+
$lines[] = 'get_updated_html ' . sha1( $p->get_updated_html() );
615+
}
616+
if ( $roll < $set_below && '#tag' === $p->get_token_type() && ! $p->is_tag_closer() ) {
617+
$lines[] = 'set_attribute ' . var_export( $p->set_attribute( 'data-x', 'y' ), true );
618+
}
619+
}
564620
$attributes = array();
565621
foreach ( (array) $p->get_attribute_names_with_prefix( '' ) as $name ) {
566622
$attributes[] = $name . '=' . var_export( $p->get_attribute( $name ), true );
@@ -596,14 +652,8 @@ function difftrace_child( string $checkout, array $options, array $extra ): arra
596652
array( PHP_BINARY, '-d', 'memory_limit=-1', __FILE__, '--seed', (string) $options['seed'], '--mutations', (string) $options['mutations'] ),
597653
$extra
598654
);
599-
$process = proc_open(
600-
$command,
601-
array(
602-
1 => array( 'pipe', 'w' ),
603-
2 => STDERR,
604-
),
605-
$pipes
606-
);
655+
// Stderr is left out of the spec so the child inherits it: passing STDERR makes PHP seek the shared offset.
656+
$process = proc_open( $command, array( 1 => array( 'pipe', 'w' ) ), $pipes );
607657
$output = stream_get_contents( $pipes[1] );
608658
fclose( $pipes[1] );
609659
if ( 0 !== proc_close( $process ) ) {
@@ -620,7 +670,7 @@ function difftrace_child( string $checkout, array $options, array $extra ): arra
620670
if ( isset( $options['emit-html'] ) ) {
621671
Benchmark_Bootstrap::load( $options['emit-html'] );
622672
foreach ( difftrace_inputs( $seed, (int) $options['mutations'] ) as $id => $html ) {
623-
echo $id, "\t", sha1( implode( "\n", difftrace_html_stream( $html ) ) ), "\n";
673+
echo $id, "\t", sha1( implode( "\n", array_merge( difftrace_html_stream( $html ), array( 'modified' ), difftrace_html_stream( $html, true ) ) ) ), "\n";
624674
}
625675
exit( 0 );
626676
}
@@ -629,7 +679,7 @@ function difftrace_child( string $checkout, array $options, array $extra ): arra
629679
Benchmark_Bootstrap::load( $checkout );
630680
foreach ( difftrace_inputs( $seed, (int) $options['mutations'] ) as $id => $html ) {
631681
if ( $id === $wanted ) {
632-
echo implode( "\n", difftrace_html_stream( $html ) ), "\n";
682+
echo implode( "\n", array_merge( difftrace_html_stream( $html ), array( 'modified' ), difftrace_html_stream( $html, true ) ) ), "\n";
633683
exit( 0 );
634684
}
635685
}
@@ -667,7 +717,7 @@ function difftrace_child( string $checkout, array $options, array $extra ): arra
667717
$shown = 0;
668718
foreach ( difftrace_inputs( $seed, (int) $options['mutations'] ) as $id => $html ) {
669719
++$inputs;
670-
foreach ( array( 'token', 'tag', 'closers', 'query', 'modify', 'bookmarks' ) as $walk ) {
720+
foreach ( array( 'token', 'tag', 'closers', 'query', 'modify', 'bookmarks', 'mixed' ) as $walk ) {
671721
$difference = difftrace_tag_walk( $id, $html, $walk, $seed, $steps );
672722
if ( null === $difference ) {
673723
continue;
@@ -684,7 +734,7 @@ function difftrace_child( string $checkout, array $options, array $extra ): arra
684734
}
685735
}
686736
}
687-
printf( "Tag Processor: %d inputs, 6 walks each, %d steps compared, %d walks differ.\n", $inputs, $steps, $failures );
737+
printf( "Tag Processor: %d inputs, 7 walks each, %d steps compared, %d walks differ.\n", $inputs, $steps, $failures );
688738
}
689739

690740
if ( ! isset( $options['tag-only'] ) ) {

‎tests/performance/html-api/lib/class-benchmark-report.php‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,18 @@ private static function render_summary( array $summary, string $mode, bool $mark
217217
$overall['documents'],
218218
$overall['method'] ?? 'bootstrap'
219219
);
220+
if ( ! empty( $overall['round_geomeans'] ) ) {
221+
// One slow round (a worker stuck on a slow core) widens the interval; show where it came from.
222+
$lines[] = 'Per round: ' . implode(
223+
' ',
224+
array_map(
225+
static function ( $ratio ) {
226+
return Benchmark_Stats::format_pct( ( $ratio - 1.0 ) * 100.0 );
227+
},
228+
$overall['round_geomeans']
229+
)
230+
);
231+
}
220232
}
221233
}
222234

‎tests/performance/html-api/with-lock.php‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
exit( 2 );
2020
}
2121

22-
$lock = Benchmark_Lock::acquire( 'with-lock.php ' . implode( ' ', $command ) );
23-
$process = proc_open( $command, array( STDIN, STDOUT, STDERR ), $pipes );
22+
$lock = Benchmark_Lock::acquire( 'with-lock.php ' . implode( ' ', $command ) );
23+
// An empty spec lets the child inherit stdin, stdout and stderr; passing STDOUT or STDERR makes
24+
// PHP seek a redirected file's shared offset, and the outputs then overwrite each other.
25+
$process = proc_open( $command, array(), $pipes );
2426
if ( false === $process ) {
2527
fwrite( STDERR, 'with-lock.php: could not start ' . $command[0] . "\n" );
2628
exit( 2 );

0 commit comments

Comments
 (0)