Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[nfc] api headers benchmark #1089

Merged
merged 2 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/workerd/tests/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,21 @@ wd_cc_benchmark(
)

wd_cc_benchmark(
name = "bench-headers",
srcs = ["bench-headers.c++"],
name = "bench-kj-headers",
srcs = ["bench-kj-headers.c++"],
deps = [
"@capnp-cpp//src/kj/compat:kj-http",
],
)

wd_cc_benchmark(
name = "bench-api-headers",
srcs = ["bench-api-headers.c++"],
deps = [
":test-fixture",
],
)

wd_cc_benchmark(
name = "bench-global-scope",
srcs = ["bench-global-scope.c++"],
Expand Down
61 changes: 61 additions & 0 deletions src/workerd/tests/bench-api-headers.c++
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright (c) 2023 Cloudflare, Inc.
// Licensed under the Apache 2.0 license found in the LICENSE file or at:
// https://opensource.org/licenses/Apache-2.0

#include <workerd/tests/bench-tools.h>
#include <workerd/tests/test-fixture.h>
#include <workerd/api/http.h>

// A benchmark for js Header class.

namespace workerd {
namespace {

struct ApiHeaders: public benchmark::Fixture {
virtual ~ApiHeaders() noexcept(true) {}


void SetUp(benchmark::State& state) noexcept(true) override {
fixture = kj::heap<TestFixture>();

kj::HttpHeaderTable::Builder builder;
builder.add("Host");
builder.add("Accept");
builder.add("Content-Type");
builder.add("Last-Modified");
table = builder.build();
kjHeaders = kj::heap<kj::HttpHeaders>(*table);
auto in = kj::heapString(
"GET /favicon.ico HTTP/1.1\r\n"
"Host: 0.0.0.0=5000\r\n"
"User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9) Gecko/2008061015 Firefox/3.0\r\n"
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n"
"Accept-Language: en-us,en;q=0.5\r\n"
"Accept-Encoding: gzip,deflate\r\n"
"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n"
"Keep-Alive: 300\r\n"
"Connection: keep-alive\r\n"
"\r\n");
KJ_EXPECT(kjHeaders->tryParseRequest(in.asArray()).is<kj::HttpHeaders::Request>());
}

void TearDown(benchmark::State& state) noexcept(true) override {
fixture = nullptr;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR LGTM overall, but can you explain what the virtual destructor and fixture = nullptr; statements are used for? I'd imagine that the virtual destructor means that just the benchmark::Fixture destructor is used and that fixture is set to nullptr so that kj::Own knows it can free it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

virtual destructor is needed to annotate it by noexcept. otherwise in our code base clang assumes it has a different exception specification.

I use fixture = null to force- destroy an Isolate. Not entirely sure what's the fixture lifecycle yet.

}

kj::Own<TestFixture> fixture;
kj::Own<kj::HttpHeaderTable> table;
kj::Own<kj::HttpHeaders> kjHeaders;
};

// initialization performs a lot of copying, benchmark it
BENCHMARK_F(ApiHeaders, constructor)(benchmark::State& state) {
fixture->runInIoContext([&](const TestFixture::Environment& env) {
for (auto _ : state) {
auto jsHeaders = jsg::alloc<api::Headers>(*kjHeaders, api::Headers::Guard::REQUEST);
}
});
}

} // namespace
} // namespace workerd
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
namespace workerd {
namespace {

struct Fixture: public benchmark::Fixture {
virtual ~Fixture() noexcept(true) {}
struct KjHeaders: public benchmark::Fixture {
virtual ~KjHeaders() noexcept(true) {}

void SetUp(benchmark::State& state) noexcept(true) override {
kj::HttpHeaderTable::Builder builder;
Expand All @@ -23,7 +23,7 @@ struct Fixture: public benchmark::Fixture {
kj::Own<kj::HttpHeaderTable> table;
};

BENCHMARK_F(Fixture, Parse)(benchmark::State& state) {
BENCHMARK_F(KjHeaders, Parse)(benchmark::State& state) {
for (auto _ : state) {
kj::HttpHeaders headers(*table);

Expand Down