/builds/MusicScience37Projects/utility-libraries/cpp-stat-bench/src/stat_bench/reporter/data_file_helper.cpp
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2021 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 Definition of functions to help output of data files. |
19 | | */ |
20 | | #include "stat_bench/reporter/data_file_helper.h" |
21 | | |
22 | | #include <cstddef> |
23 | | #include <functional> |
24 | | |
25 | | #include "stat_bench/benchmark_case_name.h" |
26 | | #include "stat_bench/benchmark_condition.h" |
27 | | #include "stat_bench/benchmark_full_name.h" |
28 | | #include "stat_bench/benchmark_group_name.h" |
29 | | #include "stat_bench/measurer/measurement_type.h" |
30 | | |
31 | | namespace stat_bench { |
32 | | namespace reporter { |
33 | | namespace data_file_spec { |
34 | | |
35 | | auto convert(const param::ParameterDict& params) |
36 | 60 | -> std::unordered_map<util::Utf8String, util::Utf8String> { |
37 | 60 | return params.as_string_dict(); |
38 | 60 | } |
39 | | |
40 | 96 | auto convert(const stat::Statistics& stat) -> StatData { |
41 | 96 | return StatData{static_cast<float>(stat.mean()), |
42 | 96 | static_cast<float>(stat.max()), static_cast<float>(stat.min()), |
43 | 96 | static_cast<float>(stat.median()), static_cast<float>(stat.variance()), |
44 | 96 | static_cast<float>(stat.standard_deviation()), |
45 | 96 | static_cast<float>(stat.standard_error())}; |
46 | 96 | } |
47 | | |
48 | | auto convert(const std::vector<std::vector<clock::Duration>>& durations, |
49 | 60 | const stat::Statistics& durations_stat) -> DurationData { |
50 | 60 | std::vector<std::vector<float>> values; |
51 | 60 | values.reserve(durations.size()); |
52 | 60 | for (const auto& durations_per_thread : durations) { |
53 | 60 | values.emplace_back(); |
54 | 60 | auto& values_per_thread = values.back(); |
55 | 60 | values_per_thread.reserve(durations_per_thread.size()); |
56 | 177 | for (const auto& duration : durations_per_thread) { |
57 | 177 | values_per_thread.push_back(static_cast<float>(duration.seconds())); |
58 | 177 | } |
59 | 60 | } |
60 | 60 | return DurationData{convert(durations_stat), std::move(values)}; |
61 | 60 | } |
62 | | |
63 | | auto convert(const std::shared_ptr<stat::CustomStatOutput>& stat_output, |
64 | 36 | const stat::Statistics& stat) -> CustomStatOutputData { |
65 | 36 | std::vector<std::vector<float>> data; |
66 | 36 | data.reserve(stat_output->data().size()); |
67 | 36 | for (const auto& vec : stat_output->data()) { |
68 | 36 | std::vector<float> vec_copy; |
69 | 36 | vec_copy.reserve(vec.size()); |
70 | 108 | for (const double val : vec) { |
71 | 108 | vec_copy.push_back(static_cast<float>(val)); |
72 | 108 | } |
73 | 36 | data.push_back(std::move(vec_copy)); |
74 | 36 | } |
75 | 36 | return CustomStatOutputData{stat_output->name().str(), convert(stat), data}; |
76 | 36 | } |
77 | | |
78 | | auto convert( |
79 | | const std::vector<std::shared_ptr<stat::CustomStatOutput>>& stat_outputs, |
80 | | const std::vector<stat::Statistics>& stats) |
81 | 60 | -> std::vector<CustomStatOutputData> { |
82 | 60 | std::vector<CustomStatOutputData> data; |
83 | 60 | data.reserve(stat_outputs.size()); |
84 | 96 | for (std::size_t i = 0; i < stat_outputs.size(); ++i) { |
85 | 36 | data.push_back(convert(stat_outputs.at(i), stats.at(i))); |
86 | 36 | } |
87 | 60 | return data; |
88 | 60 | } |
89 | | |
90 | | auto convert(const std::vector<std::pair<CustomOutputName, double>>& outputs) |
91 | 60 | -> std::vector<CustomOutputData> { |
92 | 60 | std::vector<CustomOutputData> data; |
93 | 60 | data.reserve(outputs.size()); |
94 | 60 | for (const auto& output : outputs) { |
95 | 54 | data.push_back(CustomOutputData{ |
96 | 54 | output.first.str(), static_cast<float>(output.second)}); |
97 | 54 | } |
98 | 60 | return data; |
99 | 60 | } |
100 | | |
101 | 60 | auto convert(const measurer::Measurement& measurement) -> MeasurementData { |
102 | 60 | return MeasurementData{measurement.case_info().group_name().str(), |
103 | 60 | measurement.case_info().case_name().str(), |
104 | 60 | convert(measurement.cond().params()), |
105 | 60 | measurement.measurement_type().str(), measurement.iterations(), |
106 | 60 | measurement.samples(), |
107 | 60 | convert(measurement.durations(), measurement.durations_stat()), |
108 | 60 | convert(measurement.custom_stat_outputs(), measurement.custom_stat()), |
109 | 60 | convert(measurement.custom_outputs())}; |
110 | 60 | } |
111 | | |
112 | | } // namespace data_file_spec |
113 | | } // namespace reporter |
114 | | } // namespace stat_bench |