/builds/MusicScience37Projects/utility-libraries/cpp-stat-bench/src/stat_bench/reporter/plot_reporter.cpp
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2025 MusicScience37 (Kenta Kabashima) |
3 | | * |
4 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | | * you may not use this file except in compliance with the License. |
6 | | * You may obtain a copy of the License at |
7 | | * |
8 | | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | | * |
10 | | * Unless required by applicable law or agreed to in writing, software |
11 | | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | | * See the License for the specific language governing permissions and |
14 | | * limitations under the License. |
15 | | */ |
16 | | /*! |
17 | | * \file |
18 | | * \brief Implementation of PlotReporter class. |
19 | | */ |
20 | | #include "stat_bench/reporter/plot_reporter.h" |
21 | | |
22 | | #include <cstddef> |
23 | | #include <filesystem> |
24 | | #include <memory> |
25 | | #include <utility> |
26 | | |
27 | | #include <fmt/format.h> |
28 | | |
29 | | #include "stat_bench/bench_impl/benchmark_group_config.h" |
30 | | #include "stat_bench/benchmark_group_name.h" |
31 | | #include "stat_bench/plots/box_plot.h" |
32 | | #include "stat_bench/plots/cdf_line_plot.h" |
33 | | #include "stat_bench/plots/samples_line_plot.h" |
34 | | #include "stat_bench/plots/violin_plot.h" |
35 | | #include "stat_bench/util/escape_for_file_name.h" |
36 | | #include "stat_bench/util/utf8_string.h" |
37 | | |
38 | | namespace stat_bench { |
39 | | namespace reporter { |
40 | | |
41 | | PlotReporter::PlotReporter(std::string prefix) |
42 | 6 | : prefix_(std::move(prefix)), group_name_(""), measurement_type_("") { |
43 | 6 | builtin_plots_.push_back(std::make_shared<plots::SamplesLinePlot>()); |
44 | 6 | builtin_plots_.push_back(std::make_shared<plots::CdfLinePlot>()); |
45 | 6 | builtin_plots_.push_back(std::make_shared<plots::ViolinPlot>()); |
46 | 6 | builtin_plots_.push_back(std::make_shared<plots::BoxPlot>()); |
47 | 6 | } |
48 | | |
49 | | void PlotReporter::experiment_starts( |
50 | 6 | const clock::SystemTimePoint& /*time_stamp*/) { |
51 | | // no operation |
52 | 6 | } |
53 | | |
54 | | void PlotReporter::experiment_finished( |
55 | 6 | const clock::SystemTimePoint& /*time_stamp*/) { |
56 | | // no operation |
57 | 6 | } |
58 | | |
59 | | void PlotReporter::group_starts(const BenchmarkGroupName& name, |
60 | 9 | const bench_impl::BenchmarkGroupConfig& config) { |
61 | 9 | group_name_ = name; |
62 | 9 | group_plots_ = config.plots(); |
63 | 9 | } |
64 | | |
65 | 9 | void PlotReporter::group_finished(const BenchmarkGroupName& /*name*/) { |
66 | | // no operation |
67 | 9 | } |
68 | | |
69 | | void PlotReporter::measurement_type_starts( |
70 | 17 | const measurer::MeasurementType& type) { |
71 | 17 | measurement_type_ = type; |
72 | | |
73 | 17 | std::string measurement_type_without_space = measurement_type_.str().str(); |
74 | 17 | std::size_t pos = 0; |
75 | 41 | while ((pos = measurement_type_without_space.find(' ', pos)) != |
76 | 41 | std::string::npos) { |
77 | 24 | measurement_type_without_space.erase(pos, 1); |
78 | 24 | } |
79 | | |
80 | 17 | measurement_type_for_file_paths_ = util::escape_for_file_name( |
81 | 17 | util::Utf8String(measurement_type_without_space)); |
82 | 17 | } |
83 | | |
84 | | void PlotReporter::measurement_type_finished( |
85 | 17 | const measurer::MeasurementType& /*type*/) { |
86 | 17 | const auto process_plot = [this]( |
87 | 122 | const std::shared_ptr<plots::IPlot>& plot) { |
88 | 122 | const std::string file_path = fmt::format("{}/{}/{}_{}.html", prefix_, |
89 | 122 | util::escape_for_file_name(group_name_.str()), |
90 | 122 | measurement_type_for_file_paths_, |
91 | 122 | util::escape_for_file_name(plot->name_for_file())); |
92 | 122 | std::filesystem::create_directories( |
93 | 122 | std::filesystem::path(file_path).parent_path()); |
94 | 122 | plot->write(measurement_type_, group_name_, measurements_, file_path); |
95 | 122 | }; |
96 | 68 | for (const auto& plot : builtin_plots_) { |
97 | 68 | process_plot(plot); |
98 | 68 | } |
99 | 54 | for (const auto& plot : group_plots_) { |
100 | 54 | process_plot(plot); |
101 | 54 | } |
102 | | |
103 | 17 | measurements_.clear(); |
104 | 17 | } |
105 | | |
106 | 157 | void PlotReporter::case_starts(const BenchmarkFullName& /*case_info*/) { |
107 | | // no operation |
108 | 157 | } |
109 | | |
110 | 157 | void PlotReporter::case_finished(const BenchmarkFullName& /*case_info*/) { |
111 | | // no operation |
112 | 157 | } |
113 | | |
114 | | void PlotReporter::measurement_succeeded( |
115 | 156 | const measurer::Measurement& measurement) { |
116 | 156 | measurements_.push_back(measurement); |
117 | 156 | } |
118 | | |
119 | | void PlotReporter::measurement_failed(const BenchmarkFullName& /*case_info*/, |
120 | 1 | const BenchmarkCondition& /*cond*/, const std::exception_ptr& /*error*/) { |
121 | | // no operation |
122 | 1 | } |
123 | | |
124 | | } // namespace reporter |
125 | | } // namespace stat_bench |