| 1 |
#![feature(test)] |
| 2 |
/// Unfortunately, `cargo bench` does not permit running these benchmarks out-of-the-box |
| 3 |
/// on a WebAssembly virtual machine. |
| 4 |
/// |
| 5 |
/// Follow these steps to run these benchmarks: |
| 6 |
/// 0. Prerequisites: Install the `wasm32-wasi` target and `wasmer` |
| 7 |
/// |
| 8 |
/// |
| 9 |
/// 1. Build these benchmarks |
| 10 |
/// ```bash |
| 11 |
/// cargo build --bench=bench_rustfft_wasm_simd --release --target wasm32-wasi --features "wasm_simd" |
| 12 |
/// ``` |
| 13 |
/// |
| 14 |
/// After cargo built the bench binary, cargo stores it inside the |
| 15 |
/// `<PROJECT_ROOT>/target/wasm32-wasi/release/deps` directory. |
| 16 |
/// The file name of this binary follows this format: `bench_rustfft_wasm_simd-<CHECKSUM>.wasm`. |
| 17 |
/// For instance, it could be named |
| 18 |
/// `target/wasm32-wasi/release/deps/bench_rustfft_scalar-6d2b3d5a567416f5.wasm` |
| 19 |
/// |
| 20 |
/// 2. Copy the most recently built WASM binary to hex.wasm |
| 21 |
/// ```bash |
| 22 |
/// cp `ls -t target/wasm32-wasi/release/deps/*.wasm | head -n 1` hex.wasm |
| 23 |
/// ``` |
| 24 |
/// |
| 25 |
/// 3. Run these benchmark e. g. with [wasmer](https://github.com/wasmerio/wasmer) |
| 26 |
/// ```bash |
| 27 |
/// wasmer run --dir=. hex.wasm -- --bench |
| 28 |
/// ``` |
| 29 |
/// |
| 30 |
/// For more information, refer to [Criterion's user guide](https://github.com/bheisler/criterion.rs/blob/dc2b06cd31f7aa34cff6a83a00598e0523186dad/book/src/user_guide/wasi.md) |
| 31 |
/// which should be mostly applicable to our use case. |
| 32 |
extern crate rustfft; |
| 33 |
extern crate test; |
| 34 |
|
| 35 |
use paste::paste; |
| 36 |
use rustfft::num_complex::Complex; |
| 37 |
use rustfft::num_traits::Zero; |
| 38 |
use rustfft::Fft; |
| 39 |
use std::sync::Arc; |
| 40 |
use test::Bencher; |
| 41 |
|
| 42 |
// Make fft using scalar planner |
| 43 |
fn bench_scalar_32(b: &mut Bencher, len: usize) { |
| 44 |
let mut planner = rustfft::FftPlannerScalar::new(); |
| 45 |
let fft: Arc<dyn Fft<f32>> = planner.plan_fft_forward(len); |
| 46 |
|
| 47 |
let mut buffer: Vec<Complex<f32>> = vec![Complex::zero(); len]; |
| 48 |
let mut scratch: Vec<Complex<f32>> = vec![Complex::zero(); fft.get_inplace_scratch_len()]; |
| 49 |
b.iter(|| { |
| 50 |
fft.process_with_scratch(&mut buffer, &mut scratch); |
| 51 |
}); |
| 52 |
} |
| 53 |
|
| 54 |
// Make fft using scalar planner |
| 55 |
fn bench_scalar_64(b: &mut Bencher, len: usize) { |
| 56 |
let mut planner = rustfft::FftPlannerScalar::new(); |
| 57 |
let fft: Arc<dyn Fft<f64>> = planner.plan_fft_forward(len); |
| 58 |
|
| 59 |
let mut buffer: Vec<Complex<f64>> = vec![Complex::zero(); len]; |
| 60 |
let mut scratch: Vec<Complex<f64>> = vec![Complex::zero(); fft.get_inplace_scratch_len()]; |
| 61 |
b.iter(|| { |
| 62 |
fft.process_with_scratch(&mut buffer, &mut scratch); |
| 63 |
}); |
| 64 |
} |
| 65 |
|
| 66 |
// Make fft using WASM SIMD planner |
| 67 |
fn bench_wasmsimd_32(b: &mut Bencher, len: usize) { |
| 68 |
let mut planner = rustfft::FftPlannerWasmSimd::new().unwrap(); |
| 69 |
let fft: Arc<dyn Fft<f32>> = planner.plan_fft_forward(len); |
| 70 |
|
| 71 |
let mut buffer: Vec<Complex<f32>> = vec![Complex::zero(); len]; |
| 72 |
let mut scratch: Vec<Complex<f32>> = vec![Complex::zero(); fft.get_inplace_scratch_len()]; |
| 73 |
b.iter(|| { |
| 74 |
fft.process_with_scratch(&mut buffer, &mut scratch); |
| 75 |
}); |
| 76 |
} |
| 77 |
|
| 78 |
// Make fft using WASM SIMD planner |
| 79 |
fn bench_wasmsimd_64(b: &mut Bencher, len: usize) { |
| 80 |
let mut planner = rustfft::FftPlannerWasmSimd::new().unwrap(); |
| 81 |
let fft: Arc<dyn Fft<f64>> = planner.plan_fft_forward(len); |
| 82 |
|
| 83 |
let mut buffer: Vec<Complex<f64>> = vec![Complex::zero(); len]; |
| 84 |
let mut scratch: Vec<Complex<f64>> = vec![Complex::zero(); fft.get_inplace_scratch_len()]; |
| 85 |
b.iter(|| { |
| 86 |
fft.process_with_scratch(&mut buffer, &mut scratch); |
| 87 |
}); |
| 88 |
} |
| 89 |
|
| 90 |
// Create benches using functions taking one argument |
| 91 |
macro_rules! make_benches { |
| 92 |
($name:ident, { $($len:literal),* }) => { |
| 93 |
paste! { |
| 94 |
$( |
| 95 |
#[bench] |
| 96 |
fn [<$name _ $len _f32_scalar>](b: &mut Bencher) { |
| 97 |
[<bench_scalar_32>](b, $len); |
| 98 |
} |
| 99 |
|
| 100 |
#[bench] |
| 101 |
fn [<$name _ $len _f64_scalar>](b: &mut Bencher) { |
| 102 |
[<bench_scalar_64>](b, $len); |
| 103 |
} |
| 104 |
|
| 105 |
#[bench] |
| 106 |
fn [<$name _ $len _f32_wasmsimd>](b: &mut Bencher) { |
| 107 |
[<bench_wasmsimd_32>](b, $len); |
| 108 |
} |
| 109 |
|
| 110 |
#[bench] |
| 111 |
fn [<$name _ $len _f64_wasmsimd>](b: &mut Bencher) { |
| 112 |
[<bench_wasmsimd_64>](b, $len); |
| 113 |
} |
| 114 |
)* |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
make_benches!(wasmsimdcomparison, {4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072}); |
| 120 |
make_benches!(wasmsimdcomparison, { 262144, 524288, 1048576, 2097152, 4194304 }); |