forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Benchmark setup HTML API #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
sirreal
wants to merge
18
commits into
trunk
Choose a base branch
from
bench-html-api
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
cfd3ca4
Set up for phpbench
sirreal 5d60bdb
phpbench setup
sirreal 22f75fd
Merge branch 'sirreal-benchmarking' into bench-html-api
sirreal 1fdd283
benchmarking js escaping
sirreal bbdd9a9
set_modifiable_text benches
sirreal 5d6cbd5
Fix php test data constant
sirreal a3b04f4
Add html processing benchmarks
sirreal 1bfbeff
Cleanup lints
sirreal 46f03ce
Add test html file
sirreal f52bb42
reorganize benchmarks
sirreal 1d180ae
Exclude benchmarks from phpcs
sirreal ca5ca68
improve benchmarks and isolate set up
sirreal 9aee018
lint
sirreal 93e20b0
prevent embedding huge HTML docs in XML results
sirreal f2f9fc2
tweak revolusions and iterations
sirreal d692e1c
Fix data URIs
sirreal 06b0370
Try using global namespace for special functions
sirreal c1094c3
Revert "Try using global namespace for special functions"
sirreal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| <?php | ||
| /** | ||
| * Bootstrap file for phpbench benchmarks. | ||
| * | ||
| * Loads WordPress for running benchmarks. | ||
| */ | ||
|
|
||
| define( 'PHPBENCH_ROOT', __DIR__ ); | ||
|
|
||
| if ( defined( 'WP_TESTS_CONFIG_FILE_PATH' ) ) { | ||
| $config_file_path = WP_TESTS_CONFIG_FILE_PATH; | ||
| } else { | ||
| $config_file_path = __DIR__ . '/wp-tests-config.php'; | ||
| } | ||
|
|
||
| /* | ||
| * Globalize some WordPress variables. | ||
| */ | ||
| global $wpdb, $current_site, $current_blog, $wp_rewrite, $shortcode_tags, $wp, $phpmailer, $wp_theme_directories; | ||
|
|
||
| if ( ! is_readable( $config_file_path ) ) { | ||
| exit( 1 ); | ||
| } | ||
|
|
||
| require_once $config_file_path; | ||
|
|
||
| if ( ! is_dir( ABSPATH ) ) { | ||
| exit( 1 ); | ||
| } | ||
|
Comment on lines
+27
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| function tests_reset__SERVER() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid | ||
| $_SERVER['HTTP_HOST'] = WP_TESTS_DOMAIN; | ||
| $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; | ||
| $_SERVER['REQUEST_METHOD'] = 'GET'; | ||
| $_SERVER['REQUEST_URI'] = ''; | ||
| $_SERVER['SERVER_NAME'] = WP_TESTS_DOMAIN; | ||
| $_SERVER['SERVER_PORT'] = '80'; | ||
| $_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1'; | ||
|
|
||
| unset( $_SERVER['HTTP_REFERER'] ); | ||
| unset( $_SERVER['HTTPS'] ); | ||
| } | ||
| tests_reset__SERVER(); | ||
|
|
||
| define( 'WP_TESTS_TABLE_PREFIX', $table_prefix ); | ||
|
|
||
| define( 'DIR_BENCHMARKDATA', __DIR__ . '/tests/benchmarks/data' ); | ||
| define( 'DIR_TESTDATA', __DIR__ . '/tests/phpunit/data' ); | ||
| define( 'WP_LANG_DIR', realpath( DIR_TESTDATA . '/languages' ) ); | ||
|
|
||
| /* | ||
| * Cron tries to make an HTTP request to the site, which always fails, | ||
| * because tests are run in CLI mode only. | ||
| */ | ||
| define( 'DISABLE_WP_CRON', true ); | ||
|
|
||
| define( 'WP_MEMORY_LIMIT', -1 ); | ||
| define( 'WP_MAX_MEMORY_LIMIT', -1 ); | ||
|
|
||
| $PHP_SELF = '/index.php'; | ||
| $GLOBALS['PHP_SELF'] = '/index.php'; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| $_SERVER['PHP_SELF'] = '/index.php'; | ||
|
|
||
| if ( ! defined( 'WP_DEFAULT_THEME' ) ) { | ||
| define( 'WP_DEFAULT_THEME', 'default' ); | ||
| } | ||
| $wp_theme_directories = array(); | ||
|
|
||
| if ( file_exists( DIR_TESTDATA . '/themedir1' ) ) { | ||
| $wp_theme_directories[] = DIR_TESTDATA . '/themedir1'; | ||
| } | ||
|
|
||
| // Load WordPress. | ||
| require_once ABSPATH . 'wp-settings.php'; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| "$schema": "./vendor/phpbench/phpbench/phpbench.schema.json", | ||
| "runner.bootstrap": "phpbench-bootstrap.php", | ||
| "runner.path": "tests/benchmarks/benchmarks" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
177 changes: 177 additions & 0 deletions
177
tests/benchmarks/benchmarks/html-api/WpHtmlTagProcessorBench.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| <?php | ||
| /** | ||
| * Benchmarks for WP_HTML_Tag_Processor. | ||
| * | ||
| * @package WordPress | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use PhpBench\Attributes as Bench; | ||
|
|
||
| class WpHtmlTagProcessorBench { | ||
| /** | ||
| * Processor instance for benchmarks. | ||
| */ | ||
| private WP_HTML_Tag_Processor|WP_HTML_Processor|null $processor = null; | ||
|
|
||
| public function clean_up_processor(): void { | ||
| $this->processor = null; | ||
| } | ||
|
|
||
| /** | ||
| * Benchmark normalizing simple Unix paths. | ||
| * @param array{0: string} $params | ||
| */ | ||
| #[Bench\Warmup( 2 )] | ||
| #[Bench\Iterations( 50 )] | ||
| #[Bench\Revs( 10 )] | ||
| #[Bench\BeforeMethods( 'set_up_script_tag_processor' )] | ||
| #[Bench\AfterMethods( 'clean_up_processor' )] | ||
| #[Bench\ParamProviders( 'provide_script_tag_contents' )] | ||
| public function bench_javascript_custom_escape( array $params ): void { | ||
| [ $source_text] = $params; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| assert( $this->processor->set_modifiable_text( $source_text ), 'Failed to set modifiable text.' ); | ||
| } | ||
|
|
||
| public function set_up_script_tag_processor(): void { | ||
| $this->processor = new WP_HTML_Tag_Processor( '<script></script>' ); | ||
| $this->processor->next_tag(); | ||
| } | ||
|
|
||
| /** | ||
| * @return iterable<array{0: string}> | ||
| */ | ||
| public static function provide_script_tag_contents(): iterable { | ||
| yield 'empty' => array( '' ); | ||
|
|
||
| yield 'short' => array( 'console.log("Hello, World!");' ); | ||
|
|
||
| yield 'many replacements' => array( | ||
| <<<'JS' | ||
| /* <!-- and <script> is bad news in JS land! */ | ||
| const templateString = ` | ||
| But can't we talk about <script> and </script> tags without breaking everything? | ||
| </script> | ||
| </SCRIPT> | ||
| </SCRIPT > | ||
| </SCRIPT | ||
| > | ||
| </SCRIPT/> | ||
| </script/> | ||
| </script ignored attributes /> | ||
| </script /> | ||
| <script></script> | ||
| <script></script> | ||
| <script></script> | ||
| <script></script> | ||
| <script></script> | ||
| <script></script> | ||
| <script></script> | ||
| <script></script> | ||
| <script></script> | ||
| <script></script> | ||
| <script></script> | ||
| <script></script> | ||
| <script></script> | ||
| <script></script> | ||
| <script></script> | ||
| <script></script> | ||
| <script></script> | ||
| <script></script> | ||
| <script></script> | ||
| <script></script> | ||
| <script></script> | ||
| <script></script> | ||
| <script></script> | ||
| <script></script> | ||
| <script></script> | ||
| <script></script> | ||
| <script></script> | ||
| <script></script> | ||
| <script></script> | ||
| <script></script> | ||
| <script></script> | ||
| <script></script> | ||
| <script></script> | ||
| <script></script> | ||
| <script></script> | ||
| <script></script> | ||
| <script></script> | ||
| <script></script> | ||
| <script></script> | ||
| <script></script> | ||
| <script></script> | ||
| <script></script> | ||
| `; | ||
| /* Good luck! */ | ||
| JS, | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Benchmark HTML parsing. | ||
| * @param array{0: string} $params | ||
| */ | ||
| #[Bench\Warmup( 2 )] | ||
| #[Bench\Iterations( 20 )] | ||
| #[Bench\Revs( 5 )] | ||
| #[Bench\ParamProviders( 'provide_html' )] | ||
| #[Bench\BeforeMethods( 'set_up_html_tag_processor' )] | ||
| #[Bench\AfterMethods( 'clean_up_processor' )] | ||
| public function bench_tag_processor_html_parsing( array $params ): void { | ||
| while ( $this->processor->next_token() ) { | ||
| // No-op. | ||
| } | ||
| } | ||
|
|
||
| public function set_up_html_tag_processor( array $params ): void { | ||
| $this->processor = new WP_HTML_Tag_Processor( file_get_contents( $params[0] ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Benchmark HTML parsing. | ||
| * @param array{0: string} $params | ||
| */ | ||
| #[Bench\Warmup( 2 )] | ||
| #[Bench\Iterations( 20 )] | ||
| #[Bench\Revs( 3 )] | ||
| #[Bench\ParamProviders( 'provide_html' )] | ||
| #[Bench\BeforeMethods( 'set_up_html_fragment_processor' )] | ||
| #[Bench\AfterMethods( 'clean_up_processor' )] | ||
| public function bench_html_fragment_parsing( array $params ): void { | ||
| while ( $this->processor->next_token() ) { | ||
| // No-op. | ||
| } | ||
| } | ||
|
|
||
| public function set_up_html_fragment_processor( array $params ): void { | ||
| $this->processor = WP_HTML_Processor::create_fragment( file_get_contents( $params[0] ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Benchmark HTML parsing. | ||
| * @param array{0: string} $params | ||
| */ | ||
| #[Bench\Warmup( 2 )] | ||
| #[Bench\Iterations( 20 )] | ||
| #[Bench\Revs( 3 )] | ||
| #[Bench\ParamProviders( 'provide_html' )] | ||
| #[Bench\BeforeMethods( 'set_up_html_full_parser' )] | ||
| #[Bench\AfterMethods( 'clean_up_processor' )] | ||
| public function bench_html_full_parsing( array $params ): void { | ||
| while ( $this->processor->next_token() ) { | ||
| // No-op. | ||
| } | ||
| } | ||
|
|
||
| public function set_up_html_full_parser( array $params ): void { | ||
| $this->processor = WP_HTML_Processor::create_full_parser( file_get_contents( $params[0] ) ); | ||
| } | ||
|
|
||
| public static function provide_html(): iterable { | ||
| yield 'Empty string' => array( 'data:text/html,' ); | ||
| yield 'Short doc' => array( 'data:text/html,' . rawurlencode( '<h1>Hello, world!</h1>' ) ); | ||
| yield 'HTML Standard' => array( 'file://' . DIR_BENCHMARKDATA . '/html-standard.html' ); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For better debugging, it's helpful to output an error message to
stderrwhen the script exits due to a fatal condition. This will make it easier to diagnose setup issues when running the benchmarks.