Skip to content

Support shell-script queries in performance comparison tests - #108081

Merged
alexey-milovidov merged 7 commits into
masterfrom
perf-shell-script-tests
Jun 25, 2026
Merged

alexey-milovidov merged 7 commits into
masterfrom
perf-shell-script-tests

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Jun 21, 2026 •

Copy link
Copy Markdown
Member

The performance comparison framework could only run SQL queries over the native protocol. This extends it to also accept shell scripts, marked with <query type="shell">, which are run with bash and timed end to end. This covers measurements the native protocol cannot express: HTTP latency, response compression, and tool startup time.

A shell-script query goes through the same measurement machinery as a SQL query — prewarm, the interleaved multi-run loop (alternating L,R,R,L,…), the same stop conditions, and the same median / Welch's t-test / diff statistics, reported as the client_time metric. The only difference is how a single timing is obtained (a bash subprocess wall-clock measurement instead of connection.last_query.elapsed), and that the server-side profiler runs are skipped (there is no single query to attribute them to).

Each server gets its own $CLICKHOUSE_* environment variables, mirroring the stateless tests in tests/queries/shell_config.sh, so the reference (LEFT) and the patched (RIGHT) build — which listen on different ports — are measured each on their own: CLICKHOUSE_BINARY, CLICKHOUSE_HOST, CLICKHOUSE_PORT_TCP, CLICKHOUSE_PORT_HTTP, CLICKHOUSE_DATABASE, CLICKHOUSE_CLIENT, CLICKHOUSE_LOCAL, CLICKHOUSE_CURL, CLICKHOUSE_URL. Parameter {substitutions} and <settings> are not applied to shell scripts (they would collide with ${var} and brace expansion).

Both run paths pass the binary paths and HTTP ports to perf.py (new --binary / --http-port options): ci/jobs/performance_tests.py for CI and ci/jobs/scripts/perf/compare.sh for local runs. The perf-comparison config removes <http_port>, so it is re-enabled per server on the command line (a documented config override) with distinct ports for the reference and the patched server.

This also fixes a latent bug in analyze_queries: version and query_id were taken from the non-preserved (left) side of the RIGHT JOIN against query_log. For runs without a query_log row (shell scripts) they defaulted to 0 / empty and collapsed both servers' runs onto version 0. They are now qualified to query_runs. SQL queries are unaffected, because their query_log row always matches.

Two shell-script performance tests are added:

  • clickhouse_local_startup.xml — how fast clickhouse-local --query "SELECT 1" starts up.
  • http_read_gzip.xml — reading about a megabyte over HTTP (SELECT number FROM numbers(500000)), with and without gzip response compression.

Changelog category (leave one):

  • Build/Testing/Packaging Improvement

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

The performance comparison test framework now accepts shell-script tests (<query type="shell">) in addition to SQL queries, for end-to-end measurements such as HTTP latency, response compression, and tool startup time.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.7.1.83

The performance comparison framework could only run SQL queries over the
native protocol. Extend it to also accept shell scripts, marked with
`<query type="shell">`, which are run with `bash` and timed end to end. This
covers measurements the native protocol cannot express: HTTP latency,
response compression, and tool startup time.

`perf.py` runs each shell-script query once per server, building per-server
`$CLICKHOUSE_*` environment variables (`CLICKHOUSE_BINARY`, host, TCP/HTTP
ports, `CLICKHOUSE_CLIENT`, `CLICKHOUSE_LOCAL`, `CLICKHOUSE_CURL`,
`CLICKHOUSE_URL`) that mirror the stateless tests in `shell_config.sh`, so the
reference and the patched build are measured each on their own. The timing
flows into the report as the `client_time` metric, exactly like a SQL query.
Parameter substitutions and `<settings>` are not applied to shell scripts,
and the server-side profiler runs are skipped for them.

Both run paths pass the binary paths and HTTP ports to `perf.py`:
`ci/jobs/performance_tests.py` (`CHServer`) for CI and
`ci/jobs/scripts/perf/compare.sh` for local runs. The perf-comparison config
removes `<http_port>`, so it is re-enabled per server on the command line (a
documented config override) with distinct ports for the reference and the
patched server.

Fix a latent bug in `analyze_queries`: `version` and `query_id` were taken
from the non-preserved (left) side of the `RIGHT JOIN` against `query_log`.
For runs without a `query_log` row (shell scripts) they defaulted to `0`/`''`
and collapsed both servers' runs onto version `0`. Qualify them to
`query_runs`. SQL queries are unaffected because their `query_log` row always
matches.

