|
9 | 9 |
|
10 | 10 | use PhpBench\Attributes as Bench; |
11 | 11 |
|
12 | | -#[Bench\Warmup( 3 )] |
13 | | -#[Bench\Iterations( 10 )] |
14 | | -#[Bench\Revs( 100 )] |
15 | 12 | class WpHtmlTagProcessorBench { |
| 13 | + private $processor = null; |
| 14 | + |
| 15 | + public function clean_up_processor(): void { |
| 16 | + $this->processor = null; |
| 17 | + } |
16 | 18 |
|
17 | 19 | /** |
18 | 20 | * Benchmark normalizing simple Unix paths. |
19 | | - * @param array{0: WP_HTML_Tag_Processor, 1: string} $params |
| 21 | + * @param array{0: string} $params |
20 | 22 | */ |
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' )] |
22 | 29 | 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.' ); |
25 | 32 | } |
26 | 33 |
|
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(); |
36 | 37 | } |
37 | 38 |
|
38 | 39 | /** |
39 | | - * Provide simple Unix-style paths. |
40 | | - * @return iterable<string> |
| 40 | + * @return iterable<array{0: string}> |
41 | 41 | */ |
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( '' ); |
46 | 44 |
|
47 | | - // yield 'tinymce' => file_get_contents( dirname(__DIR__, 2) . 'src/js/_enqueues/vendor/tinymce/tinymce.js' ); |
| 45 | + yield 'short' => array( 'console.log("Hello, World!");' ); |
48 | 46 |
|
49 | | - yield 'many replacements' => <<<'JS' |
| 47 | + yield 'many replacements' => array( |
| 48 | + <<<'JS' |
50 | 49 | /* <!-- and <script> is bad news in JS land! */ |
51 | 50 | const templateString = ` |
52 | 51 | But can't we talk about <script> and </script> tags without breaking everything? |
@@ -103,46 +102,71 @@ public static function provide_javascript(): iterable { |
103 | 102 | <script></script> |
104 | 103 | `; |
105 | 104 | /* Good luck! */ |
106 | | - JS; |
| 105 | + JS, |
| 106 | + ); |
107 | 107 | } |
108 | 108 |
|
109 | 109 |
|
110 | 110 | /** |
111 | 111 | * Benchmark HTML parsing. |
112 | | - * @param array{0: WP_HTML_Tag_Processor} $params |
| 112 | + * @param array{0: string} $params |
113 | 113 | */ |
| 114 | + #[Bench\Warmup( 2 )] |
| 115 | + #[Bench\Iterations( 10 )] |
| 116 | + #[Bench\Revs( 10 )] |
114 | 117 | #[Bench\ParamProviders( 'provide_html' )] |
| 118 | + #[Bench\BeforeMethods( 'set_up_html_tag_processor' )] |
| 119 | + #[Bench\AfterMethods( 'clean_up_processor' )] |
115 | 120 | 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() ) { |
118 | 122 | // No-op. |
119 | 123 | } |
120 | 124 | } |
121 | 125 |
|
| 126 | + public function set_up_html_tag_processor( array $params ): void { |
| 127 | + $this->processor = new WP_HTML_Tag_Processor( $params[0] ); |
| 128 | + } |
| 129 | + |
122 | 130 | /** |
123 | 131 | * Benchmark HTML parsing. |
124 | | - * @param array{0: WP_HTML_Tag_Processor} $params |
| 132 | + * @param array{0: string} $params |
125 | 133 | */ |
| 134 | + #[Bench\Warmup( 2 )] |
| 135 | + #[Bench\Iterations( 10 )] |
| 136 | + #[Bench\Revs( 5 )] |
126 | 137 | #[Bench\ParamProviders( 'provide_html' )] |
| 138 | + #[Bench\BeforeMethods( 'set_up_html_fragment_processor' )] |
| 139 | + #[Bench\AfterMethods( 'clean_up_processor' )] |
127 | 140 | 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() ) { |
130 | 142 | // No-op. |
131 | 143 | } |
132 | 144 | } |
133 | 145 |
|
| 146 | + public function set_up_html_fragment_processor( array $params ): void { |
| 147 | + $this->processor = WP_HTML_Processor::create_fragment( $params[0] ); |
| 148 | + } |
| 149 | + |
134 | 150 | /** |
135 | 151 | * Benchmark HTML parsing. |
136 | | - * @param array{0: WP_HTML_Tag_Processor} $params |
| 152 | + * @param array{0: string} $params |
137 | 153 | */ |
| 154 | + #[Bench\Warmup( 2 )] |
| 155 | + #[Bench\Iterations( 10 )] |
| 156 | + #[Bench\Revs( 5 )] |
138 | 157 | #[Bench\ParamProviders( 'provide_html' )] |
| 158 | + #[Bench\BeforeMethods( 'set_up_html_full_parser' )] |
| 159 | + #[Bench\AfterMethods( 'clean_up_processor' )] |
139 | 160 | 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() ) { |
142 | 162 | // No-op. |
143 | 163 | } |
144 | 164 | } |
145 | 165 |
|
| 166 | + public function set_up_html_full_parser( array $params ): void { |
| 167 | + $this->processor = WP_HTML_Processor::create_full_parser( $params[0] ); |
| 168 | + } |
| 169 | + |
146 | 170 | public static function provide_html(): iterable { |
147 | 171 | yield 'Empty string' => array( '' ); |
148 | 172 | yield 'Short doc' => array( '<h1>Hello, world!</h1>' ); |
|
0 commit comments