1#include "barretenberg/common/throw_or_abort.hpp"
2#include "barretenberg/env/hardware_concurrency.hpp"
11#pragma GCC diagnostic ignored "-Wunused-result"
21 static auto bfd_str = std::getenv(
"BENCHMARK_FD");
22 int bfd = bfd_str ? (int)std::stoul(bfd_str) : -1;
23 if (bfd >= 0 && (fcntl(bfd, F_GETFD) == -1 || errno == EBADF)) {
24 throw_or_abort(
"fd is not open. Did you redirect in your shell?");
27 }
catch (std::exception
const& e) {
28 std::string inner_msg = e.what();
29 throw_or_abort(
"Invalid BENCHMARK_FD: " + inner_msg);
34template <
typename T,
typename Enable =
void>
struct TypeTraits;
36template <
typename T>
struct TypeTraits<T, typename std::enable_if<std::is_arithmetic<T>::value>::type> {
37 static const char* type;
44 static const char* type;
50 static const char* type;
56 static const char* type;
62std::string getCurrentTimestamp()
64 std::time_t now = std::time(
nullptr);
65 std::tm* now_tm = std::gmtime(&now);
67 strftime(buf,
sizeof(buf),
"%Y-%m-%dT%H:%M:%SZ", now_tm);
68 return std::string(buf);
71template <
typename T> std::string toString(
const T& value)
73 std::ostringstream oss;
78void appendToStream(std::ostringstream&)
83template <
typename K,
typename V,
typename... Args>
84void appendToStream(std::ostringstream& oss,
const K& key,
const V& value, Args... args)
86 oss <<
", \"" << key <<
"\": \"" << toString(value) <<
"\"";
87 appendToStream(oss, args...);
90template <
typename T,
typename... Args>
void write_benchmark(
const std::string& name,
const T& value, Args... args)
95 std::ostringstream oss;
96 oss <<
"{\"timestamp\": \"" << getCurrentTimestamp() <<
"\", "
97 <<
"\"name\": \"" << name <<
"\", "
99 <<
"\"value\": " << value <<
", "
100 <<
"\"threads\": " << env_hardware_concurrency();
102 appendToStream(oss, args...);
104 oss <<
"}" << std::endl;
105 const std::string& tmp = oss.str();
106 write((
int)bfd, tmp.c_str(), tmp.size());
Definition: benchmark.hpp:34