Add two shell-script performance tests:
- `clickhouse_local_startup.xml`: `clickhouse-local --query "SELECT 1"` startup
- `http_read_gzip.xml`: reading ~1 MB over HTTP, with and without gzip

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

clickhouse-gh Bot commented Jun 21, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [cef3b90]

Summary: ✅


AI Review

Summary

This PR extends the performance comparison framework so tests can define shell-script queries, wires per-server binary/HTTP settings through both perf runners, adds two shell benchmarks, and documents the new behavior. The previously reported timeout, HTTP fail-closed, auth/TLS, and profile-collection issues are fixed in the current code; I resolved the two remaining open bot-authored threads. I found no new correctness, safety, or reporting issues that warrant inline comments.

Missing context / blind spots
  • ⚠️ The latest S3 report for cef3b90e044d3379c243b0f4182208b43556f49b still showed the performance comparison shards as pending when checked; completed perf shard output would close the end-to-end validation gap.
  • ⚠️ Local execution of perf.py was blocked by missing clickhouse_driver, and .claude/tools/fetch_perf_report.py could not run because no clickhouse binary exists in PATH. I validated Python syntax locally, but not a live perf run in this checkout.
Final Verdict

✅ No blockers or majors found in this pass.

@clickhouse-gh clickhouse-gh Bot added the pr-build Pull request with build/testing/packaging improvement label Jun 21, 2026
Comment thread tests/performance/scripts/perf.py Resolved
Comment thread tests/performance/scripts/perf.py Resolved
@alexey-milovidov alexey-milovidov added pr-performance Pull request with some performance improvements ci-performance performance only labels Jun 22, 2026
Address the AI review on #108081 (#108081):

1. `run_shell_query` now runs the script in its own session
   (`start_new_session=True`) and, on timeout, kills the whole process group
   with `os.killpg` instead of only the immediate `bash`. Previously
   `subprocess.run(..., timeout=...)` killed just `bash`, so a script blocked
   inside a child such as `curl` or `$CLICKHOUSE_LOCAL` kept running after
   `--max-query-seconds`, consuming CPU/network or holding a server-side query
   open and polluting later measurements.

2. The default `CLICKHOUSE_CURL` is now `curl -q -sS --fail --max-time 120`.
   `--fail` makes `curl` exit non-zero on an HTTP 4xx/5xx response (and `-S`
   prints the error under `-s`), so an HTTP benchmark fails the query instead of
   silently timing a server exception response as a fast successful sample.
   Unlike `tests/queries/shell_config.sh` (which omits `--fail` so some
   stateless tests can inspect error bodies), a benchmark must fail closed.

Document both behaviors in `tests/performance/README.md`.

Focused evidence (the two failure paths exercised in isolation):
- timeout: a `bash` script whose grandchild is `sleep 60` is timed out; after
  `os.killpg` the grandchild is gone (`alive_after_kill=False`).
- HTTP error: against a server returning HTTP 500, `curl -q -s` exits 0 (the
  bug — measured as success) while `curl -q -sS --fail` exits 22 (the query
  fails).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
# {"kind": "shell", "script": "..."} -- run as a bash script, see below.
test_queries = []
for e in root.findall("query"):
if e.get("type") == "shell":

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looking at the following code, a class Query looks attractive to add. With prewarm, run, human-readable display and so on

@alexey-milovidov

Copy link
Copy Markdown
Member Author

@groeneai, why was the performance comparison skipped here? I wanted to validate if the new logic and new tests work.

@groeneai

Copy link
Copy Markdown
Collaborator

@alexey-milovidov It ran. A retry then hid it behind the digest cache.

Run 27928277351 on ad3b1b3e has two attempts:

  • Attempt 1 (03:48 UTC): the full Performance Comparison executed. The LEFT/RIGHT results are recorded in CIDB (04:49-04:54 UTC), including your new http_read_gzip test, all success.
  • Attempt 2 (06:36 UTC): same SHA, so every job's per-job digest matched, was "reused from cache", and is shown as SKIPPED. The grey boxes you see are attempt 2; only Config Workflow and Finish Workflow actually re-ran.

The single red X is Finish Workflow, not perf: can_be_merged.py lists ci-performance in forbidden_labels, so it exits 1 with "ci-performance label is set, merge not allowed". That is by design for the perf-only label, unrelated to whether the tests ran.

