diff --git a/test/benchmarks/cpp/BUILD.gn b/test/benchmarks/cpp/BUILD.gn index 060f886f14..6c57952841 100644 --- a/test/benchmarks/cpp/BUILD.gn +++ b/test/benchmarks/cpp/BUILD.gn @@ -10,7 +10,10 @@ group("gn_all") { deps = [] if (v8_enable_google_benchmark) { - deps += [ ":empty_benchmark" ] + deps += [ + ":empty_benchmark", + "cppgc:gn_all", + ] } } diff --git a/test/benchmarks/cpp/cppgc/BUILD.gn b/test/benchmarks/cpp/cppgc/BUILD.gn new file mode 100644 index 0000000000..1c00cac913 --- /dev/null +++ b/test/benchmarks/cpp/cppgc/BUILD.gn @@ -0,0 +1,52 @@ +# Copyright 2020 The V8 project authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +import("../../../../gni/v8.gni") + +group("gn_all") { + testonly = true + + deps = [] + + if (v8_enable_google_benchmark) { + deps += [ ":cppgc_allocation_benchmark" ] + } +} + +if (v8_enable_google_benchmark) { + v8_source_set("cppgc_benchmark_support") { + testonly = true + + configs = [ + "../../../..:external_config", + "../../../..:internal_config_base", + "../../../..:cppgc_base_config", + ] + sources = [ + "../../../../test/unittests/heap/cppgc/test-platform.cc", + "../../../../test/unittests/heap/cppgc/test-platform.h", + "utils.h", + ] + deps = [ + "../../../..:cppgc_for_testing", + "//third_party/google_benchmark:benchmark_main", + ] + } + + v8_executable("cppgc_allocation_benchmark") { + testonly = true + + configs = [ + "../../../..:external_config", + "../../../..:internal_config_base", + "../../../..:cppgc_base_config", + ] + sources = [ "allocation_perf.cc" ] + deps = [ + ":cppgc_benchmark_support", + "../../../..:cppgc_for_testing", + "//third_party/google_benchmark:benchmark_main", + ] + } +} diff --git a/test/benchmarks/cpp/cppgc/DEPS b/test/benchmarks/cpp/cppgc/DEPS new file mode 100644 index 0000000000..d31d529ebe --- /dev/null +++ b/test/benchmarks/cpp/cppgc/DEPS @@ -0,0 +1,6 @@ +include_rules = [ + "+include/cppgc", + "+src/heap/cppgc", + "+test/unittests/heap/cppgc", + "+third_party/google_benchmark/src/include/benchmark/benchmark.h", +] diff --git a/test/benchmarks/cpp/cppgc/allocation_perf.cc b/test/benchmarks/cpp/cppgc/allocation_perf.cc new file mode 100644 index 0000000000..c5015ac9a2 --- /dev/null +++ b/test/benchmarks/cpp/cppgc/allocation_perf.cc @@ -0,0 +1,49 @@ +// Copyright 2020 the V8 project authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "include/cppgc/allocation.h" +#include "include/cppgc/garbage-collected.h" +#include "src/heap/cppgc/globals.h" +#include "src/heap/cppgc/heap.h" +#include "test/benchmarks/cpp/cppgc/utils.h" +#include "third_party/google_benchmark/src/include/benchmark/benchmark.h" + +namespace cppgc { +namespace internal { +namespace { + +using Allocate = testing::BenchmarkWithHeap; + +class TinyObject final : public cppgc::GarbageCollected { + public: + void Trace(cppgc::Visitor*) const {} +}; + +BENCHMARK_F(Allocate, Tiny)(benchmark::State& st) { + Heap::NoGCScope no_gc(*Heap::From(&heap())); + for (auto _ : st) { + benchmark::DoNotOptimize( + cppgc::MakeGarbageCollected(heap().GetAllocationHandle())); + } + st.SetBytesProcessed(st.iterations() * sizeof(TinyObject)); +} + +class LargeObject final : public GarbageCollected { + public: + void Trace(cppgc::Visitor*) const {} + char padding[kLargeObjectSizeThreshold + 1]; +}; + +BENCHMARK_F(Allocate, Large)(benchmark::State& st) { + Heap::NoGCScope no_gc(*Heap::From(&heap())); + for (auto _ : st) { + benchmark::DoNotOptimize( + cppgc::MakeGarbageCollected(heap().GetAllocationHandle())); + } + st.SetBytesProcessed(st.iterations() * sizeof(LargeObject)); +} + +} // namespace +} // namespace internal +} // namespace cppgc diff --git a/test/benchmarks/cpp/cppgc/utils.h b/test/benchmarks/cpp/cppgc/utils.h new file mode 100644 index 0000000000..b1e5992d44 --- /dev/null +++ b/test/benchmarks/cpp/cppgc/utils.h @@ -0,0 +1,41 @@ +// Copyright 2020 the V8 project 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 TEST_BENCHMARK_CPP_CPPGC_UTILS_H_ +#define TEST_BENCHMARK_CPP_CPPGC_UTILS_H_ + +#include "include/cppgc/heap.h" +#include "include/cppgc/platform.h" +#include "test/unittests/heap/cppgc/test-platform.h" +#include "third_party/google_benchmark/src/include/benchmark/benchmark.h" + +namespace cppgc { +namespace internal { +namespace testing { + +class BenchmarkWithHeap : public benchmark::Fixture { + protected: + void SetUp(const ::benchmark::State& state) override { + platform_ = std::make_shared(); + cppgc::InitializeProcess(platform_->GetPageAllocator()); + heap_ = cppgc::Heap::Create(platform_); + } + + void TearDown(const ::benchmark::State& state) override { + heap_.reset(); + cppgc::ShutdownProcess(); + } + + cppgc::Heap& heap() const { return *heap_.get(); } + + private: + std::shared_ptr platform_; + std::unique_ptr heap_; +}; + +} // namespace testing +} // namespace internal +} // namespace cppgc + +#endif // TEST_BENCHMARK_CPP_CPPGC_UTILS_H_