Skip to content

Commit

Permalink
Add non-trivial setting benchmark
Browse files Browse the repository at this point in the history
Summary: The next diff in the stack modifies the setting access hot path for trivial/non-trivial setting types. This diff ensures we have benchmarks for both

Reviewed By: yfeldblum

Differential Revision: D64331519

fbshipit-source-id: 01739b32f635a1a6a556355d5e62bf85021db73b
  • Loading branch information
Kevin Doherty authored and facebook-github-bot committed Oct 18, 2024
1 parent b6bce67 commit 02fb5ed
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 8 deletions.
2 changes: 2 additions & 0 deletions folly/settings/test/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ cpp_benchmark(
srcs = ["SettingsBenchmarks.cpp"],
deps = [
"//folly:benchmark",
"//folly:c_portability",
"//folly/init:init",
"//folly/settings:settings",
"//folly/synchronization/test:barrier",
],
)

Expand Down
94 changes: 86 additions & 8 deletions folly/settings/test/SettingsBenchmarks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,113 @@
* limitations under the License.
*/

#include <thread>
#include <vector>

#include <folly/Benchmark.h>
#include <folly/CPortability.h>
#include <folly/init/Init.h>
#include <folly/settings/Settings.h>
#include <folly/synchronization/test/Barrier.h>

/*
buck run @mode/opt folly/settings/test:settings_bench -- --bm_min_iters=10000000
============================================================================
folly/settings/test/SettingsBenchmarks.cpprelative time/iter
iters/s
============================================================================
settings_get_bench 1.73ns 577.36M
[...]/settings/test/SettingsBenchmarks.cpp relative time/iter iters/s
============================================================================
trivial_access 380.90fs 2.63T
non_trivial_access 1.01ns 987.97M
----------------------------------------------------------------------------
trival_access_parallel(1thr) 138.92ps 7.20G
trival_access_parallel(8thr) 311.42ps 3.21G
trival_access_parallel(24thr) 451.58ps 2.21G
trival_access_parallel(48thr) 793.19ps 1.26G
trival_access_parallel(72thr) 749.04ps 1.34G
----------------------------------------------------------------------------
non_trival_access_parallel(1thr) 1.22ns 819.36M
non_trival_access_parallel(8thr) 1.73ns 577.11M
non_trival_access_parallel(24thr) 2.05ns 488.47M
non_trival_access_parallel(48thr) 3.20ns 312.18M
non_trival_access_parallel(72thr) 3.73ns 267.76M
*/

FOLLY_SETTING_DEFINE(
follytest,
benchmarked,
trivial,
int,
100,
folly::settings::Mutability::Mutable,
folly::settings::CommandLine::AcceptOverrides,
"desc");

BENCHMARK(settings_get_bench, iters) {
FOLLY_SETTING_DEFINE(
follytest,
non_trivial,
std::string,
"default",
folly::settings::Mutability::Mutable,
folly::settings::CommandLine::AcceptOverrides,
"desc");

BENCHMARK(trivial_access, iters) {
for (unsigned int i = 0; i < iters; ++i) {
auto value = *FOLLY_SETTING(follytest, benchmarked);
folly::doNotOptimizeAway(value);
folly::doNotOptimizeAway(*FOLLY_SETTING(follytest, trivial));
}
}

BENCHMARK(non_trivial_access, iters) {
for (unsigned int i = 0; i < iters; ++i) {
folly::doNotOptimizeAway(*FOLLY_SETTING(follytest, non_trivial));
}
}

template <typename Func>
void parallel(size_t numThreads, const Func& func) {
folly::BenchmarkSuspender suspender;
std::vector<std::thread> threads;
folly::test::Barrier barrier(numThreads + 1);
for (size_t i = 0; i < numThreads; ++i) {
threads.emplace_back([&]() {
barrier.wait(); // A
func();
barrier.wait(); // B
});
}
barrier.wait(); // A
suspender.dismissing([&] {
barrier.wait(); // B
});
for (auto& thread : threads) {
thread.join();
}
}

FOLLY_NOINLINE void trival_access_parallel(size_t iters, size_t nThreads) {
parallel(nThreads, [&] {
for (size_t i = 0; i < iters; ++i) {
folly::doNotOptimizeAway(*FOLLY_SETTING(follytest, trivial));
}
});
}
FOLLY_NOINLINE void non_trival_access_parallel(size_t iters, size_t nThreads) {
parallel(nThreads, [&] {
for (size_t i = 0; i < iters; ++i) {
folly::doNotOptimizeAway(*FOLLY_SETTING(follytest, non_trivial));
}
});
}

#define BENCH_PARALLEL(func) \
BENCHMARK_DRAW_LINE(); \
BENCHMARK_NAMED_PARAM(func, 1thr, 1) \
BENCHMARK_NAMED_PARAM(func, 8thr, 8) \
BENCHMARK_NAMED_PARAM(func, 24thr, 24) \
BENCHMARK_NAMED_PARAM(func, 48thr, 48) \
BENCHMARK_NAMED_PARAM(func, 72thr, 72)

BENCH_PARALLEL(trival_access_parallel)
BENCH_PARALLEL(non_trival_access_parallel)

int main(int argc, char** argv) {
folly::Init init(&argc, &argv);
folly::runBenchmarks();
Expand Down

0 comments on commit 02fb5ed

Please sign in to comment.