|
| 1 | +#include "acquire.zarr.h" |
| 2 | +#include "test.macros.hh" |
| 3 | + |
| 4 | +#include <nlohmann/json.hpp> |
| 5 | + |
| 6 | +#include <chrono> |
| 7 | +#include <filesystem> |
| 8 | +#include <iostream> |
| 9 | +#include <vector> |
| 10 | + |
| 11 | +namespace fs = std::filesystem; |
| 12 | + |
| 13 | +namespace { |
| 14 | +constexpr uint32_t frame_size = 2048; |
| 15 | +const std::vector<uint8_t> |
| 16 | +frame_data(frame_size* frame_size, 0); |
| 17 | + |
| 18 | +const std::vector<uint32_t> chunk_sizes{ 32, 64, 128, 256 }; |
| 19 | +const std::vector<uint32_t> chunks_per_shard{ 8, 16, 32, 64 }; |
| 20 | +const std::vector<uint32_t> layers_per_shard{ 1, 2, 4, 8, 16 }; |
| 21 | + |
| 22 | +} // namespace |
| 23 | + |
| 24 | +ZarrStream* |
| 25 | +make_stream(uint32_t chunk_size, |
| 26 | + uint32_t n_chunks_per_shard, |
| 27 | + uint32_t n_layers_per_shard) |
| 28 | +{ |
| 29 | + ZarrStreamSettings settings{ .store_path = TEST ".zarr", |
| 30 | + .version = ZarrVersion_3, |
| 31 | + .overwrite = true }; |
| 32 | + |
| 33 | + EXPECT(ZarrStreamSettings_create_arrays(&settings, 1) == |
| 34 | + ZarrStatusCode_Success, |
| 35 | + "Failed to create array settings"); |
| 36 | + EXPECT(ZarrArraySettings_create_dimension_array(settings.arrays, 5) == |
| 37 | + ZarrStatusCode_Success, |
| 38 | + "Failed to create dimension array"); |
| 39 | + |
| 40 | + settings.arrays->data_type = ZarrDataType_uint8; |
| 41 | + |
| 42 | + settings.arrays[0].dimensions[0] = |
| 43 | + DIM("t", ZarrDimensionType_Time, 0, 1, n_layers_per_shard, nullptr, 1.0); |
| 44 | + settings.arrays[0].dimensions[1] = |
| 45 | + DIM("c", ZarrDimensionType_Channel, 1, 1, 1, nullptr, 1.0); |
| 46 | + settings.arrays[0].dimensions[2] = DIM("z", |
| 47 | + ZarrDimensionType_Space, |
| 48 | + chunk_sizes.back(), |
| 49 | + chunk_size, |
| 50 | + n_chunks_per_shard, |
| 51 | + "millimeter", |
| 52 | + 1.0); |
| 53 | + settings.arrays[0].dimensions[3] = DIM("y", |
| 54 | + ZarrDimensionType_Space, |
| 55 | + frame_size, |
| 56 | + chunk_size, |
| 57 | + n_chunks_per_shard, |
| 58 | + "micrometer", |
| 59 | + 1.0); |
| 60 | + settings.arrays[0].dimensions[4] = DIM("x", |
| 61 | + ZarrDimensionType_Space, |
| 62 | + frame_size, |
| 63 | + chunk_size, |
| 64 | + n_chunks_per_shard, |
| 65 | + "micrometer", |
| 66 | + 1.0); |
| 67 | + |
| 68 | + auto* stream = ZarrStream_create(&settings); |
| 69 | + |
| 70 | + // cleanup |
| 71 | + ZarrStreamSettings_destroy_arrays(&settings); |
| 72 | + |
| 73 | + return stream; |
| 74 | +} |
| 75 | + |
| 76 | +int |
| 77 | +main() |
| 78 | +{ |
| 79 | + int retval = 1; |
| 80 | + nlohmann::json results_arr = nlohmann::json::array(); |
| 81 | + ZarrStream* stream = nullptr; |
| 82 | + |
| 83 | + try { |
| 84 | + for (auto& layers : layers_per_shard) { |
| 85 | + for (auto& cps : chunks_per_shard) { |
| 86 | + for (auto& chunk_size : chunk_sizes) { |
| 87 | + const size_t chunks_xyz = |
| 88 | + (frame_size + chunk_size - 1) / chunk_size; |
| 89 | + if (cps > chunks_xyz) { |
| 90 | + continue; |
| 91 | + } |
| 92 | + |
| 93 | + const auto n_chunks = chunks_xyz * chunks_xyz * chunks_xyz; |
| 94 | + const auto n_frames = chunk_size * cps * layers; |
| 95 | + |
| 96 | + nlohmann::json j; |
| 97 | + j["chunk_size"] = chunk_size; |
| 98 | + j["chunks_per_shard"] = cps; |
| 99 | + j["layers_per_shard"] = layers; |
| 100 | + j["n_chunks"] = n_chunks; |
| 101 | + j["frames_written"] = n_frames; |
| 102 | + |
| 103 | + std::cout |
| 104 | + << "Testing chunk size " << chunk_size |
| 105 | + << ", chunks per shard " << cps << ", layers per shard " |
| 106 | + << layers << ", chunk count " << n_chunks << " (" |
| 107 | + << n_frames << " frames)... " << std::flush << std::endl; |
| 108 | + |
| 109 | + stream = make_stream(chunk_size, cps, layers); |
| 110 | + EXPECT(stream != nullptr, "Failed to create stream"); |
| 111 | + |
| 112 | + auto start = std::chrono::high_resolution_clock::now(); |
| 113 | + |
| 114 | + for (auto i = 0; i < n_frames; ++i) { |
| 115 | + size_t bytes_written = 0; |
| 116 | + ZarrStatusCode status = |
| 117 | + ZarrStream_append(stream, |
| 118 | + frame_data.data(), |
| 119 | + frame_data.size(), |
| 120 | + &bytes_written, |
| 121 | + nullptr); |
| 122 | + EXPECT(status == ZarrStatusCode_Success, |
| 123 | + "Failed to append frame ", |
| 124 | + i, |
| 125 | + ", status code ", |
| 126 | + int(status)); |
| 127 | + EXPECT(bytes_written == frame_data.size(), |
| 128 | + "Expected to write ", |
| 129 | + frame_data.size(), |
| 130 | + " bytes, but wrote ", |
| 131 | + bytes_written); |
| 132 | + std::cout << "." << std::flush; |
| 133 | + } |
| 134 | + auto end_append = std::chrono::high_resolution_clock::now(); |
| 135 | + std::chrono::duration<double> elapsed_append = |
| 136 | + end_append - start; |
| 137 | + |
| 138 | + std::cout << "\nFinalizing... " << std::flush; |
| 139 | + ZarrStream_destroy(stream); |
| 140 | + stream = nullptr; |
| 141 | + std::cout << "done." << std::endl; |
| 142 | + |
| 143 | + auto end_destroy = |
| 144 | + std::chrono::high_resolution_clock::now(); |
| 145 | + std::chrono::duration<double> elapsed_destroy = |
| 146 | + end_destroy - start; |
| 147 | + |
| 148 | + const double fps = n_frames / elapsed_append.count(); |
| 149 | + |
| 150 | + j["elapsed_time_append"] = elapsed_append.count(); |
| 151 | + j["elapsed_time_destroy"] = elapsed_destroy.count(); |
| 152 | + |
| 153 | + std::cout |
| 154 | + << "Wrote " << n_frames << " frames in " |
| 155 | + << elapsed_append.count() << " seconds (" << fps |
| 156 | + << " fps); time to destroy: " << elapsed_destroy.count() |
| 157 | + << " seconds" << std::endl; |
| 158 | + |
| 159 | + results_arr.push_back(j); |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + retval = 0; |
| 165 | + } catch (const std::exception& err) { |
| 166 | + LOG_ERROR("Failed: ", err.what()); |
| 167 | + } |
| 168 | + |
| 169 | + std::cout << results_arr.dump(2) << "\n"; |
| 170 | + |
| 171 | + // cleanup |
| 172 | + if (stream != nullptr) { |
| 173 | + ZarrStream_destroy(stream); |
| 174 | + } |
| 175 | + |
| 176 | + if (fs::exists(TEST ".zarr")) { |
| 177 | + fs::remove_all(TEST ".zarr"); |
| 178 | + } |
| 179 | + |
| 180 | + return retval; |
| 181 | +} |
0 commit comments