blob: ee3e3af033d24cad129c6caac9332f156a8ab068 [file] [log] [blame]
Markus Handellf70fbc82020-06-04 00:41:20 +02001/*
2 * Copyright 2020 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include "benchmark/benchmark.h"
12#include "rtc_base/synchronization/mutex.h"
13
14namespace webrtc {
15
16class PerfTestData {
17 public:
18 PerfTestData() : cache_line_barrier_1_(), cache_line_barrier_2_() {
19 cache_line_barrier_1_[0]++; // Avoid 'is not used'.
20 cache_line_barrier_2_[0]++; // Avoid 'is not used'.
21 }
22
23 int AddToCounter(int add) {
24 MutexLock mu(&mu_);
25 my_counter_ += add;
26 return 0;
27 }
28
29 private:
30 uint8_t cache_line_barrier_1_[64];
31 Mutex mu_;
32 uint8_t cache_line_barrier_2_[64];
33 int64_t my_counter_ = 0;
34};
35
36// TODO(bugs.webrtc.org/11630): remove NOLINT when linter supports mutable
37// references.
38void BM_LockWithMutex(benchmark::State& state) { // NOLINT
39 static PerfTestData test_data;
40 for (auto s : state) {
41 benchmark::DoNotOptimize(test_data.AddToCounter(2));
42 }
43}
44
45BENCHMARK(BM_LockWithMutex)->Threads(1);
46BENCHMARK(BM_LockWithMutex)->Threads(2);
47BENCHMARK(BM_LockWithMutex)->Threads(4);
48BENCHMARK(BM_LockWithMutex)->ThreadPerCpu();
49
50} // namespace webrtc
51
52/*
53
54Results:
55
56NB when reproducing: Remember to turn of power management features such as CPU
57scaling before running!
58
59pthreads (Linux):
60----------------------------------------------------------------------
61Run on (12 X 4500 MHz CPU s)
62CPU Caches:
63 L1 Data 32 KiB (x6)
64 L1 Instruction 32 KiB (x6)
65 L2 Unified 1024 KiB (x6)
66 L3 Unified 8448 KiB (x1)
67Load Average: 0.26, 0.28, 0.44
68----------------------------------------------------------------------
69Benchmark Time CPU Iterations
70----------------------------------------------------------------------
71BM_LockWithMutex/threads:1 13.4 ns 13.4 ns 52192906
72BM_LockWithMutex/threads:2 44.2 ns 88.4 ns 8189944
73BM_LockWithMutex/threads:4 52.0 ns 198 ns 3743244
74BM_LockWithMutex/threads:12 84.9 ns 944 ns 733524
75
76std::mutex performs like the pthread implementation (Linux).
77
78Abseil (Linux):
79----------------------------------------------------------------------
80Run on (12 X 4500 MHz CPU s)
81CPU Caches:
82 L1 Data 32 KiB (x6)
83 L1 Instruction 32 KiB (x6)
84 L2 Unified 1024 KiB (x6)
85 L3 Unified 8448 KiB (x1)
86Load Average: 0.27, 0.24, 0.37
87----------------------------------------------------------------------
88Benchmark Time CPU Iterations
89----------------------------------------------------------------------
90BM_LockWithMutex/threads:1 15.0 ns 15.0 ns 46550231
91BM_LockWithMutex/threads:2 91.1 ns 182 ns 4059212
92BM_LockWithMutex/threads:4 40.8 ns 131 ns 5496560
93BM_LockWithMutex/threads:12 37.0 ns 130 ns 5377668
94
95*/