So the new shell-script path works: http_read_gzip produced valid old/new comparisons in attempt 1. One gap worth checking: I find no recorded results at all for your other new test clickhouse_local_startup (nothing in CIDB for this SHA, only http_read_gzip). It may have been dropped as too short or errored on one of the two servers.

The attempt-1 results already exist, so you can validate from there. To force a fresh, non-cached run, push a commit touching a perf file (one of the new .xml tests or the perf scripts) so the digest changes and the cache will not reuse.

Report (amd batch 6/6, where http_read_gzip ran): https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=108081&sha=ad3b1b3e091f1b4fa67b2b8f554b1e9f13446f0c&name_0=PR&name_1=Performance%20Comparison%20%28amd_release%2C%20master_head%2C%206%2F6%29

@alexey-milovidov alexey-milovidov removed the ci-performance performance only label Jun 24, 2026
Comment thread tests/performance/scripts/perf.py Resolved
alexey-milovidov and others added 2 commits June 24, 2026 21:35
…ently

A `<query type="shell">` that failed on one server was swallowed as a
"partial" query: `perf.py` continued on whichever server survived and the
query was dropped from the report and CIDB with no error recorded anywhere
(empty `run-errors.tsv`, green job). A shell test broken on one build looked
indistinguishable from one that simply produced no data.

Unlike an SQL query that may legitimately use a function missing from the old
server, a shell-script benchmark is expected to run on every server, so a
failure on any of them makes the comparison meaningless. Now `perf.py` fails
the query loudly and echoes the captured error from each failing server to
stdout as a `run-error` line, which survives in the archived per-test raw
`.tsv` (the per-test stderr log is not uploaded). `compare.sh` folds those
`run-error` lines into `run-errors.tsv`, so the failure shows up in the
report's Run Errors table and in CIDB.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Root cause of `clickhouse_local_startup` producing no comparison: the patched
("right") build was only symlinked under its subcommand names
(`clickhouse-server`, `clickhouse-local`, `clickhouse-client`,
`clickhouse-keeper`) but never as a bare `clickhouse`, because `--ch-path`
defaults to `ci/tmp` while the symlinks live in `ci/tmp/perf_wd/right`. The
reference ("left") build, by contrast, is downloaded by `wget` as a bare
`left/clickhouse`.

Shell-script queries (`<query type="shell">`) run the multi-call binary
directly through `$CLICKHOUSE_BINARY` / `$CLICKHOUSE_LOCAL` /
`$CLICKHOUSE_CLIENT`, which `compare.sh` builds from `left/clickhouse` and
`right/clickhouse` (passed via `--binary`). With `right/clickhouse` missing,
`right/clickhouse local` failed with "No such file or directory" (exit 127) on
the patched server only, so `clickhouse_local_startup` ran on the reference
server alone, was recorded as a "partial" query and dropped from the report and
CIDB. HTTP queries were unaffected because they talk to the already-running
server (`right/clickhouse-server`) over curl, not to the bare binary.

Verified by reproducing the exact failure locally: a `right/` directory with
only the subcommand symlinks makes `right/clickhouse local` exit 127, while a
`left/` with a bare `clickhouse` succeeds.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Comment thread tests/performance/scripts/perf.py Resolved
alexey-milovidov and others added 2 commits June 25, 2026 03:51
Address review (`perf.py:486`): the SQL path connects through
`clickhouse_driver.Client` honouring `--user` / `--password` / `--secure`,
but the per-server shell environment built by `shell_env_for` always uses an
unauthenticated, plaintext `$CLICKHOUSE_CLIENT` and a `http://`
`$CLICKHOUSE_URL`. Running a shell-script query under non-default credentials
or TLS would therefore either fail during shell execution or, worse, measure a
different (default plaintext) endpoint than the SQL setup/prewarm connected to,
silently invalidating the comparison.

Until the shell helpers carry these options, fail closed: reject a test that
mixes shell-script queries with non-default `--user` / `--password` / `--secure`
up front, rather than benchmarking the wrong endpoint. The check runs after the
`--print-queries` / `--print-settings` early exits so those read-only paths are
unaffected. No current `perf.py` caller (`compare.sh`, `performance_tests.py`,
`collect_clickhouse_profiles.py`) passes these options, so the guard never fires
in practice; it only documents and enforces the current limitation.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Address review (`perf.py:105`): `ci/jobs/collect_clickhouse_profiles.py` runs
every `tests/performance/*.xml` against a single instrumented server started
with only `--tcp_port` (its perf config removes `<http_port>`), and it invokes
`perf.py` without `--binary` / `--http-port`. The new shell-script tests rely on
exactly those: `clickhouse_local_startup` would run whatever `clickhouse` is in
`PATH` (not the instrumented / BOLT binary), and `http_read_gzip` would hit an
HTTP endpoint that is not listening.

