Skip to content

Commit ab24a03

Browse files
committed
Add add_load_handle_unload bench
Signed-off-by: Ludvig Liljenberg <4257730+ludfjig@users.noreply.github.com>
1 parent 62fb435 commit ab24a03

1 file changed

Lines changed: 52 additions & 1 deletion

File tree

src/hyperlight-js/benches/benchmarks.rs

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
16+
17+
// this is benchmarks, assert macros are fine
18+
#![allow(clippy::disallowed_macros)]
19+
1620
use std::time::{Duration, Instant};
1721

1822
use criterion::{criterion_group, criterion_main, Bencher, Criterion};
@@ -284,6 +288,53 @@ fn handle_events_benchmark(c: &mut Criterion) {
284288
group.finish();
285289
}
286290

291+
fn full_lifecycle_benchmark(c: &mut Criterion) {
292+
let mut group = c.benchmark_group("full_lifecycle");
293+
294+
let handler = Script::from_content(
295+
r#"
296+
function handler(event) {
297+
event.request.uri = "/redirected.html";
298+
return event
299+
}"#,
300+
);
301+
302+
let event = r#"
303+
{
304+
"request": {
305+
"uri": "/index.html"
306+
}
307+
}"#;
308+
309+
group.bench_function("add_load_handle_unload", |b: &mut Bencher<'_>| {
310+
b.iter_custom(|iterations| {
311+
let mut js_sandbox = SandboxBuilder::new()
312+
.build()
313+
.unwrap()
314+
.load_runtime()
315+
.unwrap();
316+
317+
let mut elapsed = Duration::ZERO;
318+
for _ in 0..iterations {
319+
let start = Instant::now();
320+
js_sandbox.add_handler("handler", handler.clone()).unwrap();
321+
let mut loaded = js_sandbox.get_loaded_sandbox().unwrap();
322+
let result = loaded
323+
.handle_event("handler", event.to_string(), Some(true))
324+
.unwrap();
325+
js_sandbox = loaded.unload().unwrap();
326+
elapsed += start.elapsed();
327+
328+
let parsed: serde_json::Value = serde_json::from_str(&result).unwrap();
329+
assert_eq!(parsed["request"]["uri"], "/redirected.html");
330+
}
331+
elapsed
332+
});
333+
});
334+
335+
group.finish();
336+
}
337+
287338
// =============================================================================
288339
// Monitor overhead benchmark
289340
// =============================================================================
@@ -389,7 +440,7 @@ fn monitor_overhead_benchmark(c: &mut Criterion) {
389440
criterion_group! {
390441
name = benches;
391442
config = Criterion::default().measurement_time(Duration::from_secs(20));
392-
targets = js_load_handler_benchmark, handle_events_benchmark
443+
targets = js_load_handler_benchmark, handle_events_benchmark, full_lifecycle_benchmark
393444
}
394445

395446
#[cfg(all(feature = "monitor-wall-clock", feature = "monitor-cpu-time"))]

0 commit comments

Comments
 (0)