diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
commit | 2aa4a82499d4becd2284cdb482213d541b8804dd (patch) | |
tree | b80bf8bf13c3766139fbacc530efd0dd9d54394c /third_party/rust/wasmparser-0.48.2/benches/benchmark.rs | |
parent | Initial commit. (diff) | |
download | firefox-upstream.tar.xz firefox-upstream.zip |
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/wasmparser-0.48.2/benches/benchmark.rs')
-rw-r--r-- | third_party/rust/wasmparser-0.48.2/benches/benchmark.rs | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/third_party/rust/wasmparser-0.48.2/benches/benchmark.rs b/third_party/rust/wasmparser-0.48.2/benches/benchmark.rs new file mode 100644 index 0000000000..ee67ce85e3 --- /dev/null +++ b/third_party/rust/wasmparser-0.48.2/benches/benchmark.rs @@ -0,0 +1,99 @@ +pub fn read_file_data(path: &PathBuf) -> Vec<u8> { + let mut data = Vec::new(); + let mut f = File::open(path).ok().unwrap(); + f.read_to_end(&mut data).unwrap(); + data +} + +const VALIDATOR_CONFIG: Option<ValidatingParserConfig> = Some(ValidatingParserConfig { + operator_config: OperatorValidatorConfig { + enable_threads: true, + enable_reference_types: true, + enable_simd: true, + enable_bulk_memory: true, + enable_multi_value: true, + }, +}); + +#[macro_use] +extern crate criterion; +extern crate wasmparser; + +use criterion::Criterion; +use wasmparser::{ + validate, OperatorValidatorConfig, Parser, ParserState, ValidatingParser, + ValidatingParserConfig, WasmDecoder, +}; + +use std::fs::{read_dir, File}; +use std::io::Read; +use std::path::PathBuf; + +fn read_all_wasm<'a, T>(mut d: T) +where + T: WasmDecoder<'a>, +{ + loop { + match *d.read() { + ParserState::Error(e) => panic!("unexpected error {:?}", e), + ParserState::EndWasm => return, + _ => (), + } + } +} + +fn it_works_benchmark(c: &mut Criterion) { + let mut data: Vec<Vec<u8>> = vec![]; + for entry in read_dir("tests").unwrap() { + let dir = entry.unwrap(); + if !dir.file_type().unwrap().is_file() { + continue; + } + data.push(read_file_data(&dir.path())); + } + c.bench_function("it works benchmark", move |b| { + for d in &mut data { + b.iter(|| read_all_wasm(Parser::new(d.as_slice()))); + } + }); +} + +fn validator_not_fails_benchmark(c: &mut Criterion) { + let mut data: Vec<Vec<u8>> = vec![]; + for entry in read_dir("tests").unwrap() { + let dir = entry.unwrap(); + if !dir.file_type().unwrap().is_file() { + continue; + } + data.push(read_file_data(&dir.path())); + } + c.bench_function("validator no fails benchmark", move |b| { + for d in &mut data { + b.iter(|| read_all_wasm(ValidatingParser::new(d.as_slice(), VALIDATOR_CONFIG))); + } + }); +} + +fn validate_benchmark(c: &mut Criterion) { + let mut data: Vec<Vec<u8>> = vec![vec![]]; + for entry in read_dir("tests").unwrap() { + let dir = entry.unwrap(); + if !dir.file_type().unwrap().is_file() { + continue; + } + data.push(read_file_data(&dir.path())); + } + c.bench_function("validate benchmark", move |b| { + for d in &mut data { + b.iter(|| validate(&d, VALIDATOR_CONFIG)); + } + }); +} + +criterion_group!( + benchmark, + it_works_benchmark, + validator_not_fails_benchmark, + validate_benchmark +); +criterion_main!(benchmark); |