Skip to content

Commit 153c3ef

Browse files
jasnellanonrig
authored andcommitted
Some of the benchmark updates from the headers change
1 parent b9d067e commit 153c3ef

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

src/workerd/tests/bench-api-headers.c++

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ namespace {
1414
struct ApiHeaders: public benchmark::Fixture {
1515
virtual ~ApiHeaders() noexcept(true) {}
1616

17+
struct Header {
18+
bool append;
19+
kj::StringPtr name;
20+
kj::StringPtr value;
21+
};
22+
1723
void SetUp(benchmark::State& state) noexcept(true) override {
1824
fixture = kj::heap<TestFixture>();
1925

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

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

86+
BENCHMARK_F(ApiHeaders, set_append)(benchmark::State& state) {
87+
fixture->runInIoContext([&](const TestFixture::Environment& env) {
88+
auto& js = env.js;
89+
for (auto _: state) {
90+
for (size_t i = 0; i < 1000; ++i) {
91+
auto headers = js.alloc<api::Headers>();
92+
// Set common headers with various representative lengths
93+
for (int n = 0; n < 13; n++) {
94+
auto& h = kHeaders[n];
95+
if (h.append) {
96+
headers->append(
97+
env.js, jsg::ByteString(kj::str(h.name)), jsg::ByteString(kj::str(h.value)));
98+
} else {
99+
headers->set(
100+
env.js, jsg::ByteString(kj::str(h.name)), jsg::ByteString(kj::str(h.value)));
101+
}
102+
}
103+
benchmark::DoNotOptimize(i);
104+
}
105+
}
106+
});
107+
}
64108
} // namespace
65109
} // namespace workerd

src/workerd/tests/bench-response.c++

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,11 @@ BENCHMARK_F(Response, arrayBufferBody)(benchmark::State& state) {
8888
BENCHMARK_F(Response, jsonResponse)(benchmark::State& state) {
8989
fixture->runInIoContext([&](const TestFixture::Environment& env) {
9090
auto& js = env.js;
91+
// Prepare object to serialize. Do this outside the loop to avoid measuring its repeated
92+
// construction cost. What we want to measure is just the cost of the api::Response::json_ call.
93+
auto obj = js.obj();
94+
obj.set(js, "key"_kj, js.str("value"_kj));
9195
for (auto _: state) {
92-
auto obj = js.obj();
93-
obj.set(js, js.str("key"_kj), js.str("value"_kj));
9496
benchmark::DoNotOptimize(api::Response::json_(js, obj, kj::none));
9597
}
9698
});

0 commit comments

Comments
 (0)