Files
allwpilib/datalog/examples/writelog/writelog.cpp

84 lines
2.8 KiB
C++
Raw Normal View History

2020-03-21 20:43:34 -07:00
// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#include <chrono>
#include <numeric>
#include <string>
2024-09-20 17:43:39 -07:00
#include <utility>
2020-03-21 20:43:34 -07:00
#include <vector>
#include <wpi/print.h>
#include "wpi/datalog/DataLogBackgroundWriter.h"
2020-03-21 20:43:34 -07:00
int main(int argc, char** argv) {
using std::chrono::duration_cast;
using std::chrono::high_resolution_clock;
using std::chrono::microseconds;
int kNumRuns = 10;
if (argc == 2) {
kNumRuns = std::stoi(argv[1]);
}
wpi::log::DataLogBackgroundWriter log;
2020-03-21 20:43:34 -07:00
log.SetFilename("test.wpilog");
auto testVec =
std::vector<std::pair<std::string, void (*)(wpi::log::DataLog&)>>();
testVec.push_back({"50 double append", [](auto& log) {
wpi::log::DoubleLogEntry entry{log, "fifty", 1};
for (int i = 0; i < 50; ++i) {
entry.Append(1.3 * i, 20000 * i);
}
}});
#if 0
testVec.push_back({"500k double append", [](auto& log) {
wpi::log::DoubleLogEntry entry{log, "500k", 1};
for (uint64_t i = 0; i < 500000; ++i) {
entry.Append(1.3 * i, 20000 * i);
}
}});
#endif
testVec.push_back({"50 string append", [](auto& log) {
wpi::log::StringLogEntry entry{log, "string", 1};
for (int i = 0; i < 50; ++i) {
entry.Append("hello", 20000 * i);
}
}});
testVec.push_back({"Double array append", [](auto& log) {
wpi::log::DoubleArrayLogEntry entry{log, "double_array",
1};
entry.Append({1, 2, 3}, 20000);
entry.Append({4, 5}, 30000);
}});
2020-03-21 20:43:34 -07:00
testVec.push_back({"String array append", [](auto& log) {
wpi::log::StringArrayLogEntry entry{log, "string_array",
1};
entry.Append({"Hello", "World"}, 20000);
entry.Append({"This", "Is", "Fun"}, 30000);
}});
2020-03-21 20:43:34 -07:00
for (const auto& [name, fn] : testVec) {
auto resVec = std::vector<microseconds::rep>();
wpi::print("{}: ", name);
2020-03-21 20:43:34 -07:00
for (int i = 0; i < kNumRuns; ++i) {
auto start = high_resolution_clock::now();
fn(log);
auto stop = high_resolution_clock::now();
resVec.push_back(duration_cast<microseconds>(stop - start).count());
}
wpi::print("{}us\n",
2020-03-21 20:43:34 -07:00
std::accumulate(resVec.begin(), resVec.end(), 0) / kNumRuns);
}
return EXIT_SUCCESS;
}