@@ -13,6 +13,10 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313See the License for the specific language governing permissions and
1414limitations under the License.
1515*/
16+
17+ // this is benchmarks, assert macros are fine
18+ #![ allow( clippy:: disallowed_macros) ]
19+
1620use std:: time:: { Duration , Instant } ;
1721
1822use 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) {
389440criterion_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