Skip to content

Commit 1b7b779

Browse files
authored
Merge pull request #401 from wp-cli/perf/child-process-php-settings
Launch WP-CLI processes with cheaper PHP settings
2 parents 9ed5cd4 + 5915cb0 commit 1b7b779

2 files changed

Lines changed: 85 additions & 0 deletions

File tree

‎features/behat-steps.feature‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,30 @@ Feature: Test that WP-CLI Behat steps work as expected
3333
Given a non-existent test-dir directory
3434
Then the test-dir directory should not exist
3535

36+
Scenario: Always run the current version of files created by a scenario
37+
Given an empty directory
38+
And a script.php file:
39+
"""
40+
<?php echo 'first';
41+
"""
42+
43+
When I run `wp --require=script.php --skip-wordpress eval ";"`
44+
Then STDOUT should be:
45+
"""
46+
first
47+
"""
48+
49+
Given a script.php file:
50+
"""
51+
<?php echo 'other';
52+
"""
53+
54+
When I run `wp --require=script.php --skip-wordpress eval ";"`
55+
Then STDOUT should be:
56+
"""
57+
other
58+
"""
59+
3660
Scenario: Test "Given an empty cache" step
3761
Given a WP installation
3862
And an empty cache

‎src/Context/FeatureContext.php‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,8 @@ private static function get_process_env_variables(): array {
508508
$env['WP_CLI_REQUIRE'] = $updated;
509509
}
510510

511+
$env = array_merge( $env, self::get_php_tuning_env_variables() );
512+
511513
$config_path = getenv( 'WP_CLI_CONFIG_PATH' );
512514
if ( false !== $config_path ) {
513515
$env['WP_CLI_CONFIG_PATH'] = $config_path;
@@ -557,6 +559,65 @@ private static function get_process_env_variables(): array {
557559
return $env;
558560
}
559561

562+
/**
563+
* Get the environment variables that make the PHP processes launched by the tests cheaper.
564+
*
565+
* Xdebug is switched off unless code coverage is collected or a mode or session is requested
566+
* explicitly. The code of WP-CLI and of the project under test is kept in a file-based opcode
567+
* cache shared by all processes. Everything below the temporary directory is excluded from that
568+
* cache, as scenarios create and rewrite PHP files there and must always run the current version.
569+
*
570+
* Set WP_CLI_TEST_PHP_TUNING=off to launch the processes with the PHP configuration as it is.
571+
*
572+
* @return array<string, string>
573+
*/
574+
private static function get_php_tuning_env_variables(): array {
575+
if ( 'off' === getenv( 'WP_CLI_TEST_PHP_TUNING' ) || self::running_with_code_coverage() ) {
576+
return [];
577+
}
578+
579+
$env = [];
580+
581+
if ( false === getenv( 'XDEBUG_MODE' ) && false === getenv( 'XDEBUG_SESSION' ) && false === getenv( 'XDEBUG_CONFIG' ) ) {
582+
$env['XDEBUG_MODE'] = 'off';
583+
}
584+
585+
// The file cache cannot be used on its own on Windows.
586+
if ( Utils\is_windows() ) {
587+
return $env;
588+
}
589+
590+
$temp_dir = rtrim( sys_get_temp_dir(), '/' );
591+
$config_dir = "{$temp_dir}/wp-cli-test-php-config";
592+
$cache_dir = "{$temp_dir}/wp-cli-test-opcache";
593+
594+
foreach ( [ $config_dir, $cache_dir ] as $dir ) {
595+
if ( ! is_dir( $dir ) && ! @mkdir( $dir, 0777, true ) && ! is_dir( $dir ) ) {
596+
return $env;
597+
}
598+
}
599+
600+
$excluded = array_unique( array_filter( [ $temp_dir, realpath( $temp_dir ) ] ) );
601+
file_put_contents( "{$config_dir}/opcache-blacklist.txt", implode( "/\n", $excluded ) . "/\n" );
602+
603+
$ini = [
604+
'opcache.enable_cli=1',
605+
"opcache.file_cache={$cache_dir}",
606+
'opcache.file_cache_only=1',
607+
"opcache.blacklist_filename={$config_dir}/opcache-blacklist.txt",
608+
];
609+
file_put_contents( "{$config_dir}/wp-cli-tests.ini", implode( "\n", $ini ) . "\n" );
610+
611+
// Add to the directories PHP scans for configuration files rather than replacing them.
612+
$scan_dirs = getenv( 'PHP_INI_SCAN_DIR' );
613+
if ( false === $scan_dirs ) {
614+
$scan_dirs = PHP_CONFIG_FILE_SCAN_DIR;
615+
}
616+
$env['PHP_INI_SCAN_DIR'] = ( '' !== $scan_dirs ? $scan_dirs . PATH_SEPARATOR : '' ) . $config_dir;
617+
618+
return $env;
619+
}
620+
560621
/**
561622
* Get the internal variables to use within tests.
562623
*

0 commit comments

Comments
 (0)