-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make test_scheduler more smart (#278)
- Loading branch information
1 parent
b7daaf7
commit 189556d
Showing
5 changed files
with
172 additions
and
49 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 |
---|---|---|
@@ -1,58 +1,96 @@ | ||
// ReactivePlusPlus library | ||
// | ||
// | ||
// Copyright Aleksey Loginov 2022 - present. | ||
// Distributed under the Boost Software License, Version 1.0. | ||
// (See accompanying file LICENSE_1_0.txt or copy at | ||
// https://www.boost.org/LICENSE_1_0.txt) | ||
// | ||
// | ||
// Project home: https://github.com/victimsnino/ReactivePlusPlus | ||
|
||
#pragma once | ||
|
||
#include <rpp/schedulers.hpp> | ||
|
||
static rpp::schedulers::time_point s_current_time{ std::chrono::seconds{0} }; | ||
static rpp::schedulers::time_point s_current_time{std::chrono::seconds{0}}; | ||
|
||
class test_scheduler final : public rpp::schedulers::details::scheduler_tag | ||
{ | ||
public: | ||
class worker_strategy | ||
struct state | ||
{ | ||
public: | ||
worker_strategy(const rpp::subscription_base& sub, | ||
std::shared_ptr<std::vector<rpp::schedulers::time_point>> schedulings) | ||
: m_sub{ sub } | ||
, m_schedulings{ schedulings } {} | ||
state() = default; | ||
|
||
void schedule(rpp::schedulers::time_point time_point, | ||
rpp::schedulers::constraint::schedulable_fn auto&& fn) | ||
{ | ||
schedulings.push_back(time_point); | ||
queue.emplace(time_point, | ||
static_cast<size_t>(rpp::schedulers::clock_type::now().time_since_epoch().count()), | ||
std::forward<decltype(fn)>(fn)); | ||
} | ||
|
||
void defer_at(rpp::schedulers::time_point time_point, rpp::schedulers::constraint::schedulable_fn auto&& fn) const | ||
void drain() | ||
{ | ||
while (m_sub.is_subscribed()) | ||
while (!queue.empty() && sub.is_subscribed()) | ||
{ | ||
m_schedulings->push_back(time_point); | ||
auto time_point = queue.top().get_time_point(); | ||
if (time_point > s_current_time) | ||
return; | ||
|
||
auto fn = queue.top().extract_function(); | ||
queue.pop(); | ||
|
||
executions.push_back(s_current_time); | ||
if (auto duration = fn()) | ||
time_point = std::max(now(), time_point + duration.value()); | ||
else | ||
return; | ||
schedule(std::max(s_current_time, time_point + duration.value()), std::move(fn)); | ||
} | ||
} | ||
|
||
rpp::subscription_base sub{}; | ||
std::vector<rpp::schedulers::time_point> schedulings{}; | ||
std::vector<rpp::schedulers::time_point> executions{}; | ||
std::priority_queue<rpp::schedulers::details::schedulable<std::function<rpp::schedulers::optional_duration()>>> queue{}; | ||
}; | ||
|
||
class worker_strategy | ||
{ | ||
public: | ||
worker_strategy(std::shared_ptr<state> state) | ||
: m_state{state} { } | ||
|
||
void defer_at(rpp::schedulers::time_point time_point, | ||
rpp::schedulers::constraint::schedulable_fn auto&& fn) const | ||
{ | ||
if (m_state->sub.is_subscribed()) | ||
{ | ||
m_state->schedule(time_point, std::forward<decltype(fn)>(fn)); | ||
m_state->drain(); | ||
} | ||
} | ||
|
||
static rpp::schedulers::time_point now() { return s_current_time; } | ||
|
||
private: | ||
rpp::subscription_base m_sub; | ||
std::shared_ptr<std::vector<rpp::schedulers::time_point>> m_schedulings; | ||
std::shared_ptr<state> m_state; | ||
}; | ||
|
||
test_scheduler() {} | ||
|
||
rpp::schedulers::worker<worker_strategy> create_worker(const rpp::subscription_base& sub = {}) const | ||
{ | ||
return rpp::schedulers::worker<worker_strategy>{sub, m_schedulings}; | ||
m_state->sub = sub; | ||
return rpp::schedulers::worker<worker_strategy>{m_state}; | ||
} | ||
|
||
const auto& get_schedulings() const { return *m_schedulings; } | ||
const auto& get_schedulings() const { return m_state->schedulings; } | ||
const auto& get_executions() const { return m_state->executions; } | ||
|
||
void time_advance(rpp::schedulers::duration dur) const | ||
{ | ||
s_current_time += dur; | ||
m_state->drain(); | ||
} | ||
|
||
private: | ||
std::shared_ptr<std::vector<rpp::schedulers::time_point>> m_schedulings = std::make_shared<std::vector<rpp::schedulers::time_point>>(); | ||
}; | ||
std::shared_ptr<state> m_state = std::make_shared<state>(); | ||
}; |
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
189556d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BENCHMARK RESULTS (AUTOGENERATED)
ci-ubuntu-clang
Observable construction
Table
Observable lift
Table
Observable subscribe
Table
Observable subscribe #2
Table
Observer construction
Table
OnNext
Table
Subscriber construction
Table
Subscription
Table
buffer
Table
chains creation test
Table
combine_latest
Table
concat
Table
distinct_until_changed
Table
first
Table
foundamental sources
Table
from
Table
immediate scheduler
Table
just
Table
last
Table
map
Table
merge
Table
observe_on
Table
publish_subject callbacks
Table
publish_subject routines
Table
repeat
Table
scan
Table
single-threaded locks
Table
skip
Table
switch_on_next
Table
take
Table
take_last
Table
take_until
Table
trampoline scheduler
Table
window
Table
with_latest_from
Table
ci-ubuntu-gcc
Observable construction
Table
Observable lift
Table
Observable subscribe
Table
Observable subscribe #2
Table
Observer construction
Table
OnNext
Table
Subscriber construction
Table
Subscription
Table
buffer
Table
chains creation test
Table
combine_latest
Table
concat
Table
distinct_until_changed
Table
first
Table
foundamental sources
Table
from
Table
immediate scheduler
Table
just
Table
last
Table
map
Table
merge
Table
observe_on
Table
publish_subject callbacks
Table
publish_subject routines
Table
repeat
Table
scan
Table
single-threaded locks
Table
skip
Table
switch_on_next
Table
take
Table
take_last
Table
take_until
Table
trampoline scheduler
Table
window
Table
with_latest_from
Table
ci-windows
Observable construction
Table
Observable lift
Table
Observable subscribe
Table
Observable subscribe #2
Table
Observer construction
Table
OnNext
Table
Subscriber construction
Table
Subscription
Table
buffer
Table
chains creation test
Table
combine_latest
Table
concat
Table
distinct_until_changed
Table
first
Table
foundamental sources
Table
from
Table
immediate scheduler
Table
just
Table
last
Table
map
Table
merge
Table
observe_on
Table
publish_subject callbacks
Table
publish_subject routines
Table
repeat
Table
scan
Table
single-threaded locks
Table
skip
Table
switch_on_next
Table
take
Table
take_last
Table
take_until
Table
trampoline scheduler
Table
window
Table
with_latest_from
Table