Skip to content

Measure CLI tools startup time in CI - #112496

Closed
thevar1able wants to merge 4 commits into
masterfrom
faster-cli-startup
Closed

thevar1able wants to merge 4 commits into
masterfrom
faster-cli-startup

Conversation

@thevar1able

Copy link
Copy Markdown
Member
  • Add a CLI startup benchmark job that spawns clickhouse local and clickhouse client and compares CPU time and minor page faults against the latest master build.
  • Gate on minor page faults at 1.05x and on CPU time at 1.15x, since faults count work done rather than time taken and so barely move on a shared runner.
  • Attach the system and information_schema tables in clickhouse local on first access rather than at startup, keeping system.one eager because every FROM-less query resolves it during analysis.
  • Stop resolving the UTC time zone in TimezoneMixin, so that naming a DateTime type no longer builds a lookup table nothing reads; the serializations that do need it hold their own.
  • Resolve the client hostname in ClientInfo on first use instead of through a blocking getaddrinfo on every invocation.
  • Set a system table's comment before attaching it, instead of copying its whole StorageInMemoryMetadata twice.

Changelog category (leave one):

  • Performance Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Speed up clickhouse local startup: system tables and information_schema are now attached on first access, naming a DateTime type no longer builds an unused UTC lookup table, and the client hostname is resolved only when it is actually reported instead of on every invocation.

thevar1able and others added 2 commits July 30, 2026 00:32
`clickhouse local --query "SELECT 1"` spent most of its time before the query:
the query itself took 1ms of a 45ms run. Three sources of unnecessary startup
work, measured with `perf` and with an interposed `__libc_start_main` to
separate static initialisation from `main`:

* `TimezoneMixin` resolved the UTC time zone in its constructor, in addition to
  the named or default one. Building a `DateLUTImpl` walks ~146k days through
  cctz (~70M instructions), and `DataTypeDateTime` / `DataTypeDateTime64` are
  constructed just to name a column type - for example when building the
  schemas of the system tables - without ever touching UTC. Only the
  serializations need it, for ISO output and the `best_effort` parsers, so they
  declare their own `utc_time_zone` and pay for it once per formatter.

* `clickhouse local` attached all ~164 system tables and both spellings of
  `information_schema` up front. The latter alone parses 20 embedded
  `CREATE VIEW` statements and builds 40 view storages. Now
  `DatabaseWithOwnTablesBase::setDeferredPopulation` fills the table list on
  first access instead. `system.one` is the exception and stays eager: every
  `FROM`-less query resolves it during analysis, so deferring it along with the
  rest would just move the whole cost into the first query. Accordingly the
  table lookups check the already attached tables before triggering population.

* `ClientInfo::fillOSUserHostNameAndVersionInfo` resolved the local FQDN through
  `getFQDNOrHostName`, a blocking `getaddrinfo` (also dlopening the NSS
  modules). `getClientHostName` resolves it on demand instead, so a process that
  never reports the name does not pay for it, and a slow resolver no longer sits
  on the startup path.

Also stop copying every system table's `StorageInMemoryMetadata` - which carries
the full `ColumnsDescription` - twice just to fill in its comment.

`clickhouse local --query "SELECT 1"`, mean of 500 sequential runs:
44.65ms -> 27.80ms, and 513.8M -> 272.3M instructions. `clickhouse client` and
`clickhouse local --version` improve by about 1.5ms. The server keeps attaching
its system tables eagerly.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
The performance-comparison job cannot see start-up cost. It runs its queries over
a persistent client connection against servers that are already up, so process
start-up falls entirely outside its measurement window, and `clickhouse local` is
never exercised at all. The only existing coverage is the canary
`03321_clickhouse_local_initialization_not_too_slow_even_under_sanitizers`, which
asserts that at least 5 invocations complete in 10 seconds - about 2s per run, far
too coarse to notice anything short of a catastrophe.

This job measures whole short-lived invocations of both `local` and `client` and
compares them against the latest master build, the same way the performance
comparison picks up its reference binary. Four scenarios need nothing but the
binary (`local --version`, `client --version`, `local -q "SELECT 1"`, and a query
that forces the whole `system` database to be built, so that the laziness there
cannot be silently undone). A fifth runs a real `client` query against a server
started from the build under test, which is the only way to cover connecting and
the handshake - where a client pays for `ClientInfo`, including resolving its own
hostname. That server is held fixed while the two client binaries are swapped, so
its cost cancels in the ratio.

Metrics come from `getrusage(RUSAGE_CHILDREN)`, which needs neither `perf` (not
installed in any CI image) nor elevated `perf_event_paranoid`. Both of these gate
the result, because they fail in opposite directions:

* Minor page faults (`ru_minflt`) are the trustworthy metric: they count work
  done rather than time taken, so a busy neighbour cannot move them. Measured
  against itself the ratio stays within 0.4%, so the threshold is a tight 1.05x -
  and this is the gate that catches the usual shape of a start-up regression
  (more static initialisation, more objects built, more relocations), all of which
  touch more memory.
