mirror of
https://github.com/gelldur/EventBus.git
synced 2024-12-27 12:21:02 +08:00
Add sample performance test
This commit is contained in:
parent
be3d01c717
commit
919bc90579
1
.gitignore
vendored
1
.gitignore
vendored
@ -28,3 +28,4 @@
|
||||
*.app
|
||||
|
||||
build/
|
||||
cmake-build*/
|
||||
|
@ -44,3 +44,4 @@ EXPORT(TARGETS EventBus FILE EventBusConfig.cmake)
|
||||
|
||||
ENABLE_TESTING()
|
||||
ADD_SUBDIRECTORY(test/)
|
||||
ADD_SUBDIRECTORY(performance/)
|
||||
|
10
performance/CMakeLists.txt
Normal file
10
performance/CMakeLists.txt
Normal file
@ -0,0 +1,10 @@
|
||||
# http://www.levelofindirection.com/journal/2010/12/28/unit-testing-in-c-and-objective-c-just-got-easier.html
|
||||
# Thanks for CATCH!
|
||||
|
||||
ADD_SUBDIRECTORY(benchmark/)
|
||||
|
||||
ADD_EXECUTABLE(EventBusPerformance
|
||||
eventbus/EventBusPerformance.cpp
|
||||
)
|
||||
|
||||
TARGET_LINK_LIBRARIES(EventBusPerformance PUBLIC Dexode::EventBus benchmark)
|
31
performance/README.md
Normal file
31
performance/README.md
Normal file
@ -0,0 +1,31 @@
|
||||
# Performace
|
||||
|
||||
This is maybe not perfect but can show something. All result are run using RELESE build.
|
||||
Why?
|
||||
|
||||
Sample DEBUG run:
|
||||
```commandline
|
||||
Run on (8 X 3600 MHz CPU s)
|
||||
2017-08-05 12:44:53
|
||||
***WARNING*** Library was built as DEBUG. Timings may be affected.
|
||||
---------------------------------------------------------------
|
||||
Benchmark Time CPU Iterations
|
||||
---------------------------------------------------------------
|
||||
checkSimpleNotification 293 ns 293 ns 2319171
|
||||
```
|
||||
|
||||
Sample RELEASE run:
|
||||
```commandline
|
||||
Run on (8 X 3600 MHz CPU s)
|
||||
2017-08-05 12:45:43
|
||||
---------------------------------------------------------------
|
||||
Benchmark Time CPU Iterations
|
||||
---------------------------------------------------------------
|
||||
checkSimpleNotification 6 ns 6 ns 116492914
|
||||
```
|
||||
|
||||
So below all numbers are in release.
|
||||
|
||||
This library as you can read in main README.md was inspured by [CCNotificationCenter](https://github.com/cocos2d/cocos2d-x/blob/v2/cocos2dx/support/CCNotificationCenter.h) from cocos2d-x game engine.
|
||||
So i want to present comparision of performance of this two. Of course this is only showcase.
|
||||
|
129
performance/eventbus/EventBusPerformance.cpp
Normal file
129
performance/eventbus/EventBusPerformance.cpp
Normal file
@ -0,0 +1,129 @@
|
||||
//
|
||||
// Created by Dawid Drozd aka Gelldur on 05.08.17.
|
||||
//
|
||||
#include <random>
|
||||
|
||||
#include <benchmark/benchmark.h>
|
||||
#include <eventbus/Notifier.h>
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
MAKE_NOTIFICATION(SimpleNotification, int);
|
||||
|
||||
void checkSimpleNotification(benchmark::State& state)
|
||||
{
|
||||
Dexode::Notifier bus;
|
||||
int sum = 0;
|
||||
bus.listen(getNotificationSimpleNotification(), [&](int value)
|
||||
{
|
||||
sum += value * 2;
|
||||
});
|
||||
|
||||
while (state.KeepRunning())
|
||||
{
|
||||
bus.notify(getNotificationSimpleNotification(), 2);
|
||||
}
|
||||
}
|
||||
|
||||
void check100Listeners(benchmark::State& state)
|
||||
{
|
||||
Dexode::Notifier bus;
|
||||
int sum = 0;
|
||||
for (int i = 0; i < 100; ++i)
|
||||
{
|
||||
bus.listen(getNotificationSimpleNotification(), [&](int value)
|
||||
{
|
||||
sum += value * 2;
|
||||
});
|
||||
}
|
||||
|
||||
while (state.KeepRunning())
|
||||
{
|
||||
bus.notify(getNotificationSimpleNotification(), 2);
|
||||
}
|
||||
}
|
||||
|
||||
void check1kListeners(benchmark::State& state)
|
||||
{
|
||||
Dexode::Notifier bus;
|
||||
int sum = 0;
|
||||
for (int i = 0; i < 1000; ++i)
|
||||
{
|
||||
bus.listen(getNotificationSimpleNotification(), [&](int value)
|
||||
{
|
||||
sum += value * 2;
|
||||
});
|
||||
}
|
||||
|
||||
while (state.KeepRunning())
|
||||
{
|
||||
bus.notify(getNotificationSimpleNotification(), 2);
|
||||
}
|
||||
}
|
||||
|
||||
void checkNNotificationsForNListeners(benchmark::State& state, const int notificationsCount, const int listenersCount)
|
||||
{
|
||||
std::mt19937 generator(311281);
|
||||
std::uniform_int_distribution<int> uniformDistribution(0, notificationsCount - 1);
|
||||
|
||||
//We generate here N different notifications
|
||||
std::vector<Dexode::Notification<int>> notifications;
|
||||
notifications.reserve(notificationsCount);
|
||||
for (int i = 0; i < notificationsCount; ++i)
|
||||
{
|
||||
notifications.emplace_back(691283);
|
||||
}
|
||||
|
||||
Dexode::Notifier bus;
|
||||
int sum = 0;
|
||||
//We register 1k listeners for N notifications using uniform distribution
|
||||
for (int i = 0; i < listenersCount; ++i)
|
||||
{
|
||||
const auto& notification = notifications.at(uniformDistribution(generator));
|
||||
bus.listen(notification, [&](int value)
|
||||
{
|
||||
benchmark::DoNotOptimize(sum += value * 2);//we use it to prevent some? optimizations
|
||||
});
|
||||
}
|
||||
|
||||
//Performance area!
|
||||
while (state.KeepRunning())
|
||||
{
|
||||
//Pick random notification
|
||||
const auto& notification = notifications.at(uniformDistribution(generator));
|
||||
bus.notify(notification, uniformDistribution(generator));
|
||||
}
|
||||
}
|
||||
|
||||
void check10NotificationsFor1kListeners(benchmark::State& state)
|
||||
{
|
||||
checkNNotificationsForNListeners(state, 10, 1000);
|
||||
}
|
||||
|
||||
void check100NotificationsFor1kListeners(benchmark::State& state)
|
||||
{
|
||||
checkNNotificationsForNListeners(state, 100, 1000);
|
||||
}
|
||||
|
||||
void check1kNotificationsFor1kListeners(benchmark::State& state)
|
||||
{
|
||||
checkNNotificationsForNListeners(state, 1000, 1000);
|
||||
}
|
||||
|
||||
void check100NotificationsFor10kListeners(benchmark::State& state)
|
||||
{
|
||||
checkNNotificationsForNListeners(state, 100, 10000);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
BENCHMARK(checkSimpleNotification);
|
||||
BENCHMARK(check100Listeners);
|
||||
BENCHMARK(check1kListeners);
|
||||
BENCHMARK(check10NotificationsFor1kListeners);
|
||||
BENCHMARK(check100NotificationsFor1kListeners);
|
||||
BENCHMARK(check1kNotificationsFor1kListeners);
|
||||
BENCHMARK(check100NotificationsFor10kListeners);
|
||||
|
||||
BENCHMARK_MAIN();
|
Loading…
x
Reference in New Issue
Block a user