Skip to content

Average merged Start/Finish time over all threads' clients - #723

Merged
fcostaoliveira merged 4 commits into
masterfrom
fix-709-merge-weight
Oct 16, 2026
Merged

fcostaoliveira merged 4 commits into
masterfrom
fix-709-merge-weight

Conversation

@fcostaoliveira

@fcostaoliveira fcostaoliveira commented Oct 16, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

With more than one thread (-t N), the JSON Runtime Start time, Finish time and Total duration are meant to be the mean over all clients. They were the mean over the last thread's clients only. Every Ops/sec and KB/sec in the report is divided by that duration, so when the threads' durations differ (per-thread throughput under --requests, a slow or reconnecting connection, a thread cut short), every rate is computed against the wrong window.

Root cause

run_benchmark() calls client_group::merge_run_stats() once per thread, and every call merges into the same run_stats. merge_run_stats() set iteration_counter = 1 at the start of every call. run_stats::merge() computes the running mean timeval_factorial_average(avg, new, w) = (avg*(w-1) + new)/w, so each thread's first client, merged with weight 1, replaced the average built so far.

Fix

The caller owns the counter. merge_run_stats(run_stats *target, unsigned int &iteration_counter) takes it by reference, and run_benchmark() passes the same one (starting at 1) for every thread, so the weight keeps growing across threads. #707's rule is kept: clients that never started are still merged for their connection errors and do not advance the counter. This is the only caller.

Overflow in the running mean

With the weight now counting all of the run's clients, timeval_factorial_average() overflowed: it computed (a*(w-1) + b)/w in int64 epoch microseconds (~1.8e15), which wraps once w passes about 5100. Before this PR the weight restarted per thread, so that took more than about 5100 clients in one thread (-t 1 -c 5200 already broke on master). The mean is now computed as a + floor((b - a)/w). It gives the same value as the old formula wherever the old one did not overflow, so small runs are unchanged.

Test evidence

New RLTest test_reconnections.py::test_merged_duration_averages_all_threads (standalone, skipped on cluster). It runs -t 4 -c 1 --requests=300 --rate-limiting=100 --reconnect-on-error --reconnect-backoff-factor=3, so each client takes about 3 s. One client is killed once with CLIENT KILL ID, waits 3 s before reconnecting, and so ends about 3 s after the others. The rate limiter resets its budget on disconnect and does not catch up. The correct mean is about 3.75 s. The killed client connects first, so it is never in the last thread, and master always reports the last thread's about 3 s. The test asserts the mean plus or minus half of the delayed client's share: 3375 < Total duration < 4125 ms.

Local runs at 5886d22 against redis on localhost:

binary runs result Total duration (ms)
master bc7320a 10 10/10 FAIL 2995-2996
this branch 25 25/25 PASS 3737-3767
mutant: counter advances by 2 per client 5 5/5 FAIL 4357-4358

The margins are about 380 ms below the branch value, 360 ms above master, and 230 ms below the mutant. An earlier version of the test used the default 1 s delay and a 3100-3650 ms window. It was too narrow: master once passed at 3110 ms, and the counter mutant (~3442 ms) passed too.

The test matches the "attempting reconnection" log line. Both disconnect paths write it: EOF ("Connection dropped") and error ("Connection error", e.g. ECONNRESET or TLS). Requiring "Connection dropped" was a likely cause of a rare earlier failure (Bugbot). The assertions also report the exit code, the CLIENT KILL result, whether memtier was still running at the kill, and the stderr tail. Two earlier local failures were host stalls: RLTest reported Total durations of 2759094 ms and 6351734 ms, and its own "Test Took" time and /proc/uptime showed the same stall.

Unit test run_stats_merge_test (new make check program, linked like cluster_hslot_test). It merges 6000 runs with realistic epoch timestamps through run_stats::merge() and checks the merged start (within 2 ms of the exact mean; flooring each merge drifts it by a fixed 935 us with these inputs) and the duration (exact). It also checks exact floor results for small weights.

  • With the old formula (master bc7320a and d34bdfd): FAIL, start -1282306700097482, duration 3074466889699128 us.
  • With this branch: PASS, start within 935 us of the mean, duration exactly 3000000 us.

End-to-end against redis with maxclients 20000, --hide-histogram --json-out-file:

run binary Start time Finish time Total duration
-t 6 -c 1000 --requests 200 master bc7320a 1792160266786 1792160286581 19795
d34bdfd (counter fix only) -1282297056869 -1282297032428 24441
this branch 1792160316027 1792160340353 24326
-t 16 -c 900 --requests 100 master bc7320a 1792160347034 1792160376362 29328
d34bdfd (counter fix only) 511136488705 511136516606 27901
this branch 1792160417230 1792160447058 29828

