2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-01-01 01:35:39 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#include "td/actor/actor.h"
|
2020-11-23 01:24:36 +01:00
|
|
|
#include "td/actor/ConcurrentScheduler.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-10-21 11:51:16 +02:00
|
|
|
#include "td/utils/common.h"
|
2021-05-17 14:21:11 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2021-09-18 23:47:05 +02:00
|
|
|
#include "td/utils/tests.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-10-15 13:21:30 +02:00
|
|
|
class PowerWorker final : public td::Actor {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
class Callback {
|
|
|
|
public:
|
|
|
|
Callback() = default;
|
|
|
|
Callback(const Callback &) = delete;
|
|
|
|
Callback &operator=(const Callback &) = delete;
|
|
|
|
Callback(Callback &&) = delete;
|
|
|
|
Callback &operator=(Callback &&) = delete;
|
|
|
|
virtual ~Callback() = default;
|
|
|
|
virtual void on_ready(int query, int res) = 0;
|
|
|
|
virtual void on_closed() = 0;
|
|
|
|
};
|
2021-10-15 13:21:30 +02:00
|
|
|
void set_callback(td::unique_ptr<Callback> callback) {
|
2018-12-31 20:04:05 +01:00
|
|
|
callback_ = std::move(callback);
|
|
|
|
}
|
2021-10-15 13:21:30 +02:00
|
|
|
void task(td::uint32 x, td::uint32 p) {
|
|
|
|
td::uint32 res = 1;
|
|
|
|
for (td::uint32 i = 0; i < p; i++) {
|
2018-12-31 20:04:05 +01:00
|
|
|
res *= x;
|
|
|
|
}
|
|
|
|
callback_->on_ready(x, res);
|
|
|
|
}
|
|
|
|
void close() {
|
|
|
|
callback_->on_closed();
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-10-15 13:21:30 +02:00
|
|
|
td::unique_ptr<Callback> callback_;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
2021-10-15 13:21:30 +02:00
|
|
|
class Manager final : public td::Actor {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2021-10-15 13:21:30 +02:00
|
|
|
Manager(int queries_n, int query_size, td::vector<td::ActorId<PowerWorker>> workers)
|
2018-10-26 16:11:20 +02:00
|
|
|
: workers_(std::move(workers))
|
|
|
|
, ref_cnt_(static_cast<int>(workers_.size()))
|
|
|
|
, left_query_(queries_n)
|
|
|
|
, query_size_(query_size) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class Callback final : public PowerWorker::Callback {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2021-10-15 13:21:30 +02:00
|
|
|
Callback(td::ActorId<Manager> actor_id, int worker_id) : actor_id_(actor_id), worker_id_(worker_id) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_ready(int query, int result) final {
|
2021-10-15 13:21:30 +02:00
|
|
|
td::send_closure(actor_id_, &Manager::on_ready, worker_id_, query, result);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_closed() final {
|
2021-10-15 13:21:30 +02:00
|
|
|
td::send_closure_later(actor_id_, &Manager::on_closed, worker_id_);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-10-15 13:21:30 +02:00
|
|
|
td::ActorId<Manager> actor_id_;
|
2018-12-31 20:04:05 +01:00
|
|
|
int worker_id_;
|
|
|
|
};
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
int i = 0;
|
|
|
|
for (auto &worker : workers_) {
|
|
|
|
ref_cnt_++;
|
2021-10-15 13:21:30 +02:00
|
|
|
td::send_closure_later(worker, &PowerWorker::set_callback, td::make_unique<Callback>(actor_id(this), i));
|
2018-12-31 20:04:05 +01:00
|
|
|
i++;
|
2021-10-15 13:21:30 +02:00
|
|
|
td::send_closure_later(worker, &PowerWorker::task, 3, query_size_);
|
2018-12-31 20:04:05 +01:00
|
|
|
left_query_--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_ready(int worker_id, int query, int res) {
|
|
|
|
ref_cnt_--;
|
|
|
|
if (left_query_ == 0) {
|
2021-10-15 13:21:30 +02:00
|
|
|
td::send_closure(workers_[worker_id], &PowerWorker::close);
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
|
|
|
ref_cnt_++;
|
2021-10-15 13:21:30 +02:00
|
|
|
td::send_closure(workers_[worker_id], &PowerWorker::task, 3, query_size_);
|
2018-12-31 20:04:05 +01:00
|
|
|
left_query_--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_closed(int worker_id) {
|
|
|
|
ref_cnt_--;
|
|
|
|
if (ref_cnt_ == 0) {
|
2021-10-15 13:21:30 +02:00
|
|
|
td::Scheduler::instance()->finish();
|
2018-12-31 20:04:05 +01:00
|
|
|
stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-10-15 13:21:30 +02:00
|
|
|
td::vector<td::ActorId<PowerWorker>> workers_;
|
2018-12-31 20:04:05 +01:00
|
|
|
int ref_cnt_;
|
2018-10-26 16:11:20 +02:00
|
|
|
int left_query_;
|
2018-12-31 20:04:05 +01:00
|
|
|
int query_size_;
|
|
|
|
};
|
|
|
|
|
2020-11-21 18:45:09 +01:00
|
|
|
static void test_workers(int threads_n, int workers_n, int queries_n, int query_size) {
|
2022-09-14 14:06:52 +02:00
|
|
|
td::ConcurrentScheduler sched(threads_n, 0);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-10-15 13:21:30 +02:00
|
|
|
td::vector<td::ActorId<PowerWorker>> workers;
|
2018-12-31 20:04:05 +01:00
|
|
|
for (int i = 0; i < workers_n; i++) {
|
|
|
|
int thread_id = threads_n ? i % (threads_n - 1) + 2 : 0;
|
2018-07-08 01:47:46 +02:00
|
|
|
workers.push_back(sched.create_actor_unsafe<PowerWorker>(thread_id, PSLICE() << "worker" << i).release());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-10-26 16:11:20 +02:00
|
|
|
sched.create_actor_unsafe<Manager>(threads_n ? 1 : 0, "Manager", queries_n, query_size, std::move(workers)).release();
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
sched.start();
|
|
|
|
while (sched.run_main(10)) {
|
|
|
|
// empty
|
|
|
|
}
|
|
|
|
sched.finish();
|
|
|
|
|
|
|
|
// sched.test_one_thread_run();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Actors, workers_big_query_one_thread) {
|
|
|
|
test_workers(0, 10, 1000, 300000);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Actors, workers_big_query_two_threads) {
|
|
|
|
test_workers(2, 10, 1000, 300000);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Actors, workers_big_query_nine_threads) {
|
|
|
|
test_workers(9, 10, 1000, 300000);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Actors, workers_small_query_one_thread) {
|
2022-07-27 23:15:18 +02:00
|
|
|
test_workers(0, 10, 100000, 1);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Actors, workers_small_query_two_threads) {
|
2022-07-27 23:15:18 +02:00
|
|
|
test_workers(2, 10, 100000, 1);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Actors, workers_small_query_nine_threads) {
|
2022-07-27 23:15:18 +02:00
|
|
|
test_workers(9, 10, 10000, 1);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|