Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
benchmark.hpp
Go to the documentation of this file.
3#include <cstdlib>
4#include <ctime>
5#include <fcntl.h>
6#include <iostream>
7#include <sstream>
8#include <string>
9#include <unistd.h>
10
11#pragma GCC diagnostic ignored "-Wunused-result" // GCC13 hits this
12
13namespace {
19auto bfd = []() {
20 try {
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?");
25 }
26 return bfd;
27 } catch (std::exception const& e) {
28 std::string inner_msg = e.what();
29 throw_or_abort("Invalid BENCHMARK_FD: " + inner_msg);
30 }
31}();
32} // namespace
33
34template <typename T, typename Enable = void> struct TypeTraits;
35
36template <typename T> struct TypeTraits<T, typename std::enable_if<std::is_arithmetic<T>::value>::type> {
37 static const char* type;
38};
39
40template <typename T>
42
43template <> struct TypeTraits<std::string> {
44 static const char* type;
45};
46
47const char* TypeTraits<std::string>::type = "string";
48
49template <> struct TypeTraits<double> {
50 static const char* type;
51};
52
53const char* TypeTraits<double>::type = "number";
54
55template <> struct TypeTraits<bool> {
56 static const char* type;
57};
58
59const char* TypeTraits<bool>::type = "bool";
60
61// Helper function to get the current timestamp in the desired format
63{
64 std::time_t now = std::time(nullptr);
65 std::tm* now_tm = std::gmtime(&now);
66 char buf[21] = { 0 };
67 strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%SZ", now_tm);
68 return std::string(buf);
69}
70
71template <typename T> std::string toString(const T& value)
72{
73 std::ostringstream oss;
74 oss << value;
75 return oss.str();
76}
77
78void appendToStream(std::ostringstream&)
79{
80 // base case: do nothing
81}
82
83template <typename K, typename V, typename... Args>
84void appendToStream(std::ostringstream& oss, const K& key, const V& value, Args... args)
85{
86 oss << ", \"" << key << "\": \"" << toString(value) << "\"";
87 appendToStream(oss, args...); // recursively process the remaining arguments
88}
89
90template <typename T, typename... Args> void write_benchmark(const std::string& name, const T& value, Args... args)
91{
92 if (bfd == -1) {
93 return;
94 }
95 std::ostringstream oss;
96 oss << "{\"timestamp\": \"" << getCurrentTimestamp() << "\", "
97 << "\"eventName\": \"" << name << "\", "
98 << "\"type\": \"" << TypeTraits<T>::type << "\", "
99 << "\"value\": " << value << ", "
100 << "\"threads\": " << env_hardware_concurrency();
101
102 appendToStream(oss, args...); // unpack and append the key-value pairs
103
104 oss << "}" << std::endl;
105 const std::string& tmp = oss.str();
106 write((int)bfd, tmp.c_str(), tmp.size());
107}
std::string toString(const T &value)
Definition benchmark.hpp:71
std::string getCurrentTimestamp()
Definition benchmark.hpp:62
void write_benchmark(const std::string &name, const T &value, Args... args)
Definition benchmark.hpp:90
void appendToStream(std::ostringstream &)
Definition benchmark.hpp:78
uint8_t const * buf
Definition data_store.hpp:9
void write(const T t)
Definition g1.test.cpp:409
uint32_t env_hardware_concurrency()
STL namespace.
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
static const char * type
Definition benchmark.hpp:56
static const char * type
Definition benchmark.hpp:50
static const char * type
Definition benchmark.hpp:44
void throw_or_abort(std::string const &err)