James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 1 | // Copyright (c) FIRST and other WPILib contributors. |
| 2 | // Open Source Software; you can modify and/or share it under the terms of |
| 3 | // the WPILib BSD license file in the root directory of this project. |
| 4 | |
| 5 | #include <array> |
| 6 | #include <chrono> |
| 7 | #include <numeric> |
| 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
James Kuszmaul | b13e13f | 2023-11-22 20:44:04 -0800 | [diff] [blame^] | 11 | #include <fmt/format.h> |
| 12 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame] | 13 | #include "wpi/DataLog.h" |
| 14 | |
| 15 | int main(int argc, char** argv) { |
| 16 | using std::chrono::duration_cast; |
| 17 | using std::chrono::high_resolution_clock; |
| 18 | using std::chrono::microseconds; |
| 19 | |
| 20 | int kNumRuns = 10; |
| 21 | |
| 22 | if (argc == 2) { |
| 23 | kNumRuns = std::stoi(argv[1]); |
| 24 | } |
| 25 | |
| 26 | wpi::log::DataLog log; |
| 27 | log.SetFilename("test.wpilog"); |
| 28 | |
| 29 | auto testVec = |
| 30 | std::vector<std::pair<std::string, void (*)(wpi::log::DataLog&)>>(); |
| 31 | |
| 32 | testVec.push_back({"50 double append", [](auto& log) { |
| 33 | wpi::log::DoubleLogEntry entry{log, "fifty", 1}; |
| 34 | for (int i = 0; i < 50; ++i) { |
| 35 | entry.Append(1.3 * i, 20000 * i); |
| 36 | } |
| 37 | }}); |
| 38 | #if 0 |
| 39 | testVec.push_back({"500k double append", [](auto& log) { |
| 40 | wpi::log::DoubleLogEntry entry{log, "500k", 1}; |
| 41 | for (uint64_t i = 0; i < 500000; ++i) { |
| 42 | entry.Append(1.3 * i, 20000 * i); |
| 43 | } |
| 44 | }}); |
| 45 | #endif |
| 46 | testVec.push_back({"50 string append", [](auto& log) { |
| 47 | wpi::log::StringLogEntry entry{log, "string", 1}; |
| 48 | for (int i = 0; i < 50; ++i) { |
| 49 | entry.Append("hello", 20000 * i); |
| 50 | } |
| 51 | }}); |
| 52 | |
| 53 | testVec.push_back( |
| 54 | {"Double array append", [](auto& log) { |
| 55 | wpi::log::DoubleArrayLogEntry entry{log, "double_array", 1}; |
| 56 | entry.Append({1, 2, 3}, 20000); |
| 57 | entry.Append({4, 5}, 30000); |
| 58 | }}); |
| 59 | |
| 60 | testVec.push_back( |
| 61 | {"String array append", [](auto& log) { |
| 62 | wpi::log::StringArrayLogEntry entry{log, "string_array", 1}; |
| 63 | entry.Append({"Hello", "World"}, 20000); |
| 64 | entry.Append({"This", "Is", "Fun"}, 30000); |
| 65 | }}); |
| 66 | |
| 67 | for (const auto& [name, fn] : testVec) { |
| 68 | auto resVec = std::vector<microseconds::rep>(); |
| 69 | fmt::print("{}: ", name); |
| 70 | |
| 71 | for (int i = 0; i < kNumRuns; ++i) { |
| 72 | auto start = high_resolution_clock::now(); |
| 73 | fn(log); |
| 74 | auto stop = high_resolution_clock::now(); |
| 75 | resVec.push_back(duration_cast<microseconds>(stop - start).count()); |
| 76 | } |
| 77 | |
| 78 | fmt::print("{}us\n", |
| 79 | std::accumulate(resVec.begin(), resVec.end(), 0) / kNumRuns); |
| 80 | } |
| 81 | |
| 82 | return EXIT_SUCCESS; |
| 83 | } |