From adcaae433fe10da72bc4f8b61eaf559604b81d03 Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Wed, 20 Sep 2023 09:40:54 -0700 Subject: absl: add Mutex::[Reader]TryLock benchmark PiperOrigin-RevId: 566991965 Change-Id: I6c4d64de79d303e69b18330bda04fdc84d40893d --- absl/synchronization/mutex_benchmark.cc | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'absl/synchronization') diff --git a/absl/synchronization/mutex_benchmark.cc b/absl/synchronization/mutex_benchmark.cc index 0af56d9e..c3f54764 100644 --- a/absl/synchronization/mutex_benchmark.cc +++ b/absl/synchronization/mutex_benchmark.cc @@ -42,6 +42,26 @@ void BM_ReaderLock(benchmark::State& state) { } BENCHMARK(BM_ReaderLock)->UseRealTime()->Threads(1)->ThreadPerCpu(); +void BM_TryLock(benchmark::State& state) { + absl::Mutex mu; + for (auto _ : state) { + if (mu.TryLock()) { + mu.Unlock(); + } + } +} +BENCHMARK(BM_TryLock); + +void BM_ReaderTryLock(benchmark::State& state) { + static absl::Mutex* mu = new absl::Mutex; + for (auto _ : state) { + if (mu->ReaderTryLock()) { + mu->ReaderUnlock(); + } + } +} +BENCHMARK(BM_ReaderTryLock)->UseRealTime()->Threads(1)->ThreadPerCpu(); + static void DelayNs(int64_t ns, int* data) { int64_t end = absl::base_internal::CycleClock::Now() + ns * absl::base_internal::CycleClock::Frequency() / 1e9; -- cgit v1.2.3