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