[Flutter] Improve test execution speed with concurrency option

2024-01-28 hit count image

Let's see how to improve test execution speed with concurrency option when running test code in Flutter.

Outline

When developing an app in Flutter, you write test code and run it to improve the stability of the service. However, as the size of the project grows, the amount of test code also increases. So, the time it takes to run test code also increases.

In this blog post, I will introduce how to run test code in parallel using the concurrency option to improve the test execution speed.

concurency option

When running a test in Flutter, you can run the test code in parallel using the concurrency option. This reduces the time it takes to run the test code.

When you run the following command, you can check the options available in the Flutter test command.

flutter test -h

Among the various options, you can check the concurrency option as follows.

-j, --concurrency=<jobs>
The number of concurrent test processes to run. This will be ignored when running integration tests.

You can also check the concurrency option in the official documentation.

Check the number of cores

By default, Flutter test command is set to use half of the host CPU cores.

Test suites run concurrently by default, using half of the host’s CPU cores.

Therefore, if you set the cores less than half of the host machine’s cores to the concurrency option, the performance may be worse than when you run it without setting the concurrency option.

So, you need to check the number of cores of the current machine and set the appropriate number of cores. You can check the number of cores of the machine by running the following command.

nproc --all

Compare test execution speed

Let’s see how much the test code execution speed improves by using the concurrency option. Run the following command in your Flutter project to run the test code.

flutter test

After running the test code, you can check the time it took to run the test code as follows.

02:10 +657: All tests passed!

Without the concurrency option, it takes 2 minutes and 10 seconds to run the test code. Now, let’s run the test code using the concurrency option as follows..

flutter test --concurrency=$(nproc --all)

After running the test code, you can check the time it took to run the test code as follows.

02:02 +657: All tests passed!

You can see that the time it takes to run the test code is reduced by about 8 seconds when using the concurrency option.

Compare test execution speed in GitHub Actions

In my case, the performance of the local machine (12 cores) is so good that I can’t see much effect, but you can see a bigger effect in the CI/CD environment. When running the test code in GitHub Actions without using the concurrency option, you can see that it takes about 7 minutes as follows.

Flutter - test without concurrency option

If you run the test code using the concurrency option in GitHub Actions, you can see that it takes about 4 minutes as follows.

Flutter - test with concurrency option

Completed

Done! we’ve seen how to improve the test execution speed in Flutter by using the concurrency option in the test. If you’re not using the concurrency option yet, try adding the concurrency option to improve the test execution speed.

Was my blog helpful? Please leave a comment at the bottom. it will be a great help to me!

App promotion

You can use the applications that are created by this blog writer Deku.
Deku created the applications with Flutter.

If you have interested, please try to download them for free.

Posts