Skip to content

Commit c3dde4f

Browse files
authored
Speed up WordPress PHPUnit tests (#619)
## Summary The WordPress PHPUnit job takes about 10 minutes, and most of that time is PHPUnit itself. This PR speeds it up with two small changes that only affect the tests: - **Turn off PHP's garbage collector for PHPUnit.** While tests run, PHP keeps searching a large amount of memory for objects it can free, and it rarely finds much. In a short-lived test process, this isn't worth the time. - **Use the low bcrypt cost in class fixtures too.** `WP_UnitTestCase::set_up()` lowers the bcrypt cost for each test, but `wpSetUpBeforeClass()` runs before it. Users created there still hash passwords at PHP's default cost, and those hashes took most of the time spent in class fixtures. Test order and the list of expected failures stay unchanged. **Memory use goes up.** A full WordPress PHPUnit run now peaks at about 4.2 GB instead of about 0.5 GB. This fits the 16 GB CI runner, but a local Docker setup needs that much memory available for a full run. The WordPress test bootstrap already removes the PHP memory limit, so no new limit setting is needed. The SQLite plugin suites use the same command and use very little memory either way. The bcrypt issue is in WordPress's own test code, so it could also be fixed there. Until then, `wp-setup.sh` adds the filter, as it already does for other test changes. ## Why PHPUnit takes about 7.5 minutes of the job on slower runners, and the job is by far the slowest one in CI. Profiling showed that the SQLite database itself is under 5% of that time, so these changes target where the time actually goes. ## Measured speedups **On CI:** GitHub-hosted runners vary a lot in speed, so each run is compared with baseline runs on a runner of similar speed. As in #579, the webpack build time from the same job shows the runner's speed. The baseline is 14 recent runs without this change, 12 of which match these two speeds. | Runner | Baseline PHPUnit | This PR | Change | PHPUnit ÷ build time | Whole job | | --- | ---: | ---: | ---: | ---: | ---: | | Slower (build about 14s) | 464s (10 runs) | [286s](https://github.com/WordPress/sqlite-database-integration/actions/runs/42244888948/attempts/3) | **~38% faster** | 32.9 → 21.2 | ~10m50s → 7m35s | | Faster (build about 9s) | 311s (2 runs) | [172s](https://github.com/WordPress/sqlite-database-integration/actions/runs/42244888948/attempts/2) | **~45% faster** | 35.8 → 19.0 | ~7m30s → 5m13s | These are two runs of this PR, one on each runner speed. Test results match the baseline runs: 30,900 tests, with the same errors and failures, and the expected failures check passes. Peak memory went from 515 MB to 4.18 GB. **Locally:** the full suite on native PHP 8.3 (not Docker), in seconds. Each row adds to the one above it. The list of failing tests was identical in all three runs. | Mode | PHPUnit | Change | | --- | ---: | ---: | | Current settings | 197.7s | — | | Garbage collector off | 153.8s | **~22% faster** | | Garbage collector off and low bcrypt cost in fixtures | 126.6s | **~36% faster** | <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **Chores** * Updated PHP test execution settings and WordPress test setup. These changes affect the project’s testing configuration; no end-user-facing changes are included. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 586046e commit c3dde4f

2 files changed

Lines changed: 18 additions & 4 deletions

File tree

‎composer.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
"wp-test-php": [
6868
"@wp-test-ensure-env @no_additional_args",
6969
"@wp-test-remove-database @no_additional_args",
70-
"cd wordpress && node tools/local-env/scripts/docker.js run --rm php php -d opcache.enable_cli=1 -d opcache.memory_consumption=256 -d opcache.jit=tracing -d opcache.jit_buffer_size=64M ./vendor/bin/phpunit @additional_args"
70+
"cd wordpress && node tools/local-env/scripts/docker.js run --rm php php -d zend.enable_gc=0 -d opcache.enable_cli=1 -d opcache.memory_consumption=256 -d opcache.jit=tracing -d opcache.jit_buffer_size=64M ./vendor/bin/phpunit @additional_args"
7171
],
7272
"wp-test-sqlite-plugin-php": [
7373
"@wp-test-php /var/www/tests-sqlite-database-integration/phpunit"

‎wp-setup.sh‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,29 @@ sed -i.bak "s#{SQLITE_PLUGIN}#sqlite-database-integration/load.php#g" "$WP_DIR"/
6262
echo "Rewriting helper class 'WpdbExposedMethodsForTesting' to extend WP_SQLite_DB..."
6363
sed -i.bak "s#class WpdbExposedMethodsForTesting extends wpdb {#class WpdbExposedMethodsForTesting extends WP_SQLite_DB {#g" "$WP_DIR"/tests/phpunit/includes/utils.php
6464

65-
# 6. Make wp-config.php changes immediately visible to E2E tests.
65+
# 6. Use the low bcrypt cost of the test suite in class fixtures as well.
66+
# WP_UnitTestCase::set_up() sets it for each test, which is too late for wpSetUpBeforeClass().
67+
echo "Lowering the bcrypt cost for test fixtures..."
68+
cat << 'EOF' >> "$WP_DIR/tests/phpunit/includes/functions.php"
69+
70+
function _sqlite_tests_lower_bcrypt_cost( $options, $algorithm ) {
71+
if ( PASSWORD_BCRYPT === $algorithm ) {
72+
$options['cost'] = 5;
73+
}
74+
return $options;
75+
}
76+
tests_add_filter( 'wp_hash_password_options', '_sqlite_tests_lower_bcrypt_cost', 1, 2 );
77+
EOF
78+
79+
# 7. Make wp-config.php changes immediately visible to E2E tests.
6680
echo "Disabling the PHP OPcache revalidation delay..."
6781
printf '\nopcache.revalidate_freq = 0\n' >> "$WP_DIR/tools/local-env/php-config.ini"
6882

69-
# 7. Create the ".env" file early, so that Docker commands before "env:start" use it too.
83+
# 8. Create the ".env" file early, so that Docker commands before "env:start" use it too.
7084
echo "Creating the '.env' file..."
7185
cp "$WP_DIR/.env.example" "$WP_DIR/.env"
7286

73-
# 8. Install dependencies.
87+
# 9. Install dependencies.
7488
echo "Installing dependencies..."
7589
npm --prefix "$WP_DIR" install
7690
npm --prefix "$WP_DIR" run build:dev

0 commit comments

Comments
 (0)