Speed up the API Tests workflow without dropping any test

Open
#9,967 2 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Refactor
Clarity
Mostly clear
Activity status
Active
Tech stack
csharp, github-actions
Domain
ci-cd, testing

Research direction

Start with .github/workflows/api-tests.yml and identify the API test command and its current checks. Measure individual QuantConnect.Tests.API.* test times in the workflow's quantconnect/lean:foundation image, then rank the slow tests and propose the stages on this issue while beginning the first stage. Done means all existing tests and checks still pass, with repeated local runs and before/after timings documented for each pull request.

Written by the indexing model from the issue text.

Description

Expected Behavior

The API Tests workflow (.github/workflows/api-tests.yml) takes noticeably less time, while running the same tests and checks it runs today.

Actual Behavior

It takes about 16.6 minutes on average, 18.6 at most, over its last 7 successful runs. It builds the solution, then runs the API tests (QuantConnect.Tests.API.*), which call the API.

Potential Solution

Scope:

  • In stages, one pull request each. Start by measuring how long each test takes and posting a short plan on this issue: the worst offenders with their times, and the stages, worst offenders first, ordered by time saved for the effort, each a pull request that stands on its own and can be reviewed in one sitting: about 1,000 changed lines and fewer than 40 files at most. Then do the first stage in the same job, without waiting on the plan. Each pull request does one stage, says Part of #9967 rather than closing the issue, and ends with what's left for the next stage. The last stage's pull request closes the issue. When all of it fits within one pull request's limits, one pull request is fine, and it closes the issue.
  • Keep every test and check it runs today. No skipped, removed, filtered-out or weakened tests or assertions, and no lower coverage. The same tests have to pass, only faster.
  • Make the tests themselves faster. Replace fixed sleeps with waiting on the condition they wait for, and share or cut repeated expensive setup.
  • Use parallelism where tests are independent.
  • Clean up after every test. Tests often leave tasks, threads, timers or engine instances running after they end, which slows down every test after them: stop and dispose of what each test starts.
  • Mock where it helps, such as a slow external call or a heavy dependency a test doesn't check, as long as the test still checks what it set out to.
  • Keep them consistent. Faster must not mean flaky: no race conditions, no state shared between tests running at once, no crashes or hangs. Run the changed suite several times in a row, in parallel where it runs in parallel, and put the number of runs and their results in the pull request. A test that already fails now and then gets fixed when the cause is in the test itself, such as a race in its setup, a fixed sleep or state shared with another test, not retried or skipped. When the cause is in the code under test, describe it in the pull request instead. CI runs for many pull requests also happen at the same time, against the same API account and the same runners, so a test can't rely on state another run could change: each run creates its own, as the API tests do with a new project every time. Sharing a project between tests or runs to save time would break that, so it's not a speed-up to take.
  • Measure locally first, in the workflow's own quantconnect/lean:foundation image with its commands, and put the before and after times in the pull request. Push once the change is measured, not to try ideas on CI.
  • Use only this workflow's runners. On the pull request, cancel the runs of the other workflows, which this change doesn't affect, so the shared self-hosted runners stay free.
  • Workflow file changes: if the speed-up needs a change to .github/workflows/api-tests.yml itself, describe it in the pull request description.

Calls to the API often wait on server-side work, such as a compile or a backtest finishing: poll the state they wait for, at a short interval, rather than sleeping a fixed time, and run independent fixtures at once.

Checklist
  • I have completely filled out this template
  • I have confirmed that this issue exists on the current master branch
  • I have confirmed that this is not a duplicate issue by searching issues
Dominant language
C#
Stars
22.6k
Forks
5.4k
Avg merge
11h 39m
Merged PRs (30d)
104

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from QuantConnect/Lean

All issues in QuantConnect/Lean

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.