Skip to content

Fix --clients-start runs ending up to 1 s before --test-time - #722

Merged
fcostaoliveira merged 1 commit into
masterfrom
fix-713-staircase-finish
Oct 16, 2026
Merged

fcostaoliveira merged 1 commit into
masterfrom
fix-713-staircase-finish

Conversation

@fcostaoliveira

@fcostaoliveira fcostaoliveira commented Oct 16, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

In staircase mode (--clients-start), a --test-time run ends when the wall clock's seconds counter reaches start_sec + test_time, not when test_time seconds have passed. "Total duration" for --test-time 3 was anywhere between about 2.0 and 3.0 s, the Finish time always landed on a whole second, and the last staircase step got a shorter window than the others.

Root cause

client::finished() and client_group::handle_staircase_step() computed the elapsed time as now.tv_sec - benchmark_start_time.tv_sec, dropping tv_usec. A run that started at 41.598 s had elapsed >= 3 at 44.000 s.

Fix

  • client::finished() compares the full elapsed timeval (timersub), as shard_connection::run_is_over() already does.
  • client_group::handle_staircase_step() now decides by step count, not by the clock: step k (due at k * step_duration) is taken only if k * step_duration < test_time. An exact clock check does not work there: the EV_PERSIST timer runs on libevent's monotonic clock and can fire a hair before the deadline by gettimeofday() (e.g. elapsed 2.9993 s for --test-time 3), which connected one more client per thread right before the run stopped (and, in cluster mode, sent a needless CLUSTER SLOTS from a client that can never start). The full-timeval clock check is kept as a second stop condition: after an event-loop stall (SIGSTOP, VM pause) libevent reschedules the EV_PERSIST timer from "now", so the count lags the clock and a step could otherwise fire after the deadline. The clock half cannot fire early, so it does not bring back the extra step. Checked with --test-time 5 --step-duration 2 and SIGSTOP at 1 s for 3.5 s: no step after the deadline, 7.5 s wall (same as before the count rule; the count rule alone ran 9.5 s with 4 extra activations).
  • The live display's client count now rounds down from the microsecond difference (ts_diff).
  • JSON Active Clients (run_stats.cpp) is capped at the last step due before --test-time, (test_time - 1) / step_duration. The trailing Time-Serie bucket at t = test_time therefore no longer reports a step that never ran. For example, -c 5 --clients-start 1 --clients-step 1 --step-duration 1 --test-time 3 used to report 4 per thread in bucket "3" and now reports 3. This corrects a value in the JSON output.
  • The special case Retry CLUSTER SLOTS after an empty, error or malformed reply instead of hanging #667 added in handle_cluster_slots_retry_event() is kept. Under --clients-start it is now equivalent to run_is_over(), but it is not clearly redundant in the other modes, so only its comment about whole seconds is removed.

Behaviour note: since staircase runs now last the full --test-time, the last requests can complete a few hundred µs after the deadline, so the Time-Serie may contain a small trailing bucket at t = test_time. Non-staircase runs already have this. No existing staircase test encoded the old behaviour; all existing tests in tests/test_client_staircase.py pass unchanged.

Test evidence

Two new tests in tests/test_client_staircase.py:

  1. test_staircase_runs_full_test_time launches memtier half way through a second and checks Total duration is within [2900, 3300] ms for --test-time 3 --clients-start 1 --clients-step 1 --step-duration 1.
    • origin/master f50bd03: 6/6 FAIL (2468-2494 ms, finish at xx000). This branch: 6/6 PASS.
    • Raw repro, random start offsets: this branch 20/20 within range (3000-3002 ms); master 4/6 out of range (2145, 2241, 2631, 2764 ms).
  2. test_staircase_no_step_at_deadline runs -t 8 -c 5 --clients-start 1 --clients-step 1 --step-duration 1 --test-time 3 three times and asserts the server's total_connections_received delta is exactly 24 (start + 2 steps per thread).
    • Previous head 39636c3 (exact clock check): 5/5 FAIL (25-32 connections).
    • This branch: 15/15 PASS.

