Skip to content

Commit 5ec9b93

Browse files
authored
Merge pull request #405 from wp-cli/perf/parallel-scenarios
Run scenarios in parallel workers
2 parents d2c7f06 + 10e2ae7 commit 5ec9b93

6 files changed

Lines changed: 397 additions & 4 deletions

File tree

‎.readme-partials/USING.md‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,17 @@ composer behat -- --stop-on-failure
254254
composer behat-rerun
255255
```
256256
257+
### Running scenarios in parallel
258+
259+
Set `WP_CLI_TEST_PARALLEL` to a number of workers to spread the scenarios over that many Behat processes:
260+
```bash
261+
WP_CLI_TEST_PARALLEL=4 composer behat
262+
```
263+
264+
Every worker but the first gets its own temporary directory, and with it its own home directory, caches and run directories, as well as its own test database, named after the test database with a numeric suffix (`wp_cli_test_1`, `wp_cli_test_2`, ...). Run `composer prepare-tests` once to allow the test user to create these databases. Without that permission, the scenarios are run one after the other, as they are on Windows, when collecting code coverage, or when selecting scenarios by path or name.
265+
266+
Scenarios that rely on resources shared across the system, such as a fixed port or a fixed path in the system's temporary directory, are all run by the first worker, one after the other. `composer behat-rerun` re-runs exactly the scenarios that failed in the last parallel run.
267+
257268
### Linting the feature files
258269

259270
`composer lint-gherkin` checks `features/` with

‎bin/install-package-tests‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ install_mysql_db_8_0_plus() {
150150
${CLIENT_BINARY} -e "CREATE USER IF NOT EXISTS \`${TEST_USER}\`@'%' IDENTIFIED WITH caching_sha2_password BY '${TEST_PASSWORD}'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
151151
${CLIENT_BINARY} -e "GRANT ALL PRIVILEGES ON \`${TEST_DB}\`.* TO '${TEST_USER}'@'%'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
152152
${CLIENT_BINARY} -e "GRANT ALL PRIVILEGES ON \`${TEST_DB}_scaffold\`.* TO '${TEST_USER}'@'%'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
153+
# The databases of parallel test workers, named after the test database with a numeric suffix.
154+
${CLIENT_BINARY} -e "GRANT ALL PRIVILEGES ON \`${TEST_DB}\\_%\`.* TO '${TEST_USER}'@'%'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
153155
{ set +ex; } 2> /dev/null # stop printing the commands
154156
}
155157

@@ -159,6 +161,8 @@ install_mysql_db_lower_than_8_0() {
159161
${CLIENT_BINARY} -e "CREATE DATABASE IF NOT EXISTS \`${TEST_DB}\`;" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
160162
${CLIENT_BINARY} -e "GRANT ALL ON \`${TEST_DB}\`.* TO '${TEST_USER}'@'%' IDENTIFIED BY '${TEST_PASSWORD}'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
161163
${CLIENT_BINARY} -e "GRANT ALL ON \`${TEST_DB}_scaffold\`.* TO '${TEST_USER}'@'%' IDENTIFIED BY '${TEST_PASSWORD}'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
164+
# The databases of parallel test workers, named after the test database with a numeric suffix.
165+
${CLIENT_BINARY} -e "GRANT ALL ON \`${TEST_DB}\\_%\`.* TO '${TEST_USER}'@'%' IDENTIFIED BY '${TEST_PASSWORD}'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
162166
{ set +ex; } 2> /dev/null # stop printing the commands
163167
}
164168

@@ -169,6 +173,8 @@ install_mariadb() {
169173
${CLIENT_BINARY} -e "CREATE USER IF NOT EXISTS \`${TEST_USER}\`@'%' IDENTIFIED BY '${TEST_PASSWORD}'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
170174
${CLIENT_BINARY} -e "GRANT ALL PRIVILEGES ON \`${TEST_DB}\`.* TO '${TEST_USER}'@'%'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
171175
${CLIENT_BINARY} -e "GRANT ALL PRIVILEGES ON \`${TEST_DB}_scaffold\`.* TO '${TEST_USER}'@'%'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
176+
# The databases of parallel test workers, named after the test database with a numeric suffix.
177+
${CLIENT_BINARY} -e "GRANT ALL PRIVILEGES ON \`${TEST_DB}\\_%\`.* TO '${TEST_USER}'@'%'" ${HOST_STRING} -u"${USER}" "${PASSWORD_STRING}"
172178
}
173179

174180
if [ "${TYPE}" = "MariaDB" ]; then

‎bin/run-behat-tests‎

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,10 @@ else
7878

7979
DBNAME=${WP_CLI_TEST_DBNAME:-wp_cli_test}
8080

81-
# We need to test the connection.
82-
# Let's try to connect to the specific test database.
83-
if ! ${DB_CLIENT} ${HOST_STRING} --user="${USER}" ${PASSWORD_ARG} --execute="USE \`${DBNAME}\`;" 2>/dev/null; then
81+
# We need to test the connection, and that the test user may create the test database.
82+
# The database itself does not need to exist, as the scenarios drop and create it, which
83+
# is also why a rerun after a run in parallel workers usually finds it missing.
84+
if ! ${DB_CLIENT} ${HOST_STRING} --user="${USER}" ${PASSWORD_ARG} --execute="CREATE DATABASE IF NOT EXISTS \`${DBNAME}\`;" 2>/dev/null; then
8485
echo "Warning: Could not connect to the MySQL/MariaDB database."
8586
echo "Please make sure the database is running and run 'composer prepare-tests' once to set it up."
8687
echo "The tests will continue to be run, but with WP_CLI_TEST_DBTYPE=sqlite."
@@ -270,4 +271,39 @@ for arg in "$@"; do
270271
fi
271272
done
272273

274+
# Run the scenarios in parallel workers when WP_CLI_TEST_PARALLEL is set to their number.
275+
# Every worker but the first uses its own test database, which the test user needs
276+
# to be allowed to create, as set up by "composer prepare-tests".
277+
PARALLEL="${WP_CLI_TEST_PARALLEL-}"
278+
279+
# A rerun after a parallel run covers exactly the scenarios that failed in it, which only the
280+
# parallel runner knows, whether or not WP_CLI_TEST_PARALLEL is set for the rerun as well.
281+
for arg in "$@"; do
282+
if [ "$arg" = "--rerun" ] && [ -f "${TMPDIR:-/tmp}/wp-cli-test-parallel-failures.scenarios" ]; then
283+
exec php "$WP_CLI_TESTS_ROOT"/utils/behat-parallel.php 1 --snippets-for="WP_CLI\Tests\Context\FeatureContext" "${FORMAT_ARGS[@]}" "$BEHAT_TAGS" --strict "${BEHAT_EXTRA_ARGS[@]}" -- "$@"
284+
fi
285+
done
286+
287+
case "$(uname -s)" in
288+
MINGW*|MSYS*|CYGWIN*)
289+
PARALLEL="";;
290+
esac
291+
if [ -n "${PARALLEL}" ] && [ "${PARALLEL}" != "1" ]; then
292+
if ! is_numeric "${PARALLEL}"; then
293+
echo "Warning: WP_CLI_TEST_PARALLEL is not a number of workers, running the scenarios one after the other." >&2
294+
PARALLEL=""
295+
elif [[ "${WP_CLI_TEST_COVERAGE}" == "true" ]]; then
296+
echo "Code coverage is collected, running the scenarios one after the other." >&2
297+
PARALLEL=""
298+
elif [ "${WP_CLI_TEST_DBTYPE-}" != "sqlite" ] && ! ${DB_CLIENT} ${HOST_STRING} --user="${USER}" ${PASSWORD_ARG} --execute="CREATE DATABASE IF NOT EXISTS \`${DBNAME}_1\`; DROP DATABASE \`${DBNAME}_1\`;" 2>/dev/null; then
299+
echo "Warning: The test user cannot create the databases of parallel workers, running the scenarios one after the other." >&2
300+
echo "Run 'composer prepare-tests' once to allow it." >&2
301+
PARALLEL=""
302+
fi
303+
fi
304+
305+
if [ -n "${PARALLEL}" ] && [ "${PARALLEL}" != "1" ]; then
306+
exec php "$WP_CLI_TESTS_ROOT"/utils/behat-parallel.php "${PARALLEL}" --snippets-for="WP_CLI\Tests\Context\FeatureContext" "${FORMAT_ARGS[@]}" "$BEHAT_TAGS" --strict "${BEHAT_EXTRA_ARGS[@]}" -- "$@"
307+
fi
308+
273309
vendor/bin/behat --snippets-for="WP_CLI\Tests\Context\FeatureContext" "${FORMAT_ARGS[@]}" "$BEHAT_TAGS" --strict "${BEHAT_EXTRA_ARGS[@]}" "$@"

‎phpcs.xml.dist‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,11 @@
7979
so this file does not have to comply with WP naming conventions. -->
8080
<rule ref="WordPress.NamingConventions.PrefixAllGlobals">
8181
<exclude-pattern>*/utils/behat-tags\.php$</exclude-pattern>
82+
<exclude-pattern>*/utils/behat-parallel\.php$</exclude-pattern>
8283
</rule>
8384
<rule ref="WordPress.WP.GlobalVariablesOverride">
8485
<exclude-pattern>*/utils/behat-tags\.php$</exclude-pattern>
86+
<exclude-pattern>*/utils/behat-parallel\.php$</exclude-pattern>
8587
</rule>
8688

8789
<!-- This is a procedural stand-alone file that is adding polyfills when

‎src/Context/FeatureContext.php‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,9 @@ private static function get_process_env_variables(): array {
481481

482482
$env = array_merge( $_ENV, $env );
483483

484-
foreach ( [ 'TEMP', 'TMP', 'SystemRoot', 'windir' ] as $key ) {
484+
// Keep the launched processes in the same temporary directory as the tests, which differs per
485+
// parallel worker. WordPress saves downloads under fixed names there, which would clash otherwise.
486+
foreach ( [ 'TMPDIR', 'TEMP', 'TMP', 'SystemRoot', 'windir' ] as $key ) {
485487
$value = getenv( $key );
486488
if ( false !== $value ) {
487489
$env[ $key ] = $value;

0 commit comments

Comments
 (0)