File
Blob: src/workerd/tests/bench-util.c++
| 1 | // Copyright (c) 2023 Cloudflare, Inc. |
| 2 | // Licensed under the Apache 2.0 license found in the LICENSE file or at: |
| 3 | // https://opensource.org/licenses/Apache-2.0 |
| 4 | |
| 5 | #include <workerd/jsg/jsg.h> |
| 6 | #include <workerd/jsg/setup.h> |
| 7 | #include <workerd/jsg/util.h> |
| 8 | #include <workerd/tests/bench-tools.h> |
| 9 | #include <workerd/tests/test-fixture.h> |
| 10 | |
| 11 | namespace workerd { |
| 12 | namespace { |
| 13 | |
| 14 | // Ref: https://developers.cloudflare.com/workers/runtime-apis/request/#the-cf-property-requestinitcfproperties |
| 15 | static constexpr kj::StringPtr cfProperty = R"DATA({ |
| 16 | "apps": false, |
| 17 | "cacheEverything": false, |
| 18 | "cacheKey": "my-cache-key", |
| 19 | "cacheTags": ["production", "development"], |
| 20 | "cacheTtl": 3600, |
| 21 | "cacheTtlByStatus": { "200-299": 86400, "404": 1, "500-599": 0 }, |
| 22 | "image": null, |
| 23 | "mirage": true, |
| 24 | "polish": "lossless", |
| 25 | "scrapeShield": true, |
| 26 | "webp": false |
| 27 | })DATA"_kjc; |
| 28 | |
| 29 | static void Util_RecursivelyFreeze(benchmark::State& state) { |
| 30 | TestFixture fixture; |
| 31 | fixture.runInIoContext([&](const TestFixture::Environment& env) { |
| 32 | auto& js = env.js; |
| 33 | auto obj = jsg::check(v8::JSON::Parse(js.v8Context(), jsg::v8Str(js.v8Isolate, cfProperty))); |
| 34 | |
| 35 | for (auto _: state) { |
| 36 | for (size_t i = 0; i < 1000; ++i) { |
| 37 | jsg::recursivelyFreeze(js.v8Context(), obj); |
| 38 | benchmark::DoNotOptimize(i); |
| 39 | } |
| 40 | } |
| 41 | }); |
| 42 | } |
| 43 | |
| 44 | WD_BENCHMARK(Util_RecursivelyFreeze); |
| 45 | |
| 46 | } // namespace |
| 47 | } // namespace workerd |