| 1 | #include <chrono> |
| 2 | #include <cstdlib> |
| 3 | #include <iostream> |
| 4 | #include <sstream> |
| 5 | #include <string> |
| 6 | #include <vector> |
| 7 | |
| 8 | #include "gsd.h" |
| 9 | |
| 10 | int main(int argc, char** argv) // NOLINT |
| 11 | { |
| 12 | const size_t n_keys = 2048; |
| 13 | const size_t max_frames = 100; |
| 14 | std::vector<char> data; |
| 15 | |
| 16 | std::vector<std::string> names; |
| 17 | for (size_t i = 0; i < n_keys; i++) |
| 18 | { |
| 19 | std::ostringstream s; |
| 20 | s << "key " << i; |
| 21 | names.push_back(s.str()); |
| 22 | } |
| 23 | |
| 24 | gsd_handle handle; |
| 25 | auto t1 = std::chrono::high_resolution_clock::now(); |
| 26 | gsd_open(&handle, "test.gsd", GSD_OPEN_READONLY); |
| 27 | auto t2 = std::chrono::high_resolution_clock::now(); |
| 28 | auto const open_time = std::chrono::duration_cast<std::chrono::duration<double>>(t2 - t1); |
| 29 | std::cout << "Time to open: " << open_time.count() / 1e-6 << " microseconds\n"; |
| 30 | |
| 31 | size_t const n_frames = gsd_get_nframes(&handle); |
| 32 | size_t n_read = n_frames; |
| 33 | if (n_read > max_frames) |
| 34 | { |
| 35 | n_read = max_frames; |
| 36 | } |
| 37 | |
| 38 | std::cout << "Reading test.gsd with: " << n_keys << " keys and " << n_frames << " frames." |
| 39 | << '\n'; |
| 40 | |
| 41 | t1 = std::chrono::high_resolution_clock::now(); |
| 42 | |
| 43 | size_t total_bytes = 0; |
| 44 | for (size_t frame = 0; frame < n_read; frame++) |
| 45 | { |
| 46 | for (auto const& name : names) |
| 47 | { |
| 48 | const gsd_index_entry* e; |
| 49 | e = gsd_find_chunk(&handle, frame, name.c_str()); |
| 50 | if (e == nullptr) |
| 51 | { |
| 52 | std::cout << "ERROR: Chunk `" << name << "` not found\n"; |
| 53 | exit(1); |
| 54 | } |
| 55 | if (data.empty()) |
| 56 | { |
| 57 | data.resize(e->N * e->M * gsd_sizeof_type((gsd_type)e->type)); |
| 58 | } |
| 59 | total_bytes += e->N * e->M * gsd_sizeof_type((gsd_type)e->type); |
| 60 | gsd_read_chunk(&handle, data.data(), e); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | t2 = std::chrono::high_resolution_clock::now(); |
| 65 | |
| 66 | auto const time_span = std::chrono::duration_cast<std::chrono::duration<double>>(t2 - t1); |
| 67 | double const time_per_key = time_span.count() / double(n_keys) / double(n_read); |
| 68 | double const us_per_key = time_per_key / 1e-6; |
| 69 | |
| 70 | std::cout << "Sequential latency : " << us_per_key << " microseconds/key.\n"; |
| 71 | |
| 72 | std::cout << "Sequential throughput: " |
| 73 | << double(total_bytes) / 1024.0 / 1024.0 / time_span.count() << " MB/s\n"; |
| 74 | |
| 75 | gsd_close(&handle); |
| 76 | } |