Skip to content

Commit 76a403c

Browse files
andreas-abelcopybara-github
authored andcommitted
Reduce mem_benchmark variance
PiperOrigin-RevId: 604697627 Change-Id: Iddb78e44eb1731d987dfebb6f9b3879d98fcf407
1 parent 1e756fb commit 76a403c

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

fleetbench/libc/mem_benchmark.cc

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -221,10 +221,7 @@ static void BM_Memory(benchmark::State &state,
221221
// Pre-calculates parameter values.
222222
std::vector<BM_Mem_Parameters> parameters(batch_size);
223223

224-
// Convert prod size distribution to a discrete distribution.
225-
std::discrete_distribution<uint16_t> size_bytes_sampler(
226-
memory_size_distribution.begin(), memory_size_distribution.end());
227-
CHECK_LE(size_bytes_sampler.max(), kMaxSizeBytes)
224+
CHECK_LE(memory_size_distribution.size(), kMaxSizeBytes)
228225
<< "Maximum of the distribution larger than expected";
229226

230227
// Max buffer size can be stored in current cache.
@@ -247,10 +244,17 @@ static void BM_Memory(benchmark::State &state,
247244
// as the call to absl::Uniform may perform a different number of calls to the
248245
// random number generator, depending on the value of offset_upper_bound,
249246
// which depends on the cache size.
250-
for (auto &p : parameters) {
251-
// Size_bytes is sampled from collected prod distribution.
252-
p.size_bytes = size_bytes_sampler(GetRNG());
247+
double percentage_sum = 0.0;
248+
int n_parameters = 0;
249+
for (int i = 0; i < memory_size_distribution.size(); ++i) {
250+
// percentage_sum stores the relative frequency for an input of size <= i
251+
percentage_sum += memory_size_distribution[i];
252+
while (percentage_sum * batch_size - n_parameters > 0.999) {
253+
parameters[n_parameters++].size_bytes = i;
254+
}
253255
}
256+
CHECK_EQ(n_parameters, parameters.size());
257+
std::shuffle(parameters.begin(), parameters.end(), GetRNG());
254258

255259
if (memory_call == &MemmoveFunction || memory_call == &CmpFunction<memcmp> ||
256260
memory_call == &CmpFunction<bcmp>) {

0 commit comments

Comments
 (0)