Line | Branch | Exec | Source |
---|---|---|---|
1 | #pragma once | ||
2 | |||
3 | #include <cstdint> | ||
4 | #include <functional> | ||
5 | #include <iomanip> | ||
6 | #include <iostream> | ||
7 | #include <memory> | ||
8 | #include <sstream> | ||
9 | #include <stdexcept> | ||
10 | #include <string> | ||
11 | |||
12 | #include "core/task/include/task.hpp" | ||
13 | |||
14 | namespace ppc::core { | ||
15 | |||
16 |
9/27✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 10 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 10 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 10 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✓ Branch 14 taken 10 times.
✗ Branch 15 not taken.
✓ Branch 17 taken 10 times.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✓ Branch 21 taken 10 times.
✗ Branch 22 not taken.
✓ Branch 24 taken 10 times.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
✓ Branch 28 taken 10 times.
✗ Branch 29 not taken.
|
90 | struct PerfAttr { |
17 | /// @brief Number of times the task is run for performance evaluation. | ||
18 | uint64_t num_running = 5; | ||
19 | /// @brief Timer function returning current time in seconds. | ||
20 | /// @cond | ||
21 | std::function<double()> current_timer = [&] { return -1.0; }; | ||
22 | /// @endcond | ||
23 | }; | ||
24 | |||
25 | 50 | struct PerfResults { | |
26 | /// @brief Measured execution time in seconds. | ||
27 | double time_sec = 0.0; | ||
28 | enum TypeOfRunning : uint8_t { kPipeline, kTaskRun, kNone } type_of_running = kNone; | ||
29 | constexpr static double kMaxTime = 10.0; | ||
30 | }; | ||
31 | |||
32 | template <typename InType, typename OutType> | ||
33 |
5/20✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 10 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 10 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✓ Branch 14 taken 10 times.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✓ Branch 18 taken 10 times.
✗ Branch 19 not taken.
|
50 | class Perf { |
34 | public: | ||
35 | // Init performance analysis with an initialized task and initialized data | ||
36 | 50 | explicit Perf(const TaskPtr<InType, OutType>& task_ptr) : task_(task_ptr) { | |
37 |
5/10✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 10 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 10 times.
✗ Branch 9 not taken.
|
50 | task_ptr->GetStateOfTesting() = StateOfTesting::kPerf; |
38 | } | ||
39 | // Check performance of full task's pipeline: PreProcessing() -> | ||
40 | // Validation() -> Run() -> PostProcessing() | ||
41 | 60 | void PipelineRun(const PerfAttr& perf_attr) { | |
42 | 60 | perf_results_.type_of_running = PerfResults::TypeOfRunning::kPipeline; | |
43 | |||
44 |
1/2✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
|
280 | CommonRun(perf_attr, [&]() { |
45 | 110 | task_->Validation(); | |
46 | 110 | task_->PreProcessing(); | |
47 | 110 | task_->Run(); | |
48 | 110 | task_->PostProcessing(); | |
49 |
1/2✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
|
60 | }, perf_results_); |
50 | 60 | } | |
51 | // Check performance of task's Run() function | ||
52 | 40 | void TaskRun(const PerfAttr& perf_attr) { | |
53 | 40 | perf_results_.type_of_running = PerfResults::TypeOfRunning::kTaskRun; | |
54 | |||
55 | 40 | task_->Validation(); | |
56 | 40 | task_->PreProcessing(); | |
57 |
1/3✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
180 | CommonRun(perf_attr, [&]() { task_->Run(); }, perf_results_); |
58 | 40 | task_->PostProcessing(); | |
59 | |||
60 | 40 | task_->Validation(); | |
61 | 40 | task_->PreProcessing(); | |
62 | 40 | task_->Run(); | |
63 | 40 | task_->PostProcessing(); | |
64 | 40 | } | |
65 | // Pint results for automation checkers | ||
66 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 40 times.
|
100 | void PrintPerfStatistic(const std::string& test_id) const { |
67 | std::string type_test_name; | ||
68 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 40 times.
|
100 | if (perf_results_.type_of_running == PerfResults::TypeOfRunning::kTaskRun) { |
69 | type_test_name = "task_run"; | ||
70 |
2/2✓ Branch 0 taken 30 times.
✓ Branch 1 taken 10 times.
|
80 | } else if (perf_results_.type_of_running == PerfResults::TypeOfRunning::kPipeline) { |
71 | type_test_name = "pipeline"; | ||
72 | } else { | ||
73 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | std::stringstream err_msg; |
74 |
2/4✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
|
20 | err_msg << '\n' << "The type of performance check for the task was not selected.\n"; |
75 |
1/2✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
|
60 | throw std::runtime_error(err_msg.str().c_str()); |
76 | 20 | } | |
77 | |||
78 | 80 | auto time_secs = perf_results_.time_sec; | |
79 |
1/2✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
|
80 | std::stringstream perf_res_str; |
80 |
2/2✓ Branch 0 taken 30 times.
✓ Branch 1 taken 10 times.
|
80 | if (time_secs < PerfResults::kMaxTime) { |
81 | perf_res_str << std::fixed << std::setprecision(10) << time_secs; | ||
82 |
1/2✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
|
60 | std::cout << test_id << ":" << type_test_name << ":" << perf_res_str.str() << '\n'; |
83 | } else { | ||
84 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | std::stringstream err_msg; |
85 |
2/4✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
|
20 | err_msg << '\n' << "Task execute time need to be: "; |
86 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | err_msg << "time < " << PerfResults::kMaxTime << " secs." << '\n'; |
87 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | err_msg << "Original time in secs: " << time_secs << '\n'; |
88 | perf_res_str << std::fixed << std::setprecision(10) << -1.0; | ||
89 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
20 | std::cout << test_id << ":" << type_test_name << ":" << perf_res_str.str() << '\n'; |
90 |
1/2✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
|
60 | throw std::runtime_error(err_msg.str().c_str()); |
91 | 20 | } | |
92 | 140 | } | |
93 | /// @brief Retrieves the performance test results. | ||
94 | /// @return The latest PerfResults structure. | ||
95 |
3/6✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 10 times.
✗ Branch 8 not taken.
|
30 | PerfResults GetPerfResults() { return perf_results_; } |
96 | |||
97 | private: | ||
98 | PerfResults perf_results_; | ||
99 | std::shared_ptr<Task<InType, OutType>> task_; | ||
100 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
|
100 | static void CommonRun(const PerfAttr& perf_attr, const std::function<void()>& pipeline, PerfResults& perf_results) { |
101 | auto begin = perf_attr.current_timer(); | ||
102 |
2/2✓ Branch 0 taken 210 times.
✓ Branch 1 taken 50 times.
|
520 | for (uint64_t i = 0; i < perf_attr.num_running; i++) { |
103 | pipeline(); | ||
104 | } | ||
105 | auto end = perf_attr.current_timer(); | ||
106 | 100 | perf_results.time_sec = (end - begin) / static_cast<double>(perf_attr.num_running); | |
107 | 100 | } | |
108 | }; | ||
109 | |||
110 | 30 | inline std::string GetStringParamName(ppc::core::PerfResults::TypeOfRunning type_of_running) { | |
111 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 20 times.
|
30 | if (type_of_running == core::PerfResults::kTaskRun) { |
112 | 10 | return "task_run"; | |
113 | } | ||
114 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
|
20 | if (type_of_running == core::PerfResults::kPipeline) { |
115 | 10 | return "pipeline"; | |
116 | } | ||
117 | 10 | return "none"; | |
118 | } | ||
119 | |||
120 | } // namespace ppc::core | ||
121 |