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 | |
| 11 | #include "fmt/format.h" |
| 12 | #include "wpi/DataLog.h" |
| 13 | |
| 14 | int main(int argc, char** argv) { |
| 15 | using std::chrono::duration_cast; |
| 16 | using std::chrono::high_resolution_clock; |
| 17 | using std::chrono::microseconds; |
| 18 | |
| 19 | int kNumRuns = 10; |
| 20 | |
| 21 | if (argc == 2) { |
| 22 | kNumRuns = std::stoi(argv[1]); |
| 23 | } |
| 24 | |
| 25 | wpi::log::DataLog log; |
| 26 | log.SetFilename("test.wpilog"); |
| 27 | |
| 28 | auto testVec = |
| 29 | std::vector<std::pair<std::string, void (*)(wpi::log::DataLog&)>>(); |
| 30 | |
| 31 | testVec.push_back({"50 double append", [](auto& log) { |
| 32 | wpi::log::DoubleLogEntry entry{log, "fifty", 1}; |
| 33 | for (int i = 0; i < 50; ++i) { |
| 34 | entry.Append(1.3 * i, 20000 * i); |
| 35 | } |
| 36 | }}); |
| 37 | #if 0 |
| 38 | testVec.push_back({"500k double append", [](auto& log) { |
| 39 | wpi::log::DoubleLogEntry entry{log, "500k", 1}; |
| 40 | for (uint64_t i = 0; i < 500000; ++i) { |
| 41 | entry.Append(1.3 * i, 20000 * i); |
| 42 | } |
| 43 | }}); |
| 44 | #endif |
| 45 | testVec.push_back({"50 string append", [](auto& log) { |
| 46 | wpi::log::StringLogEntry entry{log, "string", 1}; |
| 47 | for (int i = 0; i < 50; ++i) { |
| 48 | entry.Append("hello", 20000 * i); |
| 49 | } |
| 50 | }}); |
| 51 | |
| 52 | testVec.push_back( |
| 53 | {"Double array append", [](auto& log) { |
| 54 | wpi::log::DoubleArrayLogEntry entry{log, "double_array", 1}; |
| 55 | entry.Append({1, 2, 3}, 20000); |
| 56 | entry.Append({4, 5}, 30000); |
| 57 | }}); |
| 58 | |
| 59 | testVec.push_back( |
| 60 | {"String array append", [](auto& log) { |
| 61 | wpi::log::StringArrayLogEntry entry{log, "string_array", 1}; |
| 62 | entry.Append({"Hello", "World"}, 20000); |
| 63 | entry.Append({"This", "Is", "Fun"}, 30000); |
| 64 | }}); |
| 65 | |
| 66 | for (const auto& [name, fn] : testVec) { |
| 67 | auto resVec = std::vector<microseconds::rep>(); |
| 68 | fmt::print("{}: ", name); |
| 69 | |
| 70 | for (int i = 0; i < kNumRuns; ++i) { |
| 71 | auto start = high_resolution_clock::now(); |
| 72 | fn(log); |
| 73 | auto stop = high_resolution_clock::now(); |
| 74 | resVec.push_back(duration_cast<microseconds>(stop - start).count()); |
| 75 | } |
| 76 | |
| 77 | fmt::print("{}us\n", |
| 78 | std::accumulate(resVec.begin(), resVec.end(), 0) / kNumRuns); |
| 79 | } |
| 80 | |
| 81 | return EXIT_SUCCESS; |
| 82 | } |