Skip to content
File

Blob: src/workerd/tests/bench-global-scope.c++

1.2 KB
1// Copyright (c) 2023 Cloudflare, Inc.
2// Licensed under the Apache 2.0 license found in the LICENSE file or at:
3// https://opensource.org/licenses/Apache-2.0
4 
5#include <workerd/tests/bench-tools.h>
6#include <workerd/tests/test-fixture.h>
7 
8// A benchmark for GlobalScope functionality.
9 
10namespace workerd {
11namespace {
12 
13struct GlobalScopeBenchmark: public benchmark::Fixture {
14 virtual ~GlobalScopeBenchmark() noexcept(true) {}
15 
16 void SetUp(benchmark::State& state) noexcept(true) override {
17 TestFixture::SetupParams params = {.mainModuleSource = R"(
18 export default {
19 async fetch(request) {
20 return new Response("OK");
21 },
22 };
23 )"_kj};
24 fixture = kj::heap<TestFixture>(kj::mv(params));
25 }
26 
27 void TearDown(benchmark::State& state) noexcept(true) override {
28 fixture = nullptr;
29 }
30 
31 kj::Own<TestFixture> fixture;
32};
33 
34BENCHMARK_F(GlobalScopeBenchmark, request)(benchmark::State& state) {
35 for (auto _: state) {
36 for (size_t i = 0; i < 1000; ++i) {
37 benchmark::DoNotOptimize(
38 fixture->runRequest(kj::HttpMethod::POST, "http://www.example.com"_kj, "TEST"_kj));
39 benchmark::DoNotOptimize(i);
40 }
41 }
42}
43 
44} // namespace
45} // namespace workerd