Skip to content

perf(ci): the startup-import tests start 56 interpreters and take 204 s of the suite's 2,423 s #2369

Closed as not planned

Description

@dan-petty

Problem or Motivation

tests/test_cli_startup_imports.py is the largest file of the suite in test time. In a full run of 2026-10-27 on a 16-CPU host (11,285 tests, 287 s of pytest), its 56 tests took 204 s of the 2,423 s that all tests took together (8.4%). It runs no line of src/ that another test does not run, and it is a structural budget on what devops <command> imports, so it stays. Its cost is the problem.

Each test starts a fresh interpreter under -X importtime and runs devops <argv> (_probe_startup_imports, line 443), then parses the importtime text of stderr. There are 56 probes: --version, --help, one <command> --help for every command group, the argvs of the budget tables, and three controls. Three tests of the file are among the 13 slowest tests of the run: test_ai_agents_and_providers_import_no_model_sdks (16.8 s in its call), test_pydantic_ai_exceptions_lazy (11.6 s) and test_startup_structural_budget_fails_when_extra_heavy_package_imported (9.4 s).

The 204 s is the cost of the probes, not a cost that the 16 workers repeat. The same file in one process (pytest tests/test_cli_startup_imports.py -n 0) took 233 s of CPU time (203 s user, 30 s system) for 56 probes, about 4.2 s of CPU for each. That run was on a host with a load average of 26 to 29, so the figure is high. A bare import devops_cli.main took about 1 s of CPU on the same host. The rest of each probe is the cost of -X importtime (a line for each import, written to stderr and parsed in Python), of the work of the command's help, and of the contention.

This issue is a sub-issue of #2356.

Proposed Solution & Architectural Design

