Commit 683c7bb8 authored by Victor Zverovich's avatar Victor Zverovich Committed by Facebook Github Bot

Pass benchmark name as StringPiece

Summary: In cases where `addBenchmark` is called directly the name often resides in `std::string` which requires calling `c_str()`. Make it possible to pass `std::string` by making the function take `StringPiece` instead of `const char*`.

Reviewed By: rhodo

Differential Revision: D19507553

fbshipit-source-id: 49dd2bb77daefffeda3fc2153215de77b530f214
parent 6aecd684
......@@ -107,10 +107,10 @@ size_t getGlobalBenchmarkBaselineIndex() {
void detail::addBenchmarkImpl(
const char* file,
const char* name,
StringPiece name,
BenchmarkFun fun,
bool useCounter) {
benchmarks().push_back({file, name, std::move(fun), useCounter});
benchmarks().push_back({file, name.str(), std::move(fun), useCounter});
}
static std::pair<double, UserCounters> runBenchmarkGetNSPerIteration(
......
......@@ -18,6 +18,7 @@
#include <folly/Portability.h>
#include <folly/Preprocessor.h> // for FB_ANONYMOUS_VARIABLE
#include <folly/Range.h>
#include <folly/ScopeGuard.h>
#include <folly/Traits.h>
#include <folly/functional/Invoke.h>
......@@ -96,7 +97,7 @@ struct BenchmarkResult {
*/
void addBenchmarkImpl(
const char* file,
const char* name,
StringPiece name,
BenchmarkFun,
bool useCounter);
......@@ -188,7 +189,7 @@ struct BenchmarkSuspender {
*/
template <typename Lambda>
typename std::enable_if<folly::is_invocable<Lambda, unsigned>::value>::type
addBenchmark(const char* file, const char* name, Lambda&& lambda) {
addBenchmark(const char* file, StringPiece name, Lambda&& lambda) {
auto execute = [=](unsigned int times) {
BenchmarkSuspender::timeSpent = {};
unsigned int niter;
......@@ -213,7 +214,7 @@ addBenchmark(const char* file, const char* name, Lambda&& lambda) {
*/
template <typename Lambda>
typename std::enable_if<folly::is_invocable<Lambda>::value>::type
addBenchmark(const char* file, const char* name, Lambda&& lambda) {
addBenchmark(const char* file, StringPiece name, Lambda&& lambda) {
addBenchmark(file, name, [=](unsigned int times) {
unsigned int niter = 0;
while (times-- > 0) {
......@@ -230,7 +231,7 @@ addBenchmark(const char* file, const char* name, Lambda&& lambda) {
template <typename Lambda>
typename std::enable_if<
folly::is_invocable<Lambda, UserCounters&, unsigned>::value>::type
addBenchmark(const char* file, const char* name, Lambda&& lambda) {
addBenchmark(const char* file, StringPiece name, Lambda&& lambda) {
auto execute = [=](unsigned int times) {
BenchmarkSuspender::timeSpent = {};
unsigned int niter;
......@@ -254,7 +255,7 @@ addBenchmark(const char* file, const char* name, Lambda&& lambda) {
template <typename Lambda>
typename std::enable_if<folly::is_invocable<Lambda, UserCounters&>::value>::type
addBenchmark(const char* file, const char* name, Lambda&& lambda) {
addBenchmark(const char* file, StringPiece name, Lambda&& lambda) {
addBenchmark(file, name, [=](UserCounters& counters, unsigned int times) {
unsigned int niter = 0;
while (times-- > 0) {
......
......@@ -362,6 +362,7 @@ BENCHMARK(BenchmarkSuspender_dismissing_value, iter) {
int main(int argc, char** argv) {
gflags::ParseCommandLineFlags(&argc, &argv, true);
folly::addBenchmark("-", std::string("string_name"), [] { return 0; });
runBenchmarks();
runBenchmarksOnFlag();
}
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