blob: ee9925ec9254ebf548b25b0d56987e9f6a961948 [file] [log] [blame]
// Copyright 2020 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef SRC_TESTS_BENCHMARKS_FIDL_LLCPP_ENCODE_BENCHMARK_UTIL_H_
#define SRC_TESTS_BENCHMARKS_FIDL_LLCPP_ENCODE_BENCHMARK_UTIL_H_
#include <lib/fidl/llcpp/coding.h>
#include <zircon/status.h>
#include <zircon/types.h>
#include <algorithm>
#include <type_traits>
#include <perftest/perftest.h>
namespace llcpp_benchmarks {
template <typename BuilderFunc>
bool EncodeBenchmark(perftest::RepeatState* state, BuilderFunc builder) {
using FidlType = std::invoke_result_t<BuilderFunc>;
static_assert(fidl::IsFidlType<FidlType>::value, "FIDL type required");
state->DeclareStep("Setup/WallTime");
state->DeclareStep("Encode/WallTime");
state->DeclareStep("Teardown/WallTime");
while (state->KeepRunning()) {
fidl::aligned<FidlType> aligned_value = builder();
state->NextStep(); // End: Setup. Begin: Encode.
{
auto encoded = ::fidl::internal::LinearizedAndEncoded<FidlType>(&aligned_value.value);
auto& encode_result = encoded.result();
ZX_ASSERT(encode_result.status == ZX_OK && encode_result.error == nullptr);
}
state->NextStep(); // End: Encode. Begin: Teardown.
}
return true;
}
} // namespace llcpp_benchmarks
#endif // SRC_TESTS_BENCHMARKS_FIDL_LLCPP_ENCODE_BENCHMARK_UTIL_H_