* CPU time (`ru_utime + ru_stime`) is the noisy one. It beats wall clock, since it
  excludes time spent waiting to be scheduled, but on a shared runner cache and
  memory-bandwidth contention still make the same work burn more cycles. So it
  gets a loose 1.15x threshold and exists only to catch what faults cannot: a
  regression that burns CPU without touching new memory.

Wall clock is reported for context and never gates.

Each scenario spawns the binary 330 times per build (11 rounds of 30), alternating
between the two builds and swapping which goes first, then takes a median of the
per-round ratios so that a round which lost its CPU to a neighbour is discarded
rather than averaged in.

`ci/jobs/scripts/cli_startup/measure.py` also runs standalone for local work:

    python3 ci/jobs/scripts/cli_startup/measure.py --left old/clickhouse --right new/clickhouse

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Jul 29, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [fcce434]

Summary: ✅


AI Review

Summary

This PR speeds up short-lived CLI invocations by deferring most system/information_schema attachment in clickhouse local, avoiding eager UTC DateLUT construction, resolving the client hostname on demand, and adding a CI startup benchmark. The runtime changes look coherent and CI is green, but the new benchmark job still does not reschedule when some of its own shared harness and environment inputs change, so the CI protection it adds remains incomplete.

Findings

⚠️ Majors

  • [ci/defs/job_configs.py:1458-1473] cli_startup_jobs.digest_config now tracks compiled-binary inputs, but the job still executes ClickHouseService for the client_select_1 scenario (ci/jobs/cli_startup_benchmark.py:131-135), and that shared start path depends on ci/jobs/scripts/clickhouse_service.py, ci/jobs/scripts/server_cleanup.py, and the clickhouse/performance-comparison image. None of those are in include_paths, so a PR that changes only the benchmark harness or container can still get a cache hit and skip the benchmark entirely.
Final Verdict

Changes requested: the new benchmark should reschedule when its own shared harness and container change, not only when the binary inputs change.

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Jul 29, 2026
Comment thread ci/defs/defs.py Resolved
Comment thread ci/defs/job_configs.py Outdated
runs_on=["#from param"],
command="python3 ./ci/jobs/cli_startup_benchmark.py",
digest_config=Job.CacheDigestConfig(
include_paths=[

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The digest only tracks the benchmark harness itself, but the client_select_1 scenario also depends on the shared server-start path in ci/jobs/scripts/clickhouse_service.py and ci/jobs/scripts/server_cleanup.py. On a CI-only PR that changes either helper, build artifacts stay unaffected and this job is treated as unaffected too, so the startup benchmark never reruns even though one of its measured code paths changed. ClickBench already includes those shared helpers in its digest for this reason; this job should do the same.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

build_digest_config.include_paths fixes reruns on binary changes, but the benchmark still calls ClickHouseService for client_select_1 (ci/jobs/cli_startup_benchmark.py), and that shared start path depends on ci/jobs/scripts/clickhouse_service.py plus ci/jobs/scripts/server_cleanup.py (ci/jobs/scripts/clickhouse_service.py). Neither of those, nor ./ci/docker/performance-comparison, is in cli_startup_jobs.digest_config at ci/defs/job_configs.py, so a PR that changes only the shared start path or the benchmark container is still served from cache and skips this job.

thevar1able and others added 2 commits July 30, 2026 13:06
CI release artifacts arrive without the executable bit, so spawning the build under
test failed with `PermissionError: [Errno 13] Permission denied` on both
architectures. The reference build was already chmod-ed after download; do the same
for the artifact, as `performance_tests.py` and `ClickHouseService` both do.

While here, self-extract both binaries up front. They are self-extracting archives
whose first run replaces the wrapper in place with the real ELF, and doing that
explicitly keeps decompression out of every measured round rather than relying on the
warmup runs to absorb it.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…anges

Two ways the new job would have been silently omitted.

`should_skip_job` keeps only jobs whose name contains `performance`, plus an explicit
allowlist, so `ci-performance` - the label used to request perf-only coverage - filtered
the benchmark out. Match it by prefix rather than adding it to the allowlist tuple: the
name carries a build-type parameter, so the tuple would have needed both
`CLI startup benchmark (amd_release)` and `(arm_release)` spelled out, as the `Build`
entries already are.

The job digest covered only the job's own script, and `requires` is dropped from the
digest (see `drop_fields` in `praktika/digest.py`), so depending on the build artifact
does not make the digest move. Once the job had succeeded once, every later pull request
that did not touch the script itself would have been served from cache - skipping the
measurement on exactly the changes worth measuring. Include the build inputs, so the
digest tracks whatever can change the binary; a docs-only pull request still gets a cache
hit and pays nothing.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.50% 86.50% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 78.70% 78.60% -0.10%

Changed lines: Changed C/C++ lines covered: 168/172 (97.67%) · Uncovered code

Full report · Diff report

@rschu1ze

Copy link
Copy Markdown
Member

I like the different improvements. Maybe we can send them as separate small PRs?

@thevar1able

Copy link
Copy Markdown
Member Author

@rschu1ze yes, I'll split it when I get back to this PR.

@thevar1able

Copy link
Copy Markdown
Member Author

Split to separate PRs

#115488
#115489
#115490
#115491

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-performance Pull request with some performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants