@@ -14,6 +14,12 @@ namespace {
1414struct ApiHeaders : public benchmark ::Fixture {
1515 virtual ~ApiHeaders () noexcept (true ) {}
1616
17+ struct Header {
18+ bool append;
19+ jsg::ByteString name;
20+ jsg::ByteString 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" _bs, " example.com" _bs},
55+ Header{false , " User-Agent" _bs,
56+ " Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36" _bs},
57+ Header{false , " Accept" _bs,
58+ " text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" _bs},
59+ Header{false , " Accept-Language" _bs, " en-US,en;q=0.9" _bs},
60+ Header{false , " Accept-Encoding" _bs, " gzip, deflate, br" _bs},
61+ Header{false , " Content-Type" _bs, " application/json; charset=utf-8" _bs},
62+ Header{false , " Authorization" _bs,
63+ " Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0" _bs},
64+ Header{false , " Cache-Control" _bs, " no-cache, no-store, must-revalidate" _bs},
65+ Header{false , " Content-Length" _bs, " 1234" _bs},
66+ Header{false , " Referer" _bs, " https://www.example.com/page?query=value&other=param" _bs},
67+ Header{false , " X-Forwarded-For" _bs, " 203.0.113.1, 198.51.100.17" _bs},
68+ Header{true , " Set-Cookie" _bs, " new_session=token123; Path=/; Secure; HttpOnly" _bs},
69+ Header{true , " Set-Cookie" _bs, " new_session=token124; Path=/abc; Secure; HttpOnly" _bs}};
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
0 commit comments