The RLTest now matches the "attempting reconnection" log line, which both the EOF ("Connection dropped") and the error ("Connection error") disconnect paths write; requiring "Connection dropped" was one cause of the rare failures (Bugbot). After this change it passed 15/15 locally. The assertions also report the exit code, the CLIENT KILL result, whether memtier was still running at the kill, and the stderr tail. In later local batches every failure on this branch was a host stall: one run reported Total duration 2759094 ms and another 6351734 ms, and RLTest's own "Test Took" time and /proc/uptime showed the same stall. One failure had memtier still running at the kill with rc=0 and killed=1, but its stderr tail was not saved, so its cause is unconfirmed.

Release notes

  • Fix: with more than one thread, the JSON Runtime Start time, Finish time and Total duration are now the mean over all clients, not over the last thread's clients only (Multi-thread runs: merged Start time / Finish time / Total duration average only the last thread's clients (merge_run_stats restarts the averaging weight per thread) #709). Multi-thread Ops/sec and KB/sec can shift compared with 1.2.4-2.5.2 when threads ran for different lengths of time.
  • Fix: Start and Finish time no longer overflow to garbage (negative or decades-off) values with more than about 5100 clients in total. On older versions this needs more than about 5100 clients in one thread, e.g. -t 1 -c 5200.
  • The merged start time also feeds the --hdr-file-prefix .hgrm log: the #[StartTime: ...] header and the interval timestamps written by save_hdr_log_format. These also change for multi-thread runs, and above about 5100 clients they were corrupt in the same way.

Affected versions

1.2.4 through 2.5.2 (latest) and master.

Fixes #709

🤖 Generated with Claude Code


Note

Medium Risk
Changes core benchmark reporting math (merged duration and all derived rates) for multi-thread runs; behavior is intentional but affects published JSON metrics.

Overview
Fixes #709: with -t > 1, JSON Runtime Start time, Finish time, and Total duration (and every rate derived from them) were averaged over the last thread’s clients only, because each client_group::merge_run_stats() call reset the merge weight to 1.

run_benchmark() now keeps a single merge_iteration counter (starting at 1) and passes it by reference into every thread’s merge_run_stats(), so the running mean spans all clients across threads. Clients that never started still do not advance the counter.

timeval_factorial_average() in run_stats.cpp uses a + floor((b - a) / weight) instead of (a * (weight - 1) + b) / weight, avoiding int64 overflow when thousands of clients are merged with epoch microsecond timestamps.

Adds run_stats_merge_test (make check) and RLTest test_merged_duration_averages_all_threads (4 threads, one delayed reconnect) to lock in the correct mean duration.

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

@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. Passing the counter in from the caller is the smallest fix and keeps #707's rule. I checked the new timeval_factorial_average(): with the diff % weight < 0 correction it is exactly floor((a*(w-1)+b)/w), so small runs don't change. The RLTest fails on master and passes here, and the unit test checks the >5100-client overflow directly, which meets the fails-then-passes bar (PR#364 is an earlier example in this repo).

One question, not blocking: each merge still floors to a whole microsecond. That error adds up roughly linearly with client count, which is where the 935 us at 6000 clients comes from, so very large runs would drift Start/Finish by several ms. The old formula floored the same way, and duration mostly cancels out, so this isn't a regression. Did you consider summing start - first_start offsets in int64 and dividing once at the end? I think that avoids both the overflow and the drift. It's fine to leave it for a follow-up if you'd rather keep this diff small.

On the output change: Ops/sec and KB/sec can shift for multi-thread runs, but the old numbers were wrong, so I don't see a less-breaking design that would be worth keeping. The release note covers it. Looks good to me otherwise.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 43979dc. Configure here.

Comment thread tests/test_reconnections.py Resolved
fcostaoliveira and others added 2 commits October 16, 2026 15:14
With more than one thread, run_benchmark() calls merge_run_stats() once
per thread, all into the same run_stats. merge_run_stats() started its
averaging weight at 1 on every call, and run_stats::merge() computes the
running mean as (avg * (w - 1) + new) / w, so the first client of each
thread replaced the average built so far. The merged Start time, Finish
time and Total duration, and every Ops/sec and KB/sec derived from them,
came only from the last thread's clients.

