parselink-old/tests/msgpack/test_speed.cpp
Kurt Sassenrath a80b9d0fc6 token_reader::read_some, operator==, "speed test"
- Added token_reader::read_some(), which takes a view of a token buffer
  reads/unpacks up to that many tokens from a buffer. If an error occurs,
  or if there are fewer msgpack formats within the buffer, then the
  returned span will be a subspan of the original buffer. If no tokens
  were read before encountering an error, token_reader will return the
  error instead.
- token::operator== overload for types that might be stored within the
  token. Handy for cleaning up tests, even though both methods of
  `token::get<T>() == value` and `token == value` are represented for
  now.
- Added a simple tool to open a file containing msgpack content and read
  tokens into a buffer. This is for testing speed, but needs to be made
  into a proper benchmark once more work has been done. Pretty fast
  right now, only taking 2us to parse ~200 tokens (that takes the
  pythong msgpack implementation ~1s to handle).
2023-10-06 16:16:56 -07:00

49 lines
1.2 KiB
C++

#include "parselink/msgpack/token.h"
#include <fmt/format.h>
#include <chrono>
#include <cstdlib>
#include <fcntl.h>
#include <vector>
using namespace std::chrono_literals;
auto read_file(char const* filename) {
std::vector<std::byte> data;
auto fd = open(filename, O_RDONLY);
if (!fd) {
fmt::print("Could not open {}: {}\n", filename, strerror(errno));
return data;
}
auto size = lseek(fd, 0, SEEK_END);
data.resize(size);
lseek(fd, 0, SEEK_SET);
fmt::print("Reading {} bytes from {}\n", size, filename);
read(fd, data.data(), data.size());
close(fd);
return data;
}
int main(int argc, char** argv) {
std::array<msgpack::token, 4096> buf;
auto data = read_file(argc < 2 ? "output.bin" : argv[1]);
msgpack::token_reader reader(data);
auto before = std::chrono::steady_clock::now();
auto test = reader.read_some(buf);
auto after = std::chrono::steady_clock::now();
test.map([&](auto sp){
fmt::print("Read {} tokens\n", sp.size());
}).map_error([&](auto err){
fmt::print("Failed to read tokens: {}\n", (int)err);
});
fmt::print("Took {} us\n", (after - before) / 1us);
}