The Optimization Pipeline: From 700 MB/s to 85 GB/s in a Distributed S3 Load Test
Introduction
In the course of building a horizontally scalable S3-compatible storage system for the Filecoin Gateway, a single performance bottleneck became the focus of an intense optimization session. The system under development featured a three-layer architecture: stateless S3 frontend proxies routing requests to independent Kuri storage nodes, with object metadata shared via YugabyteDB. After establishing a working test cluster, debugging the monitoring dashboard, and fixing critical bugs, the team turned its attention to performance — specifically, to the load testing utility that would validate the system's throughput and correctness.
What followed was a textbook example of profiling-driven optimization. The assistant, guided by the user's demanding performance targets, identified and eliminated bottlenecks in two distinct layers: the S3 frontend proxy's HTTP data path and the load test's data generator. The results were dramatic: the data generator's throughput jumped from approximately 700 MB/s to over 85 GB/s — a 120x improvement that was not about writing faster code but about removing the hidden costs that were silently consuming CPU cycles.
This article traces that optimization journey, examining the decisions, assumptions, profiling insights, and iterative fixes that transformed a functional-but-slow data generator into a high-performance benchmarking tool.
The Optimization Imperative
The optimization phase began with a clear directive from the user at message 950, which laid out three specific tasks: integrate go-buffer-pool into all S3 HTTP paths to eliminate allocation overhead, implement shard-based random data generation in the loadtest to avoid the crypto/rand bottleneck, and add unit-test benchmarks with a mock HTTP server to validate performance [143]. This was not a vague request for "make it faster" — it was a precise, actionable specification targeting specific components and specific optimization techniques.
The user's message revealed deep architectural thinking. The buffer-pool directive targeted Go's standard io.Copy and io.ReadAll calls, which allocate fresh 32KB buffers on every invocation. In a high-throughput S3 proxy handling concurrent uploads and downloads, these allocations accumulate rapidly, pressuring the garbage collector and causing latency spikes. The go-buffer-pool library, already a dependency in the project's carlog and rbdeal packages, provided a pool of reusable byte buffers that could eliminate this allocation overhead entirely.
The shard-based data generation directive was equally insightful. The naive approach to generating test data — calling crypto/rand for every byte of every payload — would become CPU-bound on the random number generator long before the S3 proxy reached its throughput limits. The user's proposed solution was elegant: pre-generate a pool of random shards during initialization, then assemble test payloads by shuffling and concatenating these shards using the much faster math/rand source. This moved the expensive cryptographic randomness to initialization time, making per-request data generation essentially a fast shuffle-and-copy operation.
The third directive — adding benchmarks with a mock HTTP server — closed the feedback loop. Without isolated benchmarks, the team could not measure whether their optimizations were actually working. A mock server that simply read and discarded request bodies would allow the benchmarks to run without the full test cluster, making them fast, reproducible, and suitable for regression testing [143].
Reconnaissance and Implementation
The assistant began by surveying the codebase to understand existing patterns. A grep for io.Copy, io.ReadAll, and ioutil.ReadAll across the S3-related code revealed that the main targets were in server/s3frontend/server.go, specifically in the proxyRequest function that forwarded HTTP requests to backend Kuri nodes [146]. The function read entire request bodies into memory with io.ReadAll, then forwarded them — a pattern that would benefit significantly from buffer pooling.
The assistant also discovered that go-buffer-pool was already used elsewhere in the project, making it a natural fit. The implementation involved replacing io.ReadAll with pool.Get(estimatedSize) to obtain a pre-allocated buffer, and using io.CopyBuffer with a 256KB pooled buffer for copying response bodies back to clients [164]. This was not a complex change — 26 insertions and 5 deletions across one file — but it required understanding the existing buffer-pool infrastructure and the specific I/O patterns in the proxy.
For the loadtest optimization, the assistant read the existing loadtest.go and identified the core problem: every test payload was generated by calling crypto/rand.Read, which is designed for cryptographic security and runs at approximately 700–800 MB/s on the test machine's AMD Ryzen Threadripper PRO 7995WX processor [170]. While this sounds fast, it was the bottleneck compared to what the rest of the system could sustain.
The solution was a ShardedDataGenerator structure that pre-generated a pool of random data shards using crypto/rand once during initialization, then assembled test payloads by selecting and concatenating shards using the much faster math/rand source. Each worker received its own generator instance, eliminating lock contention. The implementation hit several snags — unused imports, a misplaced import statement in the test file, and a worker function signature mismatch — but each was caught by LSP diagnostics and fixed iteratively [172][174].
The Benchmark That Lied
With the optimizations implemented and committed, the assistant ran the benchmarks and reported data generation throughput of approximately 700–850 MB/s. The commit message read like a victory lap: "Performance optimizations," "Benchmark results show ~700-850 MB/s data generation throughput." The todo list was marked complete. The assistant was ready to move on.
Then the user read those numbers and responded with a single sentence that changed everything: "The performance is actually extremely slow for just the benchmark, profile why it's so extremely slow. Expect 2-3GB/s per core" [190].
This was a moment of reckoning. The assistant had accepted 700 MB/s as "good" because it had no reference point for what was achievable. The user, drawing on deep domain knowledge about what a 96-core AMD Ryzen Threadripper PRO 7995WX could deliver, recognized that this was barely a fraction of the hardware's potential. The instruction was precise: don't guess, don't tweak — profile.
The assistant responded by running go test with CPU and memory profiling enabled, capturing a cpu.prof file, and then using go tool pprof -top to analyze it [191]. The output was unambiguous:
- 49.75% of CPU time was spent in
crypto/md5.block— the MD5 checksum computation - 15.58% was spent in
runtime.memclrNoHeapPointers— zeroing newly allocated memory The bottleneck was not in the random number generation at all. It was in the MD5 checksum that theGeneratemethod computed on every payload, presumably for read-after-write verification. The assistant had inadvertently coupled two independent operations — data production and integrity verification — into a single function, and the benchmark was measuring the combined cost rather than isolating the data generation performance [192][193]. This is a classic profiling pitfall. When you measure a composite operation and find it slow, the instinct is to optimize the whole thing. But profiling reveals which part is slow, and often the answer is that a seemingly minor sub-operation is consuming the majority of resources. The MD5 hash, while fast in absolute terms, operates on every byte of data and is fundamentally CPU-bound. When your goal is to measure raw data generation throughput, including an MD5 computation is like measuring a car's top speed while driving with the parking brake engaged.
The Refactoring: Separating Concerns
Armed with this knowledge, the assistant made a strategic decision: separate data generation from checksum computation [194]. The refactoring involved three key changes:
- Adding a
GenerateDatamethod that produces random data without computing a checksum, using a pre-allocated buffer to avoid allocation overhead - Adding a
FillBuffermethod that writes directly into an existing buffer, eliminating both allocation and the MD5 computation entirely - Keeping the original
Generatemethod for cases where the checksum is actually needed, but ensuring the benchmarks could measure each path independently The assistant also replacedmrand.Intn()withmrand.Uint32()for faster random index selection in the shard shuffling logic — a micro-optimization that targeted the shard selection itself, anticipating that after eliminating the MD5 and allocation bottlenecks, this would become a more significant fraction of remaining CPU time [194]. This separation of concerns is a textbook example of the Unix philosophy applied at the function level: each function should do one thing well. The originalGeneratemethod was doing two things (data generation + checksumming), and the benchmark couldn't distinguish between them. By splitting these responsibilities, the assistant created the ability to measure each operation's true cost.
The Duplicate Loop and the Final Fix
The refactoring of the benchmark test file did not go smoothly. When the assistant edited loadtest_test.go to add new benchmark variants for the FillBuffer and GenerateData paths, the edit introduced a syntax error: a duplicate for loop was left behind, causing the Go parser to encounter a loop keyword where it expected a top-level declaration [195][196].
The LSP diagnostics caught this immediately: ERROR [216:2] expected declaration, found 'for'. The assistant read the file to diagnose the problem, identified the duplicate loop, and applied a minimal fix — deleting the offending code [197]. This small correction was the gate that allowed the benchmarks to compile and run, enabling the verification of the entire optimization effort.
The Results: Three Performance Tiers
With the benchmarks running correctly, the assistant discovered three distinct performance tiers that mapped directly to the optimization decisions [198]:
- WithMD5 (the original
Generatemethod): ~700–800 MB/s — unchanged, confirming that MD5 was indeed the bottleneck - DataOnly (
GenerateDatawith allocation but no MD5): ~3–6.5 GB/s — meeting and exceeding the user's 2–3 GB/s target - FillBuffer (no allocation, no MD5): ~50–85 GB/s — an astonishing result that revealed the true memory-bandwidth-bound ceiling The jump from 700 MB/s to 85 GB/s was a 120x improvement. But this wasn't because the assistant made the data generation 120x faster. It was because the original measurement was measuring the wrong thing — it was measuring data generation plus MD5 checksumming, and the checksum was the dominant cost. The
FillBufferpath, which eliminated both allocation and checksumming, revealed the true cost of the data generation itself: essentially just memory bandwidth. The three-tier model also provided a valuable taxonomy for understanding where each bottleneck lived. TheWithMD5tier was CPU-bound on cryptographic hashing. TheDataOnlytier was allocation-bound, limited by memory zeroing and garbage collection. TheFillBuffertier was memory-bandwidth-bound, limited only by how fast the CPU could copy data from the shard pool into the output buffer. Each tier had a different bottleneck, and each required a different optimization strategy.
The Commit That Told the Story
With the optimizations validated, the assistant committed the changes in two logical commits [186][187]. The first covered the S3 frontend buffer-pool changes, explaining the "why" and "how" of the optimization. The second covered the loadtest optimization and benchmarks, including a summary of benchmark results in the commit message. Each commit told a coherent story, making future code archaeology easier.
The assistant then updated the todo list, marking all four high-priority optimization tasks as completed [188]. This was not just administrative housekeeping — it was a signal that the optimization sprint had reached its conclusion, and the project could move on to the next phase.
What This Journey Reveals
The optimization pipeline from 700 MB/s to 85 GB/s reveals several fundamental principles of performance engineering:
Measure before optimizing. The assistant's initial optimization — the shard-based data generator — was clever and well-implemented, but it was solving the wrong problem. The profiling step revealed that MD5, not random number generation, was the dominant bottleneck. Without profiling, the assistant might have continued optimizing the wrong thing indefinitely.
Isolate what you measure. The original Generate method bundled data generation with checksum computation, making it impossible to tell which part was slow. By separating these concerns, the assistant enabled accurate measurement of each component's true cost. The 120x "improvement" was not really an improvement at all — it was a correction of measurement.
Set clear performance targets. The user's expectation of 2–3 GB/s per core provided a concrete goal that drove the entire diagnostic process. Without that target, the assistant might have accepted 700 MB/s as "good enough" and moved on. The target created the motivation to dig deeper, profile, and find the real bottlenecks.
Use the right tools. Go's pprof toolchain — go test -cpuprofile followed by go tool pprof -top — provided the precise diagnostic data needed to identify the MD5 bottleneck. Without it, the assistant would have been flying blind.
Iterate with rapid feedback. The LSP diagnostics caught syntax errors immediately, preventing the assistant from running invalid code. The read-edit-verify loop — read the file, apply the edit, check for errors — is the same disciplined approach a human developer would use.
Conclusion
The optimization of the loadtest data generator was not a single breakthrough but a pipeline of discoveries, each building on the last. The user's initial specification identified the right targets (buffer pooling, shard-based generation, benchmarks). The assistant implemented them correctly. But it was the user's insistence on profiling — on demanding to know why 700 MB/s was "extremely slow" — that unlocked the real performance gains.
The MD5 bottleneck was hiding in plain sight, consuming half the CPU time in a benchmark that was supposed to measure data generation. It was not a bug — it was a design choice that had not been evaluated for its performance impact. The allocation overhead, at 15% of CPU time, was a secondary cost that had been accepted as unavoidable. Both were eliminated through targeted refactoring: separate the concerns, reuse the buffers, measure each path independently.
The result — a data generator that could sustain 50–85 GB/s per core — was more than sufficient for the load testing needs of the distributed S3 architecture. More importantly, the process established a methodology for future optimization work: profile first, target the real bottleneck, verify with isolated benchmarks, and never accept "good enough" without understanding what "good" actually means.