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