Let the caller own the counter: run_benchmark() passes the same one to
every thread's merge_run_stats(), so the weight keeps growing across
threads. Clients that never started still do not advance it (#707).

The test runs four rate-limited single-client threads and disconnects
one client once, so it ends about 1 s after the others. The merged
duration must be the mean of the four (about 3.25 s), not the last
thread's 3 s or 4 s.

Fixes #709

Co-Authored-By: Claude Opus 5.5 <[email protected]>
timeval_factorial_average() computed (a * (w - 1) + b) / w in int64
microseconds. With epoch timestamps (~1.8e15 us) a * (w - 1) wraps once
w passes about 5100. The weight used to restart for every thread, so
this needed one thread with over 5100 clients; now that it counts all
the run's clients, -t 6 -c 1000 already gave negative Start and Finish
times in the JSON.

Compute a + floor((b - a) / w) instead. It gives the same value as the
old formula wherever that did not overflow, so small runs are unchanged.

run_stats_merge_test merges 6000 runs with realistic timestamps through
run_stats::merge() and checks the merged start and duration, plus exact
results for small weights. The RLTest assertions now report the exit
code, the CLIENT KILL result and the stderr tail.

Refs #709

Co-Authored-By: Claude Opus 5.5 <[email protected]>
CLIENT KILL can reach the client as EOF, which logs "Connection
dropped", or as an error (ECONNRESET, or a TLS error), which logs
"Connection error". Both paths reconnect and log "attempting
reconnection", so match that. Note why the rate limiter does not catch
up after the reconnect, and give the unit test's deterministic 935 us
drift a 2 ms bound.

Refs #709

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Thanks for the review. Answers, as of f2ece53:

RLTest flakiness. I checked the failures I saw locally:

  • One was the log-line assertion Bugbot flagged: a CLIENT KILL that arrives as an error logs "Connection error", not "Connection dropped". The test now matches the "attempting reconnection" line that both paths log.
  • Two were host stalls, not timing sensitivity. RLTest reported Total durations of 2759094 ms and 6351734 ms, and its own "Test Took" time and /proc/uptime showed the same stall.
  • After the fix it passed 15/15 locally.
  • The CLIENT KILL, return-code and reconnect assertions now print the exit code, the kill result, whether memtier was still running at the kill, and the stderr tail. Any CI failure will show its cause.

On the bounds:

  • The window is 3100-3650 ms. The correct value measures about 3240 ms. The buggy value is about 2995 ms, or about 3995 ms when the last thread holds the killed client.
  • The rate limiter keeps every client at about 3 s regardless of scheduling, and only one 1 s reconnect delay moves the mean. I left the lower bound at 3100: below that, a wrong result could pass.

Rate limiter catch-up. It does not catch up. disconnect() sets m_request_per_cur_interval = 0. On reconnect the timer is recreated with a fresh request_per_interval, and each refill assigns the per-interval budget rather than adding to it. So the delayed client finishes about 1 s late. The local runs agree: the killed client ends at about 3.97 s of wall time. The test docstring now says this.

Release notes. Added to the PR body, including the separate -t 1 -c 5200 overflow.

Cross-thread coverage. You're right that run_stats_merge_test covers the overflow and only the RLTest covers the per-thread counter. Unit-testing merge_run_stats() would mean building client_groups with real clients. I kept that out to keep the diff small.

With the default 1 s reconnect delay the correct mean (~3.24 s) sat
only ~0.25 s above the buggy last-thread value (~3.0 s). One slow client
could carry master over the 3.1 s lower bound, and a weight that
overcounts the first client (~3.44 s) passed the 3.65 s upper bound.

Use --reconnect-backoff-factor=3 so the killed client waits 3 s. The
mean is then ~3.75 s, and the bounds are that mean plus or minus half
of the delayed client's share (3375-4125 ms). Master gives ~3.0 s and a
weight advancing by 2 per client gives ~4.36 s. Correct the docstring:
the killed client connects first, so it is never in the last thread,
and master always reports ~3 s.

Refs #709

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fcostaoliveira

Copy link
Copy Markdown
Collaborator Author

Thanks. On the floor drift: yes, I considered keeping an int64 sum of start - first_start offsets and dividing once. It removes both the overflow and the drift. I kept this diff to the running mean because that is the formula #707 and the existing callers use. The drift was already there before this PR, and it doesn't affect Duration or any rate, since Start and Finish drift together: about 1.5 ms of absolute Start at 6000 clients, against 0 to 23 us of duration error in the reviewers' simulations. I'll take the exact-sum version as a follow-up rather than widen this one.

@fcostaoliveira
fcostaoliveira merged commit 5b92b23 into master Oct 16, 2026
129 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant