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