Measure first, then change one thing at a time. Each step has its own number in the pull request.

  1. Split the cost. For one ci --help probe, time (a) the interpreter alone, (b) import devops_cli.entry, (c) the help of the group, (d) the same with and without -X importtime, and (e) the parse of stderr. Report CPU time (user plus system), not wall time: a loaded host moved the wall time of the whole suite from 3 m 28 s to 5 m 3 s and 6 m 27 s on 2026-10-27 (perf(test): lower the coverage floor to 85%, remove the slowest tests of least value and find further test speed-ups #2356).
  2. Read the modules a different way. The budget asks which modules a command loads. -X importtime is only needed for the report of the ten slowest imports in a failure message. A probe without it, with sys.modules printed at the end, may cost far less. If it does, run the probe again with -X importtime only when a budget fails, to build the message.
  3. Keep one interpreter for each argv. A module that one argv imports stays in sys.modules, so two argvs cannot share an interpreter. The fixed cost of an interpreter is the floor, and step 1 says how high it is.
  4. Keep the budget exactly as strict. No limit moves. The control that proves the probe fails when a heavy package is imported stays.

Acceptance: the file takes at most half of its present CPU time, measured over three runs of pytest tests/test_cli_startup_imports.py -n 0 on the same host, with the same 56 probes and the same limits.

Target Subsystem

SDLC, Git & PR Governance

Existing Tool Considered

  • python -X importtime is the tool in use. It stays for the failure message.
  • pytest-xdist is in use and runs the probes on all workers.
  • pytest-benchmark and pyinstrument time code inside a process. The cost here is in the start of processes, so the timings of step 1 use time and -X importtime, and nothing new is built.

Source: #2356

Proposed design

Problem statement

The startup-import test file takes 204 s of the suite's 2,423 s. Each of its 56 probes starts a fresh CPython interpreter. The proposed fix of dropping -X importtime is shown false by paired measurement: the flag adds no measurable cost. The real per-probe cost is the import of the main module and the work of the command help. Issue #2164 plans to move this budget into a devops ci row, which conflicts with changes to the pytest file.

Acceptance criteria

Requirement: Reduced per-probe CPU time

The file SHALL take at most half its present CPU time over three runs of pytest tests/test_cli_startup_imports.py -n 0 on the same host.

Scenario: Three consecutive runs on the same host

  • WHEN The test file is run three times with -n 0 on the same host after the change
  • THEN The total CPU time (user plus system) across the three runs is at most half the baseline measured the same way before the change

Requirement: All probes pass

All 56 probes SHALL execute and pass with no failures or errors.

Scenario: Full test file run

  • WHEN The test file is run with pytest tests/test_cli_startup_imports.py -n 0
  • THEN The run reports 56 passed tests with no failures or errors

Requirement: Budget limits unchanged

All four budget limit constants SHALL retain their integer values.

Scenario: Constant values after the change

  • WHEN The constants DEFAULT_STARTUP_IMPORT_MODULE_LIMIT_MAIN_AND_VERSION, DEFAULT_STARTUP_IMPORT_DEVOPS_MODULE_LIMIT_MAIN_AND_VERSION, DEFAULT_STARTUP_IMPORT_MODULE_LIMIT_COMMAND_HELP, and DEFAULT_STARTUP_IMPORT_DEVOPS_MODULE_LIMIT_COMMAND_HELP are read after the change
  • THEN Each constant has the same integer value as before the change

Requirement: Control test passes

The control test SHALL prove the budget fails when a heavy package is imported.

Scenario: Control test execution

  • WHEN test_startup_structural_budget_fails_when_extra_heavy_package_imported is run
  • THEN The test passes, confirming the budget correctly detects the extra heavy import

Requirement: Failure message intact

A failing budget SHALL report the ten slowest imports in its assertion message.

Scenario: Budget breach message

  • WHEN A budget limit is breached during a probe
  • THEN The assertion message includes the ten slowest imports by cumulative seconds

Existing Tool Considered

No alternatives check has run. No open, maintained tool or library covers the need of reducing the fixed cost of CPython interpreter startup and module import. The cost is inherent to the runtime. The body lists python -X importtime, pytest-xdist, pytest-benchmark, and pyinstrument, but paired measurement shows none is the cost driver. The real cost is the import of the main module and the work of the command help, which no external tool can reduce.

Key questions

Open questions

  • Is the plan folder for this issue still present? (missing verified source)

Fit

Needs splitting into 1 items:

  1. Re-scope the startup-import budget after ci(gate): checks of committed files and tool configuration run as devops ci rows, not as pytest tests that start the tool #2164 moves it into a devops ci row, targeting the real per-probe cost of the main module import and the help work

Suspected block

Issue #2164 (move startup-import budget into a devops ci row) must be delivered first. Work on the pytest file would conflict with it.

Dependencies

#2164

Alternatives check of 2026-11-03: not-applicable.
Class: perf, no search.
Tool: none found.

Activity

  1. added
    source/agent
    type/infra
    on Oct 27, 2026
  2. dan-petty commented on Oct 27, 2026

    @dan-petty
    OwnerAuthor

    Intake placed this item to the backlog: intake puts every new item in the backlog. A person plans the milestones on the board.

    • Type: type/infra. The title uses the perf prefix, which maps to infrastructure.
    • Priority: P2-Medium.
    • Value: Medium. Faster startup-import tests reduce CI cost for all contributors.
    • Effort: Medium.
  3. dan-petty commented on Oct 28, 2026

    @dan-petty
    OwnerAuthor

    Status set to Ready at bb46660 (see proposed design in issue body).

  4. added
    priority/p2-medium
    status/ready
    value/medium
    effort/medium
    scope/ci
    on Oct 28, 2026
  5. devops-cli-service commented on Nov 1, 2026

    @devops-cli-service
    Contributor

    Staged: branch perf/2369-the-startup-import-tests-start-56, draft pull request #2985. The pull request plans the item and closes no issue.

    The task groups, with the tokens of the inputs of each step, for a budget of 40000 tokens:

    1. Replace -X importtime with sys.modules dump in the probe: 3098 tokens
    2. Add timed re-probe for the failure diagnostic message: 3102 tokens
  6. 2 remaining items

  7. added
    status/planned
    on Nov 1, 2026
  8. added
    owner/agent
    on Nov 1, 2026
  9. dan-petty commented on Nov 1, 2026

    @dan-petty
    OwnerAuthor

    The premise of #2369 is false. Dropping -X importtime from the startup-import probe does not change the CPU time. The plan cannot meet the acceptance criterion (half of the CPU time).

    Measure: paired, alternating runs of the exact _STARTUP_PROBE, CPU seconds per probe (user plus system), without the flag / with -X importtime, host load average about 26.

    • --version: 1.222 / 1.205
    • --help: 1.125 / 1.160
    • ci --help: 1.479 / 1.510 (a second run today: 1.025 / 1.047)
    • ai agents --help: 3.359 / 3.304

    The ratio is 0.97 to 1.02, which is noise. A bare interpreter costs 0.03 s. import devops_cli.entry costs 0.25 to 0.36 s. The rest is the work of main and the command help, which the budget must run. The issue said that a probe without the flag "may cost far less". Step 1 of the issue measures it, and it does not.

    Open issue #2164 plans to move the startup-import budget into a devops ci row. Work on the pytest file would conflict with it. Issue #2371 (per-worker start cost) is open and does not replace this item.

    The plan folder is removed in PR #3029. A new item can re-scope this one after #2164, to cut the real per-probe cost (the import of the main module and the work of the help).

  10. added
    status/new
    and removed
    status/planned
    on Nov 1, 2026
  11. added 2 commits that reference this issue on Nov 1, 2026
  12. devops-cli-service commented on Nov 1, 2026

    @devops-cli-service
    Contributor

    Refine cannot set this item to Ready at 2ae3772 until a person answers the questions below. Reply in a comment or edit the body, and the next refine round looks at the item again.

    • Is the plan folder for this issue still present? (missing verified source)

    The next refine round also looks at the item again when a commit on the default branch changes one of these files: tests/commands/test_cli_startup_imports.py.

  13. removed
    owner/agent
    on Nov 1, 2026
  14. devops-cli-service commented on Nov 3, 2026

    @devops-cli-service
    Contributor

    Alternatives check: not-applicable

    Class: perf. The check makes no search and no model call for an item of this class.

    Tool: none found

    Checked on 2026-11-03. A person decides what this means for the item. The check changes no label and closes no issue.

  15. dan-petty commented on Nov 4, 2026

    @dan-petty
    OwnerAuthor

    The premise is false, so this item closes as not planned. Step 2 of the proposal expected a large gain from a probe with no -X importtime. Paired, alternating runs gave a ratio of 0.97 to 1.02, which is noise. The comment of dan-petty of 2026-11-01T12:52Z holds the figures. PR #3029 removed the plan of PR #2985, and openspec/changes has no folder for this item.

    #2164 moves the startup-import budget into a devops ci row, so work on the pytest file conflicts with it.

    What moved, in the section Scope from #2369 of #2164: the paired measurements, the cost split, the rule that the four limit values stay, the rule that the control test stays, the rule that a failure message lists the ten slowest imports, the corrected path tests/commands/test_cli_startup_imports.py, and a new criterion on the CPU time of the row. That criterion now owns the goal to cut the cost per probe.

    #2163 and #3050 still name this item as speed work. Read #2164 in its place. A comment on each of them says so. #2371 stays open, and this close does not replace it. This item has no change folder, no open pull request and no open parent issue.

  16. removed
    status/new
    on Nov 4, 2026
  17. added 2 commits that reference this issue on Nov 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    effort/mediumpriority/p2-mediumscope/cisource/agenttype/infravalue/medium

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions