|
| 1 | +/* vim:set ts=2 sw=2 sts=2 et: */ |
| 2 | +/** |
| 3 | + * \author Marcus Holland-Moritz ([email protected]) |
| 4 | + * \copyright Copyright (c) Marcus Holland-Moritz |
| 5 | + * |
| 6 | + * This file is part of ricepp. |
| 7 | + * |
| 8 | + * ricepp is free software: you can redistribute it and/or modify |
| 9 | + * it under the terms of the GNU General Public License as published by |
| 10 | + * the Free Software Foundation, either version 3 of the License, or |
| 11 | + * (at your option) any later version. |
| 12 | + * |
| 13 | + * ricepp is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | + * GNU General Public License for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU General Public License |
| 19 | + * along with ricepp. If not, see <https://www.gnu.org/licenses/>. |
| 20 | + */ |
| 21 | + |
| 22 | +#include <filesystem> |
| 23 | +#include <format> |
| 24 | +#include <fstream> |
| 25 | +#include <iostream> |
| 26 | + |
| 27 | +#include <benchmark/benchmark.h> |
| 28 | + |
| 29 | +#include <ricepp/byteswap.h> |
| 30 | +#include <ricepp/ricepp.h> |
| 31 | + |
| 32 | +namespace { |
| 33 | + |
| 34 | +std::filesystem::path const g_testdata_dir{ |
| 35 | + "/home/mhx/git/github/dwarfs/@ricepp-testdata"}; |
| 36 | + |
| 37 | +struct config { |
| 38 | + unsigned component_stream_count; |
| 39 | + unsigned unused_lsb_count; |
| 40 | +}; |
| 41 | + |
| 42 | +std::map<std::string, config> const g_camera_info = { |
| 43 | + {"ASI178MC", {2, 0}}, {"ASI294MC", {2, 2}}, {"ASI1600MM", {1, 4}}, |
| 44 | + {"ASI2600MC", {2, 0}}, {"ASI2600MM", {1, 0}}, {"ASI6200MC", {2, 0}}, |
| 45 | +}; |
| 46 | + |
| 47 | +class ricepp_bm : public ::benchmark::Fixture { |
| 48 | + public: |
| 49 | + void SetUp(::benchmark::State const& state) { |
| 50 | + if (state.thread_index() > 0) { |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + auto const [camera, test, operation] = [&] { |
| 55 | + std::string name{state.name()}; |
| 56 | + auto const pos1 = name.find('/'); |
| 57 | + auto const pos2 = name.find('/', pos1 + 1); |
| 58 | + return std::make_tuple(name.substr(0, pos1), |
| 59 | + name.substr(pos1 + 1, pos2 - pos1 - 1), |
| 60 | + name.substr(pos2 + 1)); |
| 61 | + }(); |
| 62 | + |
| 63 | + auto const& camera_info = g_camera_info.at(camera); |
| 64 | + |
| 65 | + codec_ = ricepp::create_codec<uint16_t>({ |
| 66 | + .block_size = 128, |
| 67 | + .component_stream_count = camera_info.component_stream_count, |
| 68 | + .byteorder = std::endian::big, |
| 69 | + .unused_lsb_count = camera_info.unused_lsb_count, |
| 70 | + }); |
| 71 | + |
| 72 | + data_ = load_fits_data(g_testdata_dir / camera / (test + ".fit")); |
| 73 | + |
| 74 | + encoded_ = codec_->encode(data_); |
| 75 | + } |
| 76 | + |
| 77 | + void TearDown(::benchmark::State const&) {} |
| 78 | + |
| 79 | + std::vector<uint16_t> load_fits_data(std::filesystem::path const& path) { |
| 80 | + static constexpr size_t kBlockSize = 2880; |
| 81 | + static constexpr size_t kDataSize = 8 * 1024 * 1024; |
| 82 | + |
| 83 | + std::ifstream ifs{path, std::ios::binary}; |
| 84 | + if (!ifs) { |
| 85 | + throw std::runtime_error{"failed to open file: " + path.string()}; |
| 86 | + } |
| 87 | + |
| 88 | + // skip a bunch of header blocks |
| 89 | + ifs.seekg(8 * kBlockSize, std::ios::beg); |
| 90 | + |
| 91 | + std::vector<uint16_t> data; |
| 92 | + data.resize(kDataSize / sizeof(data[0])); |
| 93 | + ifs.read(reinterpret_cast<char*>(data.data()), kDataSize); |
| 94 | + |
| 95 | + if (!ifs) { |
| 96 | + throw std::runtime_error{"failed to read data from file: " + |
| 97 | + path.string()}; |
| 98 | + } |
| 99 | + |
| 100 | + return data; |
| 101 | + } |
| 102 | + |
| 103 | + std::unique_ptr<ricepp::codec_interface<uint16_t>> codec_; |
| 104 | + std::vector<uint16_t> data_; |
| 105 | + std::vector<uint8_t> encoded_; |
| 106 | +}; |
| 107 | + |
| 108 | +} // namespace |
| 109 | + |
| 110 | +#define RICEPP_BENCHMARK(camera, test) \ |
| 111 | + BENCHMARK_DEFINE_F(ricepp_bm, encode_##camera##_##test) \ |
| 112 | + (::benchmark::State & state) { \ |
| 113 | + std::vector<uint8_t> encoded; \ |
| 114 | + encoded.resize(codec_->worst_case_encoded_bytes(data_)); \ |
| 115 | + for (auto _ : state) { \ |
| 116 | + auto r = codec_->encode(encoded, data_); \ |
| 117 | + ::benchmark::DoNotOptimize(r); \ |
| 118 | + } \ |
| 119 | + state.SetBytesProcessed(static_cast<int64_t>(state.iterations()) * \ |
| 120 | + data_.size() * sizeof(data_[0])); \ |
| 121 | + state.SetLabel( \ |
| 122 | + std::format("({:.2f}%)", 100.0 * encoded_.size() / \ |
| 123 | + (data_.size() * sizeof(data_[0])))); \ |
| 124 | + } \ |
| 125 | + BENCHMARK_DEFINE_F(ricepp_bm, decode_##camera##_##test) \ |
| 126 | + (::benchmark::State & state) { \ |
| 127 | + std::vector<uint16_t> decoded; \ |
| 128 | + decoded.resize(data_.size()); \ |
| 129 | + for (auto _ : state) { \ |
| 130 | + codec_->decode(decoded, encoded_); \ |
| 131 | + } \ |
| 132 | + state.SetBytesProcessed(static_cast<int64_t>(state.iterations()) * \ |
| 133 | + data_.size() * sizeof(data_[0])); \ |
| 134 | + } \ |
| 135 | + BENCHMARK_REGISTER_F(ricepp_bm, encode_##camera##_##test) \ |
| 136 | + ->Name(#camera "/" #test "/encode"); \ |
| 137 | + BENCHMARK_REGISTER_F(ricepp_bm, decode_##camera##_##test) \ |
| 138 | + ->Name(#camera "/" #test "/decode"); |
| 139 | + |
| 140 | +RICEPP_BENCHMARK(ASI1600MM, dark_120s_g139) |
| 141 | +RICEPP_BENCHMARK(ASI1600MM, dark_1s_g0) |
| 142 | +RICEPP_BENCHMARK(ASI1600MM, flat_h_2s_g0) |
| 143 | +RICEPP_BENCHMARK(ASI1600MM, light_g_60s_g0) |
| 144 | +RICEPP_BENCHMARK(ASI1600MM, light_s_120s_g139) |
| 145 | +RICEPP_BENCHMARK(ASI178MC, bias_g0) |
| 146 | +RICEPP_BENCHMARK(ASI178MC, dark_60s_g0) |
| 147 | +RICEPP_BENCHMARK(ASI178MC, flat_g0) |
| 148 | +RICEPP_BENCHMARK(ASI178MC, light_60s_g0) |
| 149 | +RICEPP_BENCHMARK(ASI2600MC, flat_g0) |
| 150 | +RICEPP_BENCHMARK(ASI2600MC, light_180s_g100) |
| 151 | +RICEPP_BENCHMARK(ASI2600MM, bias_g0) |
| 152 | +RICEPP_BENCHMARK(ASI2600MM, bias_g100) |
| 153 | +RICEPP_BENCHMARK(ASI2600MM, bias_g300) |
| 154 | +RICEPP_BENCHMARK(ASI2600MM, dark_900s_g300) |
| 155 | +RICEPP_BENCHMARK(ASI2600MM, flat_h_g0) |
| 156 | +RICEPP_BENCHMARK(ASI2600MM, light_b_30s_g0) |
| 157 | +RICEPP_BENCHMARK(ASI2600MM, light_h_120s_g300) |
| 158 | +RICEPP_BENCHMARK(ASI294MC, light_120s_g200) |
| 159 | +RICEPP_BENCHMARK(ASI294MC, light_180s_g0) |
| 160 | +RICEPP_BENCHMARK(ASI294MC, light_60s_g0) |
| 161 | +RICEPP_BENCHMARK(ASI6200MC, light_60s_g100) |
| 162 | + |
| 163 | +BENCHMARK_MAIN(); |
0 commit comments