Skip to content

Commit

Permalink
Refactor tasks architecture
Browse files Browse the repository at this point in the history
  • Loading branch information
kmichaelk committed Nov 9, 2024
1 parent 0cfd881 commit 2a5a27a
Show file tree
Hide file tree
Showing 10 changed files with 315 additions and 431 deletions.
28 changes: 13 additions & 15 deletions modules/core/perf/include/perf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,43 @@

#include "core/task/include/task.hpp"

namespace ppc {
namespace core {
namespace ppc::core {

struct PerfAttr {
// count of task's running
uint64_t num_running;
size_t num_running;
std::function<double(void)> current_timer = [&] { return 0.0; };
};

struct PerfResults {
// measurement of task's time (in seconds)
double time_sec = 0.0;
enum TypeOfRunning { PIPELINE, TASK_RUN, NONE } type_of_running = NONE;
enum class RunType : std::uint8_t { Pipeline, TaskRun, None } run_type = RunType::None;

constexpr const static double MAX_TIME = 10.0;
};

class Perf {
public:
// Init performance analysis with initialized task and initialized data
explicit Perf(std::shared_ptr<Task> task_);
explicit Perf(BaseTask& task_);
// Set task with initialized task and initialized data for performance
// analysis c
void set_task(std::shared_ptr<Task> task_);
void set_task(BaseTask& task_);
// Check performance of full task's pipeline: pre_processing() ->
// validation() -> run() -> post_processing()
void pipeline_run(const std::shared_ptr<PerfAttr>& perfAttr,
const std::shared_ptr<ppc::core::PerfResults>& perfResults);
void pipeline_run(PerfAttr& perfAttr, ppc::core::PerfResults& perfResults);
// Check performance of task's run() function
void task_run(const std::shared_ptr<PerfAttr>& perfAttr, const std::shared_ptr<ppc::core::PerfResults>& perfResults);
void task_run(PerfAttr& perfAttr, ppc::core::PerfResults& perfResults);
// Pint results for automation checkers
static void print_perf_statistic(const std::shared_ptr<PerfResults>& perfResults);
static void print_perf_statistic(const PerfResults& perfResults);

private:
std::shared_ptr<Task> task;
static void common_run(const std::shared_ptr<PerfAttr>& perfAttr, const std::function<void()>& pipeline,
const std::shared_ptr<ppc::core::PerfResults>& perfResults);
BaseTask* task{nullptr};
static void common_run(PerfAttr& perfAttr, const std::function<void()>& pipeline,
ppc::core::PerfResults& perfResults);
};

} // namespace core
} // namespace ppc
} // namespace ppc::core

#endif // MODULES_CORE_INCLUDE_PERF_HPP_
94 changes: 50 additions & 44 deletions modules/core/perf/src/perf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,78 +2,84 @@

#include <gtest/gtest.h>

#include <cassert>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <utility>

ppc::core::Perf::Perf(std::shared_ptr<Task> task_) { set_task(std::move(task_)); }
ppc::core::Perf::Perf(BaseTask& task_) { set_task(task_); }

void ppc::core::Perf::set_task(std::shared_ptr<Task> task_) {
task_->get_data()->state_of_testing = TaskData::StateOfTesting::PERF;
task = std::move(task_);
void ppc::core::Perf::set_task(BaseTask& task_) {
task_.testing_mode = BaseTask::TestingMode::Perf;
task = &task_;
}

void ppc::core::Perf::pipeline_run(const std::shared_ptr<PerfAttr>& perfAttr,
const std::shared_ptr<ppc::core::PerfResults>& perfResults) {
perfResults->type_of_running = PerfResults::TypeOfRunning::PIPELINE;
void ppc::core::Perf::pipeline_run(PerfAttr& perfAttr, ppc::core::PerfResults& perfResults) {
perfResults.run_type = PerfResults::RunType::Pipeline;

common_run(
std::move(perfAttr),
perfAttr,
[&]() {
task->validation();
task->pre_processing();
task->validate();
task->pre_process();
task->run();
task->post_processing();
task->post_process();
},
std::move(perfResults));
perfResults);
}

void ppc::core::Perf::task_run(const std::shared_ptr<PerfAttr>& perfAttr,
const std::shared_ptr<ppc::core::PerfResults>& perfResults) {
perfResults->type_of_running = PerfResults::TypeOfRunning::TASK_RUN;
void ppc::core::Perf::task_run(PerfAttr& perfAttr, ppc::core::PerfResults& perfResults) {
perfResults.run_type = PerfResults::RunType::TaskRun;

task->validation();
task->pre_processing();
common_run(std::move(perfAttr), [&]() { task->run(); }, std::move(perfResults));
task->post_processing();
task->validate();
task->pre_process();
common_run(
perfAttr, [&]() { task->run(); }, perfResults);
task->post_process();

task->validation();
task->pre_processing();
task->validate();
task->pre_process();
task->run();
task->post_processing();
task->post_process();
}

void ppc::core::Perf::common_run(const std::shared_ptr<PerfAttr>& perfAttr, const std::function<void()>& pipeline,
const std::shared_ptr<ppc::core::PerfResults>& perfResults) {
auto begin = perfAttr->current_timer();
for (uint64_t i = 0; i < perfAttr->num_running; i++) {
void ppc::core::Perf::common_run(PerfAttr& perfAttr, const std::function<void()>& pipeline,
ppc::core::PerfResults& perfResults) {
const auto begin = perfAttr.current_timer();
for (size_t i = 0; i < perfAttr.num_running; i++) {
pipeline();
}
auto end = perfAttr->current_timer();
perfResults->time_sec = end - begin;
const auto end = perfAttr.current_timer();
perfResults.time_sec = end - begin;
}

void ppc::core::Perf::print_perf_statistic(const std::shared_ptr<PerfResults>& perfResults) {
void ppc::core::Perf::print_perf_statistic(const PerfResults& perfResults) {
std::string relative_path(::testing::UnitTest::GetInstance()->current_test_info()->file());
std::string ppc_regex_template("parallel_programming_course");
std::string perf_regex_template("perf_tests");
std::string type_test_name;

auto time_secs = perfResults->time_sec;

if (perfResults->type_of_running == PerfResults::TypeOfRunning::TASK_RUN) {
type_test_name = "task_run";
} else if (perfResults->type_of_running == PerfResults::TypeOfRunning::PIPELINE) {
type_test_name = "pipeline";
} else if (perfResults->type_of_running == PerfResults::TypeOfRunning::NONE) {
type_test_name = "none";
auto time_secs = perfResults.time_sec;

switch (perfResults.run_type) {
case PerfResults::RunType::TaskRun:
type_test_name = "task_run";
break;
case PerfResults::RunType::Pipeline:
type_test_name = "pipeline";
break;
case PerfResults::RunType::None:
type_test_name = "none";
break;
default:
assert(false);
break;
}

auto first_found_position = relative_path.find(ppc_regex_template) + ppc_regex_template.length() + 1;
const auto first_found_position = relative_path.find(ppc_regex_template) + ppc_regex_template.length() + 1;
relative_path.erase(0, first_found_position);

auto last_found_position = relative_path.find(perf_regex_template) - 1;
const auto last_found_position = relative_path.find(perf_regex_template) - 1;
relative_path.erase(last_found_position, relative_path.length() - 1);

std::stringstream perf_res_str;
Expand All @@ -82,11 +88,11 @@ void ppc::core::Perf::print_perf_statistic(const std::shared_ptr<PerfResults>& p
std::cout << relative_path << ":" << type_test_name << ":" << perf_res_str.str() << std::endl;
} else {
std::stringstream errMsg;
errMsg << std::endl << "Task execute time need to be: ";
errMsg << "time < " << PerfResults::MAX_TIME << " secs." << std::endl;
errMsg << "Original time in secs: " << time_secs << std::endl;
errMsg << "\nTask execute time need to be: ";
errMsg << "time < " << PerfResults::MAX_TIME << " secs.\n";
errMsg << "Original time in secs: " << time_secs << '\n';
perf_res_str << std::fixed << std::setprecision(10) << -1.0;
std::cout << relative_path << ":" << type_test_name << ":" << perf_res_str.str() << std::endl;
throw std::runtime_error(errMsg.str().c_str());
throw std::runtime_error(errMsg.str());
}
}
Loading

0 comments on commit 2a5a27a

Please sign in to comment.