forked from dacap/observable
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathobs_benchmarks.cpp
94 lines (83 loc) · 2.16 KB
/
obs_benchmarks.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Observable Library
// Copyright (c) 2018 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#include "obs.h"
#include <benchmark/benchmark.h>
#include <atomic>
#include <condition_variable>
#include <future>
#include <thread>
#include <vector>
static void BM_ObsCreation(benchmark::State& state) {
for (auto _ : state) {
state.PauseTiming();
{
obs::signal<void()> sig;
state.ResumeTiming();
}
}
}
BENCHMARK(BM_ObsCreation);
static void BM_ObsConnect(benchmark::State& state) {
obs::signal<void()> sig;
for (auto _ : state)
sig.connect([]{ });
}
BENCHMARK(BM_ObsConnect);
static void BM_ObsDisconnect(benchmark::State& state) {
obs::signal<void()> sig;
for (auto _ : state) {
state.PauseTiming();
obs::connection c = sig.connect([]{ });
state.ResumeTiming();
c.disconnect();
}
}
BENCHMARK(BM_ObsDisconnect);
static void BM_ObsSignal(benchmark::State& state) {
obs::signal<void()> sig;
std::vector<obs::scoped_connection> conns(state.range(0));
for (auto& c : conns)
c = sig.connect([]{ });
for (auto _ : state) {
sig();
}
}
BENCHMARK(BM_ObsSignal)->Range(1, 1024);
static void BM_ObsThreads(benchmark::State& state) {
obs::signal<void()> sig;
for (auto _ : state) {
state.PauseTiming();
std::vector<std::thread> threads;
std::atomic<int> count = { 0 };
for (int i=0; i<state.range(0); ++i) {
threads.emplace_back(
[&sig, &count]{
std::mutex m;
std::unique_lock<std::mutex> l(m);
std::condition_variable cv;
obs::scoped_connection c =
sig.connect(
[&m, &cv]{
std::unique_lock<std::mutex> l(m);
cv.notify_one();
});
++count;
cv.wait(l);
});
}
// Wait that all threads are created and waiting for the signal.
while (count < state.range(0))
std::this_thread::yield();
state.ResumeTiming();
sig();
state.PauseTiming();
for (auto& t : threads)
t.join();
state.ResumeTiming();
}
}
BENCHMARK(BM_ObsThreads)->Range(1, 1024);
BENCHMARK_MAIN();