Commit cd62e86f authored by Fuat Geleri's avatar Fuat Geleri Committed by Facebook Github Bot

Folly/Benchmark: Make user counters readable

Summary:
When a user counter is added to benchmarks, the counter's value is written as a bare integer.
This change gives chance to user to either still keep it reported as bare integer, a time value or a metric value.

Differential Revision: D14451341

fbshipit-source-id: 95e64a89c201bb385d9a403f3442fa70c2ac7e2a
parent fdf4feab
...@@ -322,7 +322,20 @@ class BenchmarkResultsPrinter { ...@@ -322,7 +322,20 @@ class BenchmarkResultsPrinter {
} }
for (auto const& name : counterNames_) { for (auto const& name : counterNames_) {
if (auto ptr = folly::get_ptr(datum.counters, name)) { if (auto ptr = folly::get_ptr(datum.counters, name)) {
printf(" %-*d", int(name.length()), *ptr); switch (ptr->type) {
case UserMetric::TIME:
printf(
" %-*s", int(name.length()), readableTime(*ptr, 2).c_str());
break;
case UserMetric::METRIC:
printf(
" %-*s",
int(name.length()),
metricReadable(*ptr, 2).c_str());
break;
default:
printf(" %-*d", int(name.length()), (int)*ptr);
}
} else { } else {
printf(" %-*s", int(name.length()), "NaN"); printf(" %-*s", int(name.length()), "NaN");
} }
......
...@@ -53,7 +53,23 @@ inline bool runBenchmarksOnFlag() { ...@@ -53,7 +53,23 @@ inline bool runBenchmarksOnFlag() {
return FLAGS_benchmark; return FLAGS_benchmark;
} }
using UserCounters = std::unordered_map<std::string, int>; struct UserMetric {
enum Type { CUSTOM, TIME, METRIC };
int value;
Type type{CUSTOM};
UserMetric() = default;
template <typename T>
/* implicit */ UserMetric(T val, Type typ = CUSTOM)
: value(static_cast<int>(val)), type(typ) {}
operator int() const {
return value;
}
operator int&() {
return value;
}
};
using UserCounters = std::unordered_map<std::string, UserMetric>;
namespace detail { namespace detail {
struct TimeIterData { struct TimeIterData {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment