Skip to content

Bench Method

After setting up your VM environment and preparing your test suites, you can utilize the bench method to measure and analyze the performance of your bytecode execution with high precision.

Using TypeScript

For TypeScript, you can define your benchmarking logic by wrapping your execution targets with setup routines. It is recommended to use optimizeBytecode prior to benchmarking to ensure you are measuring the most optimized instructions.

The benchmark configuration accepts the following parameters:

  • targetTime: The target duration for benchmark execution in milliseconds (must be greater than zero)
  • bytes: The number of bytes processed per iteration (optional, used for throughput calculation)
  • samples: The number of sample iterations to collect (must be greater than zero)
ts
vm.tools().bench("test_bench")
  .bytes(512)
  .samples(20)
  .targetTime(100)
  .run(
    () => [0.5, 6.7, 8.9],
    (state) => vm.tools().blackBox(state)
  );

Using Rust

In Rust, you can leverage the bench tool through the VM tools module to configure byte sizes, initialize states, and run adaptive sample iterations. Always make sure your bytecode is optimized beforehand to get accurate and reliable performance metrics.

The benchmark configuration accepts the following parameters:

  • target_time(Duration): The target duration for benchmark execution (must be greater than zero)
  • bytes: The number of bytes processed per iteration (optional, used for throughput calculation)
  • samples: The number of sample iterations to collect (must be greater than zero)
rust
use std::time::Duration;

vm.tools().bench("test_bench")
  .bytes(1024)
  .samples(20)
  .target_time(Duration::from_millis(100))
  .run(
    || vec![0.5, 6.7, 8.9],
    |state| std::hint::black_box(state),
  );

INFO

Capability Required: no specific capability