Shell-script queries measure tool startup and HTTP timing rather than query
code paths, so they are useless for PGO/BOLT profiles. Skip them in profile
collection (logged, never silently dropped). Detection parses the test XML for a
`<query type="shell">`; a parse error is treated as "no shell query" so the test
still runs and `perf.py` reports the real error.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Jun 25, 2026
@alexey-milovidov alexey-milovidov self-assigned this Jun 25, 2026
Merged via the queue into master with commit 38dd5b7 Jun 25, 2026
167 checks passed
@alexey-milovidov
alexey-milovidov deleted the perf-shell-script-tests branch June 25, 2026 18:46
@groeneai

Copy link
Copy Markdown
Collaborator

@alexey-milovidov Confirmed the shell-script perf path is working (clickhouse_local_startup ran, stable, merged).

On the ~100ms: profiled clickhouse-local --query "SELECT 1". The dominant cost is creating the global BackgroundSchedulePool.

DatabaseCatalog::createBackgroundTasks() is the first caller of Context::getSchedulePool(), which lazily constructs the pool, and the constructor eagerly spawns all background_schedule_pool_size threads (default 512). For a one-shot query these 512 threads are created and torn down without doing any work.

Evidence (local debug build, but the cost is thread creation so it is build-independent):

  • strace -f -c: clone3 is 64% of syscall time; 528 clone3 + 526 munmap (the thread stacks are freed immediately).
  • Driving it with --background_schedule_pool_size:
    • 512 (default): ~528 clone3, this phase ~58ms, wall ~0.20s
    • 16: ~30 clone3, this phase ~2ms, wall ~0.08s
    • 4: ~18 clone3, this phase ~0.5ms
  • Lower tiers are small: attach system tables ~13ms, information_schema ~7ms, registerFunctions ~6ms.

So ~60% of startup is just spawning a thread pool that clickhouse-local does not need at full size. Lowering the default background_schedule_pool_size for ApplicationType::LOCAL (still user-overridable), the way local already forces max_concurrent_queries=0, removes it. I verified pool=16 keeps aggregations, system tables, and persistent MergeTree in --path mode working.

Two ways to do it; which do you prefer before I open the PR:

  1. Lower the local default (e.g. 16). Minimal, local-only, low risk.
  2. Make BackgroundSchedulePool spawn threads on demand. Helps every entry point but touches a shared core class used by the server.

Session id: cron:clickhouse-worker-slot-4:20260625-190000

@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 25, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
Address the AI review on ClickHouse#108081 (ClickHouse#108081):

1. `run_shell_query` now runs the script in its own session
   (`start_new_session=True`) and, on timeout, kills the whole process group
   with `os.killpg` instead of only the immediate `bash`. Previously
   `subprocess.run(..., timeout=...)` killed just `bash`, so a script blocked
   inside a child such as `curl` or `$CLICKHOUSE_LOCAL` kept running after
   `--max-query-seconds`, consuming CPU/network or holding a server-side query
   open and polluting later measurements.

2. The default `CLICKHOUSE_CURL` is now `curl -q -sS --fail --max-time 120`.
   `--fail` makes `curl` exit non-zero on an HTTP 4xx/5xx response (and `-S`
   prints the error under `-s`), so an HTTP benchmark fails the query instead of
   silently timing a server exception response as a fast successful sample.
   Unlike `tests/queries/shell_config.sh` (which omits `--fail` so some
   stateless tests can inspect error bodies), a benchmark must fail closed.

Document both behaviors in `tests/performance/README.md`.

Focused evidence (the two failure paths exercised in isolation):
- timeout: a `bash` script whose grandchild is `sleep 60` is timed out; after
  `os.killpg` the grandchild is gone (`alive_after_kill=False`).
- HTTP error: against a server returning HTTP 500, `curl -q -s` exits 0 (the
  bug — measured as success) while `curl -q -sS --fail` exits 22 (the query
  fails).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…t-tests

Support shell-script queries in performance comparison tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-build Pull request with build/testing/packaging improvement pr-performance Pull request with some performance improvements pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants