Skip to content

Commit dca8eff

Browse files
authored
Refactoring for new API (#388)
1 parent e3a0b6e commit dca8eff

File tree

58 files changed

+813
-3664
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+813
-3664
lines changed

modules/core/perf/func_tests/perf_tests.cpp

Lines changed: 42 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -12,166 +12,103 @@
1212
TEST(perf_tests, check_perf_pipeline) {
1313
// Create data
1414
std::vector<uint32_t> in(2000, 1);
15-
std::vector<uint32_t> out(1, 0);
16-
17-
// Create task_data
18-
auto task_data = std::make_shared<ppc::core::TaskData>();
19-
task_data->inputs.emplace_back(reinterpret_cast<uint8_t *>(in.data()));
20-
task_data->inputs_count.emplace_back(in.size());
21-
task_data->outputs.emplace_back(reinterpret_cast<uint8_t *>(out.data()));
22-
task_data->outputs_count.emplace_back(out.size());
2315

2416
// Create Task
25-
auto test_task = std::make_shared<ppc::test::perf::TestTask<uint32_t>>(task_data);
26-
27-
// Create Perf attributes
28-
auto perf_attr = std::make_shared<ppc::core::PerfAttr>();
29-
perf_attr->num_running = 10;
30-
31-
// Create and init perf results
32-
auto perf_results = std::make_shared<ppc::core::PerfResults>();
17+
auto test_task = std::make_shared<ppc::test::perf::TestTask<uint32_t>>(in);
3318

3419
// Create Perf analyzer
3520
ppc::core::Perf perf_analyzer(test_task);
36-
perf_analyzer.PipelineRun(perf_attr, perf_results);
21+
22+
// Create Perf attributes
23+
ppc::core::PerfAttr perf_attr;
24+
perf_analyzer.PipelineRun(perf_attr);
3725

3826
// Get perf statistic
39-
ppc::core::Perf::PrintPerfStatistic(perf_results);
40-
ASSERT_LE(perf_results->time_sec, ppc::core::PerfResults::kMaxTime);
41-
EXPECT_EQ(out[0], in.size());
27+
perf_analyzer.PrintPerfStatistic();
28+
ASSERT_LE(perf_analyzer.GetPerfResults().time_sec, ppc::core::PerfResults::kMaxTime);
29+
EXPECT_EQ(test_task->Get(), in.size());
4230
}
4331

4432
TEST(perf_tests, check_perf_pipeline_float) {
4533
// Create data
4634
std::vector<float> in(2000, 1);
47-
std::vector<float> out(1, 0);
48-
49-
// Create task_data
50-
auto task_data = std::make_shared<ppc::core::TaskData>();
51-
task_data->inputs.emplace_back(reinterpret_cast<uint8_t *>(in.data()));
52-
task_data->inputs_count.emplace_back(in.size());
53-
task_data->outputs.emplace_back(reinterpret_cast<uint8_t *>(out.data()));
54-
task_data->outputs_count.emplace_back(out.size());
5535

5636
// Create Task
57-
auto test_task = std::make_shared<ppc::test::perf::TestTask<float>>(task_data);
58-
59-
// Create Perf attributes
60-
auto perf_attr = std::make_shared<ppc::core::PerfAttr>();
61-
perf_attr->num_running = 10;
62-
63-
// Create and init perf results
64-
auto perf_results = std::make_shared<ppc::core::PerfResults>();
37+
auto test_task = std::make_shared<ppc::test::perf::TestTask<float>>(in);
6538

6639
// Create Perf analyzer
6740
ppc::core::Perf perf_analyzer(test_task);
68-
perf_analyzer.PipelineRun(perf_attr, perf_results);
41+
42+
// Create Perf attributes
43+
ppc::core::PerfAttr perf_attr;
44+
perf_analyzer.PipelineRun(perf_attr);
6945

7046
// Get perf statistic
71-
ppc::core::Perf::PrintPerfStatistic(perf_results);
72-
ASSERT_LE(perf_results->time_sec, ppc::core::PerfResults::kMaxTime);
73-
EXPECT_EQ(out[0], in.size());
47+
perf_analyzer.PrintPerfStatistic();
48+
ASSERT_LE(perf_analyzer.GetPerfResults().time_sec, ppc::core::PerfResults::kMaxTime);
49+
EXPECT_EQ(test_task->Get(), in.size());
7450
}
7551

7652
TEST(perf_tests, check_perf_pipeline_uint8_t_slow_test) {
7753
// Create data
7854
std::vector<uint8_t> in(128, 1);
79-
std::vector<uint8_t> out(1, 0);
80-
81-
// Create task_data
82-
auto task_data = std::make_shared<ppc::core::TaskData>();
83-
task_data->inputs.emplace_back(reinterpret_cast<uint8_t *>(in.data()));
84-
task_data->inputs_count.emplace_back(in.size());
85-
task_data->outputs.emplace_back(reinterpret_cast<uint8_t *>(out.data()));
86-
task_data->outputs_count.emplace_back(out.size());
8755

8856
// Create Task
89-
auto test_task = std::make_shared<ppc::test::perf::FakePerfTask<uint8_t>>(task_data);
57+
auto test_task = std::make_shared<ppc::test::perf::FakePerfTask<uint8_t>>(in);
58+
59+
// Create Perf analyzer
60+
ppc::core::Perf perf_analyzer(test_task);
61+
9062
// Create Perf attributes
91-
auto perf_attr = std::make_shared<ppc::core::PerfAttr>();
92-
perf_attr->num_running = 1;
63+
ppc::core::PerfAttr perf_attr;
64+
perf_attr.num_running = 1;
65+
9366
const auto t0 = std::chrono::high_resolution_clock::now();
94-
perf_attr->current_timer = [&] {
67+
perf_attr.current_timer = [&] {
9568
auto current_time_point = std::chrono::high_resolution_clock::now();
9669
auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(current_time_point - t0).count();
9770
return static_cast<double>(duration) * 1e-9;
9871
};
99-
100-
// Create and init perf results
101-
auto perf_results = std::make_shared<ppc::core::PerfResults>();
102-
103-
// Create Perf analyzer
104-
ppc::core::Perf perf_analyzer(test_task);
105-
perf_analyzer.PipelineRun(perf_attr, perf_results);
72+
perf_analyzer.PipelineRun(perf_attr);
10673

10774
// Get perf statistic
108-
ASSERT_ANY_THROW(ppc::core::Perf::PrintPerfStatistic(perf_results));
109-
ASSERT_GE(perf_results->time_sec, ppc::core::PerfResults::kMaxTime);
110-
EXPECT_EQ(out[0], in.size());
75+
ASSERT_ANY_THROW(perf_analyzer.PrintPerfStatistic());
11176
}
11277

113-
TEST(perf_tests, check_perf_task) {
78+
TEST(perf_tests, check_perf_task_exception) {
11479
// Create data
11580
std::vector<uint32_t> in(2000, 1);
116-
std::vector<uint32_t> out(1, 0);
117-
118-
// Create task_data
119-
auto task_data = std::make_shared<ppc::core::TaskData>();
120-
task_data->inputs.emplace_back(reinterpret_cast<uint8_t *>(in.data()));
121-
task_data->inputs_count.emplace_back(in.size());
122-
task_data->outputs.emplace_back(reinterpret_cast<uint8_t *>(out.data()));
123-
task_data->outputs_count.emplace_back(out.size());
12481

12582
// Create Task
126-
auto test_task = std::make_shared<ppc::test::perf::TestTask<uint32_t>>(task_data);
127-
128-
// Create Perf attributes
129-
auto perf_attr = std::make_shared<ppc::core::PerfAttr>();
130-
perf_attr->num_running = 10;
131-
132-
// Create and init perf results
133-
auto perf_results = std::make_shared<ppc::core::PerfResults>();
83+
auto test_task = std::make_shared<ppc::test::perf::TestTask<uint32_t>>(in);
13484

13585
// Create Perf analyzer
13686
ppc::core::Perf perf_analyzer(test_task);
137-
perf_analyzer.TaskRun(perf_attr, perf_results);
13887

13988
// Get perf statistic
140-
perf_results->type_of_running = ppc::core::PerfResults::kNone;
141-
ppc::core::Perf::PrintPerfStatistic(perf_results);
142-
ASSERT_LE(perf_results->time_sec, ppc::core::PerfResults::kMaxTime);
143-
EXPECT_EQ(out[0], in.size());
89+
ASSERT_ANY_THROW(perf_analyzer.PrintPerfStatistic());
90+
91+
// Create Perf attributes
92+
ppc::core::PerfAttr perf_attr;
93+
perf_analyzer.TaskRun(perf_attr);
14494
}
14595

14696
TEST(perf_tests, check_perf_task_float) {
14797
// Create data
14898
std::vector<float> in(2000, 1);
149-
std::vector<float> out(1, 0);
150-
151-
// Create task_data
152-
auto task_data = std::make_shared<ppc::core::TaskData>();
153-
task_data->inputs.emplace_back(reinterpret_cast<uint8_t *>(in.data()));
154-
task_data->inputs_count.emplace_back(in.size());
155-
task_data->outputs.emplace_back(reinterpret_cast<uint8_t *>(out.data()));
156-
task_data->outputs_count.emplace_back(out.size());
15799

158100
// Create Task
159-
auto test_task = std::make_shared<ppc::test::perf::TestTask<float>>(task_data);
160-
161-
// Create Perf attributes
162-
auto perf_attr = std::make_shared<ppc::core::PerfAttr>();
163-
perf_attr->num_running = 10;
164-
165-
// Create and init perf results
166-
auto perf_results = std::make_shared<ppc::core::PerfResults>();
101+
auto test_task = std::make_shared<ppc::test::perf::TestTask<float>>(in);
167102

168103
// Create Perf analyzer
169104
ppc::core::Perf perf_analyzer(test_task);
170-
perf_analyzer.TaskRun(perf_attr, perf_results);
105+
106+
// Create Perf attributes
107+
ppc::core::PerfAttr perf_attr;
108+
perf_analyzer.TaskRun(perf_attr);
171109

172110
// Get perf statistic
173-
perf_results->type_of_running = ppc::core::PerfResults::kPipeline;
174-
ppc::core::Perf::PrintPerfStatistic(perf_results);
175-
ASSERT_LE(perf_results->time_sec, ppc::core::PerfResults::kMaxTime);
176-
EXPECT_EQ(out[0], in.size());
111+
perf_analyzer.PrintPerfStatistic();
112+
ASSERT_LE(perf_analyzer.GetPerfResults().time_sec, ppc::core::PerfResults::kMaxTime);
113+
EXPECT_EQ(test_task->Get(), in.size());
177114
}

modules/core/perf/func_tests/test_task.hpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,35 @@ namespace ppc::test::perf {
1212
template <class T>
1313
class TestTask : public ppc::core::Task {
1414
public:
15-
explicit TestTask(const ppc::core::TaskDataPtr &task_data) : Task(task_data) {}
15+
explicit TestTask(const std::vector<T>& in) : input_(in) {}
16+
17+
bool ValidationImpl() override { return !input_.empty(); }
1618

1719
bool PreProcessingImpl() override {
18-
input_ = reinterpret_cast<T *>(task_data->inputs[0]);
19-
output_ = reinterpret_cast<T *>(task_data->outputs[0]);
20-
output_[0] = 0;
20+
output_ = 0;
2121
return true;
2222
}
2323

24-
bool ValidationImpl() override { return task_data->outputs_count[0] == 1; }
25-
2624
bool RunImpl() override {
27-
for (unsigned i = 0; i < task_data->inputs_count[0]; i++) {
28-
output_[0] += input_[i];
25+
for (unsigned i = 0; i < input_.size(); i++) {
26+
output_ += input_[i];
2927
}
3028
return true;
3129
}
3230

3331
bool PostProcessingImpl() override { return true; }
3432

33+
T Get() { return output_; }
34+
3535
private:
36-
T *input_{};
37-
T *output_{};
36+
std::vector<T> input_{};
37+
T output_;
3838
};
3939

4040
template <class T>
4141
class FakePerfTask : public TestTask<T> {
4242
public:
43-
explicit FakePerfTask(ppc::core::TaskDataPtr perf_task_data) : TestTask<T>(perf_task_data) {}
43+
explicit FakePerfTask(const std::vector<T>& in) : TestTask<T>(in) {}
4444

4545
bool RunImpl() override {
4646
std::this_thread::sleep_for(std::chrono::seconds(11));

modules/core/perf/include/perf.hpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace ppc::core {
1010

1111
struct PerfAttr {
1212
// count of task's running
13-
uint64_t num_running;
13+
uint64_t num_running = 10;
1414
std::function<double()> current_timer = [&] { return 0.0; };
1515
};
1616

@@ -25,21 +25,20 @@ class Perf {
2525
public:
2626
// Init performance analysis with initialized task and initialized data
2727
explicit Perf(const std::shared_ptr<Task>& task_ptr);
28-
// Set task with initialized task and initialized data for performance
29-
// analysis c
30-
void SetTask(const std::shared_ptr<Task>& task_ptr);
3128
// Check performance of full task's pipeline: PreProcessing() ->
3229
// Validation() -> Run() -> PostProcessing()
33-
void PipelineRun(const std::shared_ptr<PerfAttr>& perf_attr, const std::shared_ptr<PerfResults>& perf_results) const;
30+
void PipelineRun(const PerfAttr& perf_attr);
3431
// Check performance of task's Run() function
35-
void TaskRun(const std::shared_ptr<PerfAttr>& perf_attr, const std::shared_ptr<PerfResults>& perf_results) const;
32+
void TaskRun(const PerfAttr& perf_attr);
3633
// Pint results for automation checkers
37-
static void PrintPerfStatistic(const std::shared_ptr<PerfResults>& perf_results);
34+
void PrintPerfStatistic() const;
35+
// Get performance result structure of the current task
36+
PerfResults GetPerfResults();
3837

3938
private:
39+
PerfResults perf_results_;
4040
std::shared_ptr<Task> task_;
41-
static void CommonRun(const std::shared_ptr<PerfAttr>& perf_attr, const std::function<void()>& pipeline,
42-
const std::shared_ptr<PerfResults>& perf_results);
41+
static void CommonRun(const PerfAttr& perf_attr, const std::function<void()>& pipeline, PerfResults& perf_results);
4342
};
4443

4544
} // namespace ppc::core

modules/core/perf/src/perf.cpp

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,12 @@
1313

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

16-
ppc::core::Perf::Perf(const std::shared_ptr<Task>& task_ptr) { SetTask(task_ptr); }
17-
18-
void ppc::core::Perf::SetTask(const std::shared_ptr<Task>& task_ptr) {
19-
task_ptr->GetData()->state_of_testing = TaskData::StateOfTesting::kPerf;
20-
this->task_ = task_ptr;
16+
ppc::core::Perf::Perf(const std::shared_ptr<Task>& task_ptr) : task_(task_ptr) {
17+
task_ptr->GetStateOfTesting() = Task::StateOfTesting::kPerf;
2118
}
2219

23-
void ppc::core::Perf::PipelineRun(const std::shared_ptr<PerfAttr>& perf_attr,
24-
const std::shared_ptr<ppc::core::PerfResults>& perf_results) const {
25-
perf_results->type_of_running = PerfResults::TypeOfRunning::kPipeline;
20+
void ppc::core::Perf::PipelineRun(const PerfAttr& perf_attr) {
21+
perf_results_.type_of_running = PerfResults::TypeOfRunning::kPipeline;
2622

2723
CommonRun(
2824
perf_attr,
@@ -32,16 +28,15 @@ void ppc::core::Perf::PipelineRun(const std::shared_ptr<PerfAttr>& perf_attr,
3228
task_->Run();
3329
task_->PostProcessing();
3430
},
35-
perf_results);
31+
perf_results_);
3632
}
3733

38-
void ppc::core::Perf::TaskRun(const std::shared_ptr<PerfAttr>& perf_attr,
39-
const std::shared_ptr<ppc::core::PerfResults>& perf_results) const {
40-
perf_results->type_of_running = PerfResults::TypeOfRunning::kTaskRun;
34+
void ppc::core::Perf::TaskRun(const PerfAttr& perf_attr) {
35+
perf_results_.type_of_running = PerfResults::TypeOfRunning::kTaskRun;
4136

4237
task_->Validation();
4338
task_->PreProcessing();
44-
CommonRun(perf_attr, [&]() { task_->Run(); }, perf_results);
39+
CommonRun(perf_attr, [&]() { task_->Run(); }, perf_results_);
4540
task_->PostProcessing();
4641

4742
task_->Validation();
@@ -50,30 +45,32 @@ void ppc::core::Perf::TaskRun(const std::shared_ptr<PerfAttr>& perf_attr,
5045
task_->PostProcessing();
5146
}
5247

53-
void ppc::core::Perf::CommonRun(const std::shared_ptr<PerfAttr>& perf_attr, const std::function<void()>& pipeline,
54-
const std::shared_ptr<ppc::core::PerfResults>& perf_results) {
55-
auto begin = perf_attr->current_timer();
56-
for (uint64_t i = 0; i < perf_attr->num_running; i++) {
48+
void ppc::core::Perf::CommonRun(const PerfAttr& perf_attr, const std::function<void()>& pipeline,
49+
ppc::core::PerfResults& perf_results) {
50+
auto begin = perf_attr.current_timer();
51+
for (uint64_t i = 0; i < perf_attr.num_running; i++) {
5752
pipeline();
5853
}
59-
auto end = perf_attr->current_timer();
60-
perf_results->time_sec = end - begin;
54+
auto end = perf_attr.current_timer();
55+
perf_results.time_sec = end - begin;
6156
}
6257

63-
void ppc::core::Perf::PrintPerfStatistic(const std::shared_ptr<PerfResults>& perf_results) {
58+
void ppc::core::Perf::PrintPerfStatistic() const {
6459
std::string relative_path(::testing::UnitTest::GetInstance()->current_test_info()->file());
6560
std::string ppc_regex_template("parallel_programming_course");
6661
std::string perf_regex_template("perf_tests");
6762
std::string type_test_name;
6863

69-
auto time_secs = perf_results->time_sec;
64+
auto time_secs = perf_results_.time_sec;
7065

71-
if (perf_results->type_of_running == PerfResults::TypeOfRunning::kTaskRun) {
66+
if (perf_results_.type_of_running == PerfResults::TypeOfRunning::kTaskRun) {
7267
type_test_name = "task_run";
73-
} else if (perf_results->type_of_running == PerfResults::TypeOfRunning::kPipeline) {
68+
} else if (perf_results_.type_of_running == PerfResults::TypeOfRunning::kPipeline) {
7469
type_test_name = "pipeline";
7570
} else {
76-
type_test_name = "none";
71+
std::stringstream err_msg;
72+
err_msg << '\n' << "The type of performance check for the task was not selected.\n";
73+
throw std::runtime_error(err_msg.str().c_str());
7774
}
7875

7976
auto first_found_position = relative_path.find(ppc_regex_template) + ppc_regex_template.length() + 1;
@@ -96,3 +93,5 @@ void ppc::core::Perf::PrintPerfStatistic(const std::shared_ptr<PerfResults>& per
9693
throw std::runtime_error(err_msg.str().c_str());
9794
}
9895
}
96+
97+
ppc::core::PerfResults ppc::core::Perf::GetPerfResults() { return perf_results_; }

0 commit comments

Comments
 (0)