Skip to content

Commit 878cb81

Browse files
authored
Add add_load_handle_unload + build_and_load_runtime bench (#91)
Signed-off-by: Ludvig Liljenberg <4257730+ludfjig@users.noreply.github.com>
1 parent 7f78ad3 commit 878cb81

1 file changed

Lines changed: 62 additions & 1 deletion

File tree

src/hyperlight-js/benches/benchmarks.rs

Lines changed: 62 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,63 @@ 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("build_and_load_runtime", |b: &mut Bencher<'_>| {
310+
b.iter(|| {
311+
SandboxBuilder::new()
312+
.build()
313+
.unwrap()
314+
.load_runtime()
315+
.unwrap()
316+
});
317+
});
318+
319+
group.bench_function("add_load_handle_unload", |b: &mut Bencher<'_>| {
320+
b.iter_custom(|iterations| {
321+
let mut js_sandbox = SandboxBuilder::new()
322+
.build()
323+
.unwrap()
324+
.load_runtime()
325+
.unwrap();
326+
327+
let mut elapsed = Duration::ZERO;
328+
for _ in 0..iterations {
329+
let start = Instant::now();
330+
js_sandbox.add_handler("handler", handler.clone()).unwrap();
331+
let mut loaded = js_sandbox.get_loaded_sandbox().unwrap();
332+
let result = loaded
333+
.handle_event("handler", event.to_string(), Some(true))
334+
.unwrap();
335+
js_sandbox = loaded.unload().unwrap();
336+
elapsed += start.elapsed();
337+
338+
let parsed: serde_json::Value = serde_json::from_str(&result).unwrap();
339+
assert_eq!(parsed["request"]["uri"], "/redirected.html");
340+
}
341+
elapsed
342+
});
343+
});
344+
345+
group.finish();
346+
}
347+
287348
// =============================================================================
288349
// Monitor overhead benchmark
289350
// =============================================================================
@@ -389,7 +450,7 @@ fn monitor_overhead_benchmark(c: &mut Criterion) {
389450
criterion_group! {
390451
name = benches;
391452
config = Criterion::default().measurement_time(Duration::from_secs(20));
392-
targets = js_load_handler_benchmark, handle_events_benchmark
453+
targets = js_load_handler_benchmark, handle_events_benchmark, full_lifecycle_benchmark
393454
}
394455

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

0 commit comments

Comments
 (0)