Skip to content

Commit ca5ca68

Browse files
committed
improve benchmarks and isolate set up
1 parent 1d180ae commit ca5ca68

1 file changed

Lines changed: 58 additions & 34 deletions

File tree

tests/benchmarks/benchmarks/html-api/WpHtmlTagProcessorBench.php

Lines changed: 58 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,44 +9,43 @@
99

1010
use PhpBench\Attributes as Bench;
1111

12-
#[Bench\Warmup( 3 )]
13-
#[Bench\Iterations( 10 )]
14-
#[Bench\Revs( 100 )]
1512
class WpHtmlTagProcessorBench {
13+
private $processor = null;
14+
15+
public function clean_up_processor(): void {
16+
$this->processor = null;
17+
}
1618

1719
/**
1820
* Benchmark normalizing simple Unix paths.
19-
* @param array{0: WP_HTML_Tag_Processor, 1: string} $params
21+
* @param array{0: string} $params
2022
*/
21-
#[Bench\ParamProviders( 'provide_script_tag_processor' )]
23+
#[Bench\Warmup( 2 )]
24+
#[Bench\Iterations( 10 )]
25+
#[Bench\Revs( 50 )]
26+
#[Bench\BeforeMethods( 'set_up_script_tag_processor' )]
27+
#[Bench\AfterMethods( 'clean_up_processor' )]
28+
#[Bench\ParamProviders( 'provide_script_tag_contents' )]
2229
public function bench_javascript_custom_escape( array $params ): void {
23-
[$processor, $source_text] = $params;
24-
assert( $processor->set_modifiable_text( $source_text ), 'Failed to set modifiable text.' );
30+
[ $source_text] = $params;
31+
assert( $this->processor->set_modifiable_text( $source_text ), 'Failed to set modifiable text.' );
2532
}
2633

27-
/**
28-
* @return iterable<array{0: WP_HTML_Tag_Processor, 1: string}>
29-
*/
30-
public static function provide_script_tag_processor(): iterable {
31-
foreach ( self::provide_javascript() as $name => $source_text ) {
32-
$processor = new WP_HTML_Tag_Processor( '<script></script>' );
33-
$processor->next_tag();
34-
yield $name => array( $processor, $source_text );
35-
}
34+
public function set_up_script_tag_processor(): void {
35+
$this->processor = new WP_HTML_Tag_Processor( '<script></script>' );
36+
$this->processor->next_tag();
3637
}
3738

3839
/**
39-
* Provide simple Unix-style paths.
40-
* @return iterable<string>
40+
* @return iterable<array{0: string}>
4141
*/
42-
public static function provide_javascript(): iterable {
43-
yield 'empty' => '';
44-
45-
yield 'short' => 'console.log("Hello, World!");';
42+
public static function provide_script_tag_contents(): iterable {
43+
yield 'empty' => array( '' );
4644

47-
// yield 'tinymce' => file_get_contents( dirname(__DIR__, 2) . 'src/js/_enqueues/vendor/tinymce/tinymce.js' );
45+
yield 'short' => array( 'console.log("Hello, World!");' );
4846

49-
yield 'many replacements' => <<<'JS'
47+
yield 'many replacements' => array(
48+
<<<'JS'
5049
/* <!-- and <script> is bad news in JS land! */
5150
const templateString = `
5251
But can't we talk about <script> and </script> tags without breaking everything?
@@ -103,46 +102,71 @@ public static function provide_javascript(): iterable {
103102
<script></script>
104103
`;
105104
/* Good luck! */
106-
JS;
105+
JS,
106+
);
107107
}
108108

109109

110110
/**
111111
* Benchmark HTML parsing.
112-
* @param array{0: WP_HTML_Tag_Processor} $params
112+
* @param array{0: string} $params
113113
*/
114+
#[Bench\Warmup( 2 )]
115+
#[Bench\Iterations( 10 )]
116+
#[Bench\Revs( 10 )]
114117
#[Bench\ParamProviders( 'provide_html' )]
118+
#[Bench\BeforeMethods( 'set_up_html_tag_processor' )]
119+
#[Bench\AfterMethods( 'clean_up_processor' )]
115120
public function bench_tag_processor_html_parsing( array $params ): void {
116-
$processor = new WP_HTML_Tag_Processor( $params[0] );
117-
while ( $processor->next_token() ) {
121+
while ( $this->processor->next_token() ) {
118122
// No-op.
119123
}
120124
}
121125

126+
public function set_up_html_tag_processor( array $params ): void {
127+
$this->processor = new WP_HTML_Tag_Processor( $params[0] );
128+
}
129+
122130
/**
123131
* Benchmark HTML parsing.
124-
* @param array{0: WP_HTML_Tag_Processor} $params
132+
* @param array{0: string} $params
125133
*/
134+
#[Bench\Warmup( 2 )]
135+
#[Bench\Iterations( 10 )]
136+
#[Bench\Revs( 5 )]
126137
#[Bench\ParamProviders( 'provide_html' )]
138+
#[Bench\BeforeMethods( 'set_up_html_fragment_processor' )]
139+
#[Bench\AfterMethods( 'clean_up_processor' )]
127140
public function bench_html_fragment_parsing( array $params ): void {
128-
$processor = WP_HTML_Processor::create_fragment( $params[0] );
129-
while ( $processor->next_token() ) {
141+
while ( $this->processor->next_token() ) {
130142
// No-op.
131143
}
132144
}
133145

146+
public function set_up_html_fragment_processor( array $params ): void {
147+
$this->processor = WP_HTML_Processor::create_fragment( $params[0] );
148+
}
149+
134150
/**
135151
* Benchmark HTML parsing.
136-
* @param array{0: WP_HTML_Tag_Processor} $params
152+
* @param array{0: string} $params
137153
*/
154+
#[Bench\Warmup( 2 )]
155+
#[Bench\Iterations( 10 )]
156+
#[Bench\Revs( 5 )]
138157
#[Bench\ParamProviders( 'provide_html' )]
158+
#[Bench\BeforeMethods( 'set_up_html_full_parser' )]
159+
#[Bench\AfterMethods( 'clean_up_processor' )]
139160
public function bench_html_full_parsing( array $params ): void {
140-
$processor = WP_HTML_Processor::create_full_parser( $params[0] );
141-
while ( $processor->next_token() ) {
161+
while ( $this->processor->next_token() ) {
142162
// No-op.
143163
}
144164
}
145165

166+
public function set_up_html_full_parser( array $params ): void {
167+
$this->processor = WP_HTML_Processor::create_full_parser( $params[0] );
168+
}
169+
146170
public static function provide_html(): iterable {
147171
yield 'Empty string' => array( '' );
148172
yield 'Short doc' => array( '<h1>Hello, world!</h1>' );

0 commit comments

Comments
 (0)