Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Speeding up tests with machine learning and dis...

Speeding up tests with machine learning and distributed execution

Test time is one of the key drivers of build times. There are many contributing factors, including the growing number of integration and functional tests to cover a wide range of inputs, running tests sequentially, and dependencies on expensive external services. This often leads to developers only running tests on CI thereby considerably lengthening the feedback loop. And even then, running all tests for every change can be a challenge both in terms of cost and build time. Gradle Enterprise provides two groundbreaking new technologies that allow to run tests earlier and more frequently: Predictive Test Selection and Test Distribution.

Predictive Test Selection saves testing time by identifying, prioritizing, and running only tests that are likely to provide useful feedback. This is accomplished this by applying a machine learning model that uniquely incorporates fine-grained code snapshots, comprehensive test analytics, and flaky test data.

Test Distribution expands test parallelism by fanning out test execution to remote agents takes your existing test suites and distributes them – based on historical execution times – across remote agents to execute them faster.

Marc Philipp

December 06, 2022
Tweet

More Decks by Marc Philipp

Other Decks in Technology

Transcript

  1. Reduced test time yields increase in productivity More build and

    test executions Shift-left testing from CI to local builds Less context-switching
  2. CI Fanout Idea: run subset of tests on CI agents

    in parallel ⬢ Grouping of tests often manual ⬢ Large overhead because each CI agent has to run build up to test task ⬢ Test results are scattered over multiple CI jobs ⬢ Does not support local builds
  3. Test Distribution Overview ⬢ Broker component included in Gradle Enterprise

    server ⬢ Agents connect to the broker ⬢ Builds connect to the broker and request agents ⬢ Works for local and CI builds On-premises inside your network
  4. Running Test Distribution agents ⬢ The agent comes in two

    flavors: Jar and Docker image ⬢ Runs on Java 11+, requires about 128 MB of memory ⬢ Runs on Windows, macOS, and Linux ⬢ Detects its environment (JDKs, OS) during startup ⬢ Administrators can pass additional capabilities as command line parameters java -jar gradle-enterprise-test-distribution-agent.jar \ --server https://ge.example.com \ --api-key «api-key» \ --capabilities docker,postgres=14 docker run \ --env TEST_DISTRIBUTION_AGENT_SERVER=https://ge.example.com \ --env TEST_DISTRIBUTION_AGENT_API_KEY=«api-key» \ --env TEST_DISTRIBUTION_AGENT_CAPABILITIES=postgres=14 \ gradle/gradle-enterprise-test-distribution-agent Runs on-premises inside your own network infrastructure
  5. Gradle: Integrates with default Test task tasks.test { useJUnitPlatform() distribution

    { enabled.set(true) } } Code coverage and other output files are transferred back and merged automatically Input files (e.g. classpath) are automatically transferred to remote agents plugins { id("com.gradle.enterprise") version "3.11.4" }
  6. Maven: integrates with Surefire/Failsafe plugins <project xmlns="http://maven.apache.org/POM/4.0.0"> <!-- ... -->

    <build> <plugins> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.2</version> <configuration> <properties> <distribution> <enabled>true</enabled> </distribution> </properties> </configuration> </plugin> </plugins> </build> </project> ⬢ Requires Gradle Enterprise Maven extension ⬢ Integrates with Surefire and Failsafe plugins
  7. Test Distribution requires JUnit Platform ⬢ Most test frameworks with

    JUnit Platform test engines are supported: ◦ JUnit 5 (Jupiter) ✔ ◦ JUnit 3/4 and Spock 1.x (via junit-vintage-engine included in JUnit 5) ✔ ◦ Spock 2.x ✔ ◦ TestNG (via testng-engine) ✔ ◦ ScalaTest (via scalatest-junit-runner) ✔ ◦ ArchUnit ✔ ◦ jqwik ✔ ⬢ Currently unsupported (we have plans to add support where possible): ◦ Kotest ◦ Spek ◦ Cucumber
  8. Checking compatibility ⬢ Compatibility can be checked before adopting Test

    Distribution by applying a custom build script that adds custom values to the Build Scan https://github.com/gradle/gradle-enterprise-build-config-samples/pull/469
  9. Resilience against temporary network failures ⬢ Actively manage connections using

    WebSocket pings ⬢ Reconnect if connection is lost or unresponsive ⬢ Reschedule work on other agents if agent disappears ⬢ Retry file uploads on non-client errors ⬢ Avoid builds from breaking and causing disruption
  10. Adaptive scheduling ⬢ Be able to react to additional agents

    becoming available during test execution ⬢ Increases agent utilization ⬢ Reduces testing time
  11. Auto-scaling Test Distribution agents ⬢ Agent pools with min/max size

    and capabilities for horizontal scaling ⬢ HTTP endpoint provides metrics indicating the target number of agents for each pool, based on demand. ⬢ Step-by-step instructions for Kubernetes in docs ⬢ Real-time and historical usage can be visualized by Gradle Enterprise administrators { "id": "sosmbpbr", "name": "Linux", "capabilities": [ "jdk=8", "os=linux" ], "minimumAgents": 1, "maximumAgents": 90, "connectedAgents": 2, "idleAgents": 0, "desiredAgents": 8 }
  12. PR Build Times with Integration Tests went from 60 minutes

    to 5 Danny Thomas Developer Productivity Team For one project we reduced the build time from 62 minutes to under 5 minutes just using Test Distribution across multiple machines. So we think that Test Distribution will really move the needle and improve the test experience for everyone.
  13. Local Build Times: From 54 min to 5. CI PR

    Builds: Way More Reliable Cédric Champeau Technical Staff, Oracle (former Gradle Build Tool engineer) My test suites finished in 5 minutes instead of 54. The 10X developer is finally here!
  14. Avoid wasting time and resources ⬢ Typically less than 1%

    of codebase affected by any given change ⬢ Typically fewer than 1% of tests affected by any given change ⬢ Yet, most reported test failures are not regressions 💡 Skip irrelevant tests for a given change set using machine learning
  15. Not a new concept ⬢ Predictive Test Selection — Meta

    2019 ⬢ Improving Test Effectiveness Using Test Executions History: An Industrial Experience Report — Ericsson 2019 ⬢ Taming Google-Scale Continuous Testing — Google 2017 ⬢ Test Re-prioritization in Continuous Testing Environments — Concordia Univ. 2016 ⬢ The Art of Testing Less without Sacrificing Quality — Microsoft 2015 ⬢ Improving the effectiveness of test suite through mining historical data — ACM 2014 ⬢ … and more
  16. 1. When a test run starts, Gradle Enterprise submits a

    test input snapshot and test set to a machine learning model. 2. Gradle Enterprise automatically develops a test selection strategy by learning from historical code changes and test outcomes from your Build Scan™ data to predict a subset of relevant tests, which are then executed by your build. 3. Code change and test results data are processed immediately after a Build Scan is uploaded to Gradle Enterprise and updates the test selection strategy based on new results. How it works…
  17. Predictive Test Selection Simulator ⬢ Simulates what tests would have

    been selected had PTS been enabled ⬢ Compares full test results to selected test results ⬢ Allows to assess risk and savings of PTS per task/goal before adopting it ⬢ Requires at least 50 executions over a 14-day period before starting to make predictions
  18. <project xmlns="http://maven.apache.org/POM/4.0.0"> <!-- ... --> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.2</version> <configuration> <properties>

    <predictiveSelection> <enabled>true</enabled> </predictiveSelection> </properties> </configuration> </plugin> <!-- ... --> </project> Enabling Predictive Test Selection tasks.test { predictiveSelection { enabled.set(true) } } plugins { id("com.gradle.enterprise") version "3.11.4" }
  19. Must-run tests import com.gradle.enterprise.testing.annotations.MustRun; @MustRun public class ImportantTests { //

    ... } Using gradle-enterprise-testing-annotations from Maven Central. tasks.test { predictiveSelection { mustRun { includeClasses.add("example.ImportantTests") } } } <predictiveSelection> <mustRun> <includeClasses> <include>example.ImportantTests</include> </includeClasses> </mustRun> </predictiveSelection>
  20. Usage patterns Our recommendation is to run all tests post-merge

    or at least periodically. 1. Apply to existing pre-merge verification (run all tests post-merge) 2. Apply to existing pre- and post-merge verification (move “all tests” run to nightly build) 3. Run some high value tests pre-merge (change CI jobs to run costly tests sooner) 4. Two-pass pre-merge (split pre-merge CI jobs into two steps to get faster feedback)