forked from dacap/observable
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
168 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Observable Library | ||
# Copyright (C) 2018 David Capello | ||
|
||
include(ExternalProject) | ||
ExternalProject_Add(googlebenchmark-project | ||
URL https://github.com/google/benchmark/archive/master.zip | ||
PREFIX "${CMAKE_BINARY_DIR}/googlebenchmark" | ||
INSTALL_DIR "${CMAKE_BINARY_DIR}/googlebenchmark" | ||
BUILD_BYPRODUCTS "${CMAKE_BINARY_DIR}/googlebenchmark/lib/${CMAKE_STATIC_LIBRARY_PREFIX}benchmark${CMAKE_STATIC_LIBRARY_SUFFIX}" | ||
CMAKE_CACHE_ARGS | ||
-DBENCHMARK_ENABLE_GTEST_TESTS:BOOL=OFF | ||
-DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE} | ||
-DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR> | ||
-DCMAKE_INSTALL_LIBDIR:PATH=<INSTALL_DIR>/lib) | ||
|
||
ExternalProject_Get_Property(googlebenchmark-project install_dir) | ||
set(GOOGLEBENCHMARK_INCLUDE_DIRS ${install_dir}/include) | ||
set(GOOGLEBENCHMARK_LIBRARY ${install_dir}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}benchmark${CMAKE_STATIC_LIBRARY_SUFFIX}) | ||
|
||
# Create the directory so changing INTERFACE_INCLUDE_DIRECTORIES doesn't fail | ||
file(MAKE_DIRECTORY ${GOOGLEBENCHMARK_INCLUDE_DIRS}) | ||
|
||
add_library(googlebenchmark STATIC IMPORTED) | ||
set_target_properties(googlebenchmark PROPERTIES | ||
IMPORTED_LOCATION ${GOOGLEBENCHMARK_LIBRARY} | ||
INTERFACE_INCLUDE_DIRECTORIES ${GOOGLEBENCHMARK_INCLUDE_DIRS}) | ||
add_dependencies(googlebenchmark googlebenchmark-project) | ||
|
||
add_executable(obs_benchmarks obs_benchmarks.cpp) | ||
target_link_libraries(obs_benchmarks obs googlebenchmark) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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(); |