tests/test_client_staircase.py (standalone): 13/13 pass. tests/test_cluster_slots_bad_reply.py (oss-cluster, 3 shards, includes #667's late staircase tests): 16/16 pass.

Fixes #713

Affected versions

2.3.0 through 2.5.2 (latest) and master.

🤖 Generated with Claude Code


Note

Medium Risk
Changes core benchmark stop and ramp-up timing in staircase mode; behavior shifts from early termination to full test duration, with new edge-case guards for timer vs wall clock.

Overview
Fixes staircase mode (--clients-start) runs ending up to 1 second early when --test-time was compared using whole seconds only (tv_sec difference), ignoring microseconds.

Deadline handling: client::finished() now uses timersub for the global staircase deadline, matching full wall-clock elapsed time. The staircase timer callback tracks m_staircase_steps and stops ramp-up when either steps * step_duration >= test_time (avoids an extra client when libevent fires slightly before the wall-clock deadline) or the full timeval elapsed time reaches test_time (covers event-loop stalls where the step count lags).

Reporting: Live progress client count uses ts_diff for elapsed seconds. JSON Active Clients caps steps after the deadline so trailing time buckets do not show a step that never ran.

Adds regression tests for full --test-time duration and for no connections added at the deadline step.

Reviewed by Cursor Bugbot for commit daf351f. Bugbot is set up for automated code reviews on this repo. Configure here.

@fcostaoliveira
fcostaoliveira force-pushed the fix-713-staircase-finish branch from 39636c3 to 927e09e Compare October 16, 2026 17:25
@github-actions

github-actions Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

🤖 Automated first-pass review — a human maintainer's review is still required before merge.

Thanks for this. I went through it and the fix looks right to me. Using timersub in finished() matches what run_is_over() already does. Counting steps in handle_staircase_step() and keeping the full-timeval clock check as a fallback for stalls makes sense, and the PR description explains the monotonic-vs-gettimeofday() reasoning well. Both new tests check the real behaviour (the server's connection count and Total duration), not just a manual run, and the first one is shown to fail on master. That's the bar we want for timing/output fixes like this.

A few small things:

  • run_stats.cpp now caps the JSON "Active Clients" at the last step due before --test-time, but the live display in run_benchmark() still computes elapsed_secs / step_duration with no cap. So a display tick at or just after the deadline could show one more step than was ever taken. It's cosmetic, but did you consider applying the same (test_time - 1) / step_duration cap there so the two agree?
  • The run_stats.cpp cap is explained in the commit message but not in the PR description. Worth a line there, since it changes a value in the JSON output (even if it's a correction).
  • Just checking: is the [2900, 3300] ms window in test_staircase_runs_full_test_time safe on the CI runners with PARALLELISM=2? Your numbers (3000-3002 ms) leave plenty of room, so this is probably fine. I'm only asking because Total duration includes the drain of the last in-flight requests.

Nitpicking: the comment above the deadline check in handle_staircase_step() now has the old paragraph followed by the new "Two checks" paragraph. Merging them into one would read a bit better.

None of this blocks the merge as far as I can tell. The core change looks solid.

(would file this as a comment, not a formal block)

@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Thanks for the review. Point by point:

  • (a) Description out of sync: fixed. The body now describes the step-count rule, why an exact clock check alone over-steps (libevent's monotonic timer fires a hair before the deadline as seen by gettimeofday()), and lists both new tests. Since then, 0b4f272 also keeps the full-timeval clock check as a second stop condition. With the count rule alone, an event-loop stall (SIGSTOP, VM pause) lets libevent reschedule the EV_PERSIST timer from "now", so a step could fire after the deadline. The clock half can't fire early, so it doesn't bring the extra step back. The code comment explains why both checks exist.
  • (b) Timer stays armed until the first step past the deadline when step_duration doesn't divide test_time: agreed. This behaviour predates this PR and is out of scope here. It is being filed as a separate issue, which will be linked here.
  • (c) total_connections_received assumes one connection per client: that is true today. In standalone mode each staircase client opens exactly one connection, with no probe or setup connection. The test asserts the exact count on purpose: if that ever changes, we want the test to flag it and be updated deliberately. The JSON "Active Clients" series can't catch this bug. The extra clients are connected under 1 ms before the run stops, so they never show up in a per-second bucket.
  • (d) PARALLELISM=2: at 39636c3, all four standalone cells (Plaintext, TLS, TLS v1.2, TLS v1.3) ran with --parallelism 2, and test_staircase_runs_full_test_time passed in every one, with a wall time of 3.000 s in the logs. Each RLTest worker has its own redis, so the connection-count test is isolated from the other worker.

@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Follow-up to (b) in the reply above: the pre-existing behaviour where the staircase timer stays armed past --test-time is now tracked in #728.

In staircase mode the run's deadline was checked as
now.tv_sec - benchmark_start_time.tv_sec >= test_time, which drops the
microseconds. A run that started at x.6 s therefore ended at x+test_time
whole seconds, about 0.4 s early: "Total duration" for --test-time 3 was
anywhere between 2.0 and 3.0 s, and the last staircase step got a
shorter window than the others.

client::finished() now compares the full elapsed timeval, as
run_is_over() already does.

client_group::handle_staircase_step() used the same whole-second check
to stop at the deadline. It now decides by the step count instead: step
k is due at k * step_duration and is taken only if that is before
test_time. A clock check does not work there either way: the timer runs
on libevent's monotonic clock and can fire a hair before the deadline by
gettimeofday(), so an exact check connected one more client per thread
at the deadline, which the run then stopped at once. The clock is still
checked as well, with full timevals: after a stall of the event loop
(SIGSTOP, VM pause) libevent reschedules the timer from "now", so the
count lags behind and a step could otherwise fire after the deadline.

The JSON "Active Clients" entry is computed from the bucket's second.
Now that the last requests can complete just after the deadline, in a
bucket at t = test_time, cap it at the last step due before test_time,
so that bucket does not count a step that was never taken.

The live display's client count now rounds down from the microsecond
difference.

The special case PR #667 added in handle_cluster_slots_retry_event() is
kept, only its comment about whole seconds is dropped.

Fixes #713

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fcostaoliveira
fcostaoliveira force-pushed the fix-713-staircase-finish branch from 0b4f272 to daf351f Compare October 16, 2026 18:53
@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Re the 17:48 review:

  • "Active Clients" for the trailing bucket: fixed in daf351f. run_stats.cpp computes the count from the bucket's second. It now caps steps_done at the last step due before the deadline ((test_time - 1) / step_duration, i.e. steps k with k * step_duration < test_time) as well as at --clients, so bucket "3" for -c 5 --clients-start 1 --clients-step 1 --step-duration 1 --test-time 3 reports 3 per thread, not 4. The real activated count isn't recorded per second today. Recording it would mean new per-second state in the stats path, which I left out of this PR to keep the diff small, so the cap is the minimal fix.
  • Test: test_staircase_no_step_at_deadline now also asserts every JSON Active Clients entry for that config, the trailing bucket included. On 0b4f272 it fails 5/5 ("Active Clients at t=3s is 4, expected 3"); with the fix it passes 5/5. tests/test_client_staircase.py: 13/13 pass.
  • Nit: added a comment that the test's own connection is opened before the first total_connections_received snapshot, so it isn't counted.

@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Thanks. Point by point:

@fcostaoliveira
fcostaoliveira merged commit 0fc29f4 into master Oct 16, 2026
129 checks passed
fcostaoliveira added a commit that referenced this pull request Oct 16, 2026
In staircase mode the step timer is EV_PERSIST, and the deadline was only
checked when it fired. Once the clients stopped at --test-time, that
pending timer was the only event left and kept event_base_dispatch()
running until its next tick: --test-time 2 --step-duration 20 took 20 s
of wall time, --test-time 5 --step-duration 3 took 6-7 s. The results
were not affected, the process just idled.

setup_staircase_timer() now also adds a one-shot timer at
benchmark_start_time + test_time that removes the step timer, so the
thread's loop ends when its clients do. The step timer stays persistent,
so the step schedule does not drift. If either event_new() fails, log it
instead of passing NULL to event_add(); without the deadline timer the
old behaviour (stop at the first tick past the deadline) remains, which
is also why handle_end_stalled_clients() still removes the step timer.

The live progress line now caps its staircase client count at the last
step due before --test-time, as the JSON "Active Clients" does since
#722. On the final line it used to count a step that never ran (4 conns
instead of 3 for -c 6 --test-time 3 --step-duration 1). The StatsD
"connections" and Prometheus memtier_connections gauges are fed from the
same count, so they change the same way.

Tests, both failing on master:
- test_staircase_exits_at_test_time runs --test-time/--step-duration
  2/20, 5/2, 4/2 and 5/3 and requires the worker threads' lifetime (JSON
  CPU cpu_wall_seconds, which excludes process startup) to stay below
  test_time + 0.5 s; without per-thread CPU accounting it falls back to
  process wall time < test_time + 3 s. It now also runs on oss-cluster.
- test_staircase_progress_clients_match_json checks that the last
  progress line's conns equals the last JSON Active Clients entry.

Fixes #728

Co-Authored-By: Claude Opus 5.5 <[email protected]>
fcostaoliveira added a commit that referenced this pull request Oct 16, 2026
)

With --clients-start, setup_staircase_timer() added an EV_PERSIST step
timer and the deadline was only checked when that timer fired. Once the
clients stopped at --test-time, the pending step timer was the only
event left and kept event_base_dispatch() running until the next step
tick: --test-time 2 --step-duration 20 took 20 s.

setup_staircase_timer() now also adds a one-shot timer at
benchmark_start_time + test_time that removes the step timer, so the
step schedule does not drift and the loop ends at the deadline. The
four copies of the step-timer teardown are folded into
remove_staircase_timer(), and both event_new() results are checked.

The live progress line caps the staircase client count at the last step
due before --test-time, as the JSON "Active Clients" does since #722.
The StatsD connections and Prometheus memtier_connections gauges come
from the same count, so after the deadline they no longer report a step
that never ran.

Tests: test_staircase_exits_at_test_time (2/20, 5/2, 4/2, 5/3; worker
thread lifetime from the JSON, also on oss-cluster) and
test_staircase_progress_clients_match_json; both fail on master.

Fixes #728

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

--clients-start: --test-time deadline compares whole seconds, so staircase runs end up to 1 s early (Total duration 2.0-3.0 s for --test-time 3)

1 participant