parselink-old/tests/msgpack/test_speed.cpp

95 lines
3.2 KiB
C++

#include "parselink/msgpack/token.h"
// #include "parselink/proto/message.h"
#include <chrono>
#include <cstdlib>
#include <fcntl.h>
#include <magic_enum.hpp>
#include <vector>
#include <fmt/format.h>
#include <fmt/ranges.h>
using namespace std::chrono_literals;
struct any {
template <typename T>
operator T() {}
};
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, 20> 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());
for (auto token : sp) {
fmt::print("token type: {} value: ",
magic_enum::enum_name(token.type()));
switch (token.type()) {
case msgpack::format::type::unsigned_int:
fmt::print(
"{}", *(token.template get<std::uint64_t>()));
break;
case msgpack::format::type::signed_int:
fmt::print("{}", *(token.template get<std::int64_t>()));
break;
case msgpack::format::type::boolean:
fmt::print("{}", *(token.template get<bool>()));
break;
case msgpack::format::type::string:
fmt::print("{}",
*(token.template get<std::string_view>()));
break;
case msgpack::format::type::binary:
fmt::print(
"{}", *(token.template get<
std::span<std::byte const>>()));
break;
case msgpack::format::type::map:
fmt::print("map ({} entries)",
token.template get<msgpack::map_desc>()->count);
break;
case msgpack::format::type::array:
fmt::print("array ({} entries)",
token.template get<msgpack::array_desc>()
->count);
break;
case msgpack::format::type::nil: fmt::print("(nil)"); break;
case msgpack::format::type::invalid:
fmt::print("(invalid)");
break;
}
fmt::print("\n");
}
}).map_error([&](auto err) {
fmt::print("Failed to read tokens: {}\n", (int)err);
});
fmt::print("Took {} us\n", (after - before) / 1us);
}