Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
44 changes: 44 additions & 0 deletions src/workerd/tests/bench-api-headers.c++
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ namespace {
struct ApiHeaders: public benchmark::Fixture {
virtual ~ApiHeaders() noexcept(true) {}

struct Header {
bool append;
kj::StringPtr name;
kj::StringPtr value;
};

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

Expand Down Expand Up @@ -45,6 +51,22 @@ struct ApiHeaders: public benchmark::Fixture {
kj::Own<TestFixture> fixture;
kj::Own<kj::HttpHeaderTable> table;
kj::Own<kj::HttpHeaders> kjHeaders;
Header kHeaders[13] = {Header{false, "Host"_kj, "example.com"_kj},
Header{false, "User-Agent"_kj,
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"_kj},
Header{false, "Accept"_kj,
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"_kj},
Header{false, "Accept-Language"_kj, "en-US,en;q=0.9"_kj},
Header{false, "Accept-Encoding"_kj, "gzip, deflate, br"_kj},
Header{false, "Content-Type"_kj, "application/json; charset=utf-8"_kj},
Header{false, "Authorization"_kj,
"Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0"_kj},
Header{false, "Cache-Control"_kj, "no-cache, no-store, must-revalidate"_kj},
Header{false, "Content-Length"_kj, "1234"_kj},
Header{false, "Referer"_kj, "https://www.example.com/page?query=value&other=param"_kj},
Header{false, "X-Forwarded-For"_kj, "203.0.113.1, 198.51.100.17"_kj},
Header{true, "Set-Cookie"_kj, "new_session=token123; Path=/; Secure; HttpOnly"_kj},
Header{true, "Set-Cookie"_kj, "new_session=token124; Path=/abc; Secure; HttpOnly"_kj}};
};

// initialization performs a lot of copying, benchmark it
Expand All @@ -61,5 +83,27 @@ BENCHMARK_F(ApiHeaders, constructor)(benchmark::State& state) {
});
}

BENCHMARK_F(ApiHeaders, set_append)(benchmark::State& state) {
fixture->runInIoContext([&](const TestFixture::Environment& env) {
auto& js = env.js;
for (auto _: state) {
for (size_t i = 0; i < 1000; ++i) {
auto headers = js.alloc<api::Headers>();
// Set common headers with various representative lengths
for (int n = 0; n < 13; n++) {
auto& h = kHeaders[n];
if (h.append) {
headers->append(
env.js, jsg::ByteString(kj::str(h.name)), jsg::ByteString(kj::str(h.value)));
} else {
headers->set(
env.js, jsg::ByteString(kj::str(h.name)), jsg::ByteString(kj::str(h.value)));
}
}
benchmark::DoNotOptimize(i);
}
}
});
}
} // namespace
} // namespace workerd
6 changes: 4 additions & 2 deletions src/workerd/tests/bench-response.c++
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,11 @@ BENCHMARK_F(Response, arrayBufferBody)(benchmark::State& state) {
BENCHMARK_F(Response, jsonResponse)(benchmark::State& state) {
fixture->runInIoContext([&](const TestFixture::Environment& env) {
auto& js = env.js;
// Prepare object to serialize. Do this outside the loop to avoid measuring its repeated
// construction cost. What we want to measure is just the cost of the api::Response::json_ call.
auto obj = js.obj();
obj.set(js, "key"_kj, js.str("value"_kj));
for (auto _: state) {
auto obj = js.obj();
obj.set(js, js.str("key"_kj), js.str("value"_kj));
benchmark::DoNotOptimize(api::Response::json_(js, obj, kj::none));
}
});
Expand Down
Loading