diff --git a/rbx_binary/Cargo.toml b/rbx_binary/Cargo.toml index 840fc7f34..b053bfff0 100644 --- a/rbx_binary/Cargo.toml +++ b/rbx_binary/Cargo.toml @@ -27,16 +27,12 @@ profiling = "1.0.6" zstd = "0.13.2" [dev-dependencies] -criterion = "0.3.5" +criterion = "0.5.1" env_logger = "0.9.0" heck = "0.4.0" insta = { version = "1.14.1", features = ["yaml"] } serde = { version = "1.0.137", features = ["derive"] } [[bench]] -name = "deserializer" -harness = false - -[[bench]] -name = "serializer" +name = "suite" harness = false diff --git a/rbx_binary/bench-files/parts-10000.rbxm b/rbx_binary/bench-files/parts-10000.rbxm deleted file mode 100644 index 302791a18..000000000 Binary files a/rbx_binary/bench-files/parts-10000.rbxm and /dev/null differ diff --git a/rbx_binary/benches/deserializer.rs b/rbx_binary/benches/deserializer.rs deleted file mode 100644 index 428bac769..000000000 --- a/rbx_binary/benches/deserializer.rs +++ /dev/null @@ -1,55 +0,0 @@ -use criterion::{criterion_group, criterion_main, Criterion}; - -pub fn de_folders_100(c: &mut Criterion) { - static BUFFER: &[u8] = include_bytes!("../bench-files/folders-100.rbxm"); - - c.bench_function("Deserialize 100 Folders", |b| { - b.iter(|| { - deserialize_bench(BUFFER); - }); - }); -} - -pub fn de_deep_folders_100(c: &mut Criterion) { - static BUFFER: &[u8] = include_bytes!("../bench-files/deep-folders-100.rbxm"); - - c.bench_function("Deserialize 100 deeply nested Folders", |b| { - b.iter(|| { - deserialize_bench(BUFFER); - }); - }); -} - -pub fn de_modulescripts_100_lines_100(c: &mut Criterion) { - static BUFFER: &[u8] = include_bytes!("../bench-files/modulescripts-100-lines-100.rbxm"); - - c.bench_function("Deserialize 100 100-line ModuleScripts", |b| { - b.iter(|| { - deserialize_bench(BUFFER); - }); - }); -} - -pub fn de_parts_10000(c: &mut Criterion) { - static BUFFER: &[u8] = include_bytes!("../bench-files/parts-10000.rbxm"); - - c.bench_function("Deserialize 10,000 Parts", |b| { - b.iter(|| { - deserialize_bench(BUFFER); - }); - }); -} - -#[inline(always)] -fn deserialize_bench(buffer: &[u8]) { - rbx_binary::from_reader(buffer).unwrap(); -} - -criterion_group!( - deserializer, - de_folders_100, - de_deep_folders_100, - de_modulescripts_100_lines_100, - de_parts_10000 -); -criterion_main!(deserializer); diff --git a/rbx_binary/bench-files/deep-folders-100.rbxm b/rbx_binary/benches/files/deep-folders-100.rbxm similarity index 100% rename from rbx_binary/bench-files/deep-folders-100.rbxm rename to rbx_binary/benches/files/deep-folders-100.rbxm diff --git a/rbx_binary/bench-files/folders-100.rbxm b/rbx_binary/benches/files/folders-100.rbxm similarity index 100% rename from rbx_binary/bench-files/folders-100.rbxm rename to rbx_binary/benches/files/folders-100.rbxm diff --git a/rbx_binary/bench-files/modulescripts-100-lines-100.rbxm b/rbx_binary/benches/files/modulescripts-100-lines-100.rbxm similarity index 100% rename from rbx_binary/bench-files/modulescripts-100-lines-100.rbxm rename to rbx_binary/benches/files/modulescripts-100-lines-100.rbxm diff --git a/rbx_binary/benches/files/parts-1000.rbxm b/rbx_binary/benches/files/parts-1000.rbxm new file mode 100644 index 000000000..3ef7f4e33 Binary files /dev/null and b/rbx_binary/benches/files/parts-1000.rbxm differ diff --git a/rbx_binary/benches/serializer.rs b/rbx_binary/benches/serializer.rs deleted file mode 100644 index 367517fd8..000000000 --- a/rbx_binary/benches/serializer.rs +++ /dev/null @@ -1,48 +0,0 @@ -use criterion::{criterion_group, criterion_main, Criterion}; - -use rbx_dom_weak::{InstanceBuilder, WeakDom}; - -pub fn ser_folders_100(c: &mut Criterion) { - let mut tree = WeakDom::new(InstanceBuilder::new("Folder").with_name("Container")); - let root_ref = tree.root_ref(); - - for i in 0..99 { - tree.insert( - root_ref, - InstanceBuilder::new("Folder").with_name(format!("Folder {}", i)), - ); - } - - let mut buffer = Vec::new(); - - // Encode once into the buffer to pre-size it. - rbx_binary::to_writer(&mut buffer, &tree, &[root_ref]).unwrap(); - buffer.clear(); - - c.bench_function("Serialize 100 Folders", |b| { - b.iter(|| { - rbx_binary::to_writer(&mut buffer, &tree, &[root_ref]).unwrap(); - buffer.clear(); - }); - }); -} - -pub fn ser_parts_10000(c: &mut Criterion) { - static BUFFER: &[u8] = include_bytes!("../bench-files/parts-10000.rbxm"); - let tree = rbx_binary::from_reader(BUFFER).unwrap(); - let root_ref = tree.root_ref(); - - let mut buffer = Vec::new(); - rbx_binary::to_writer(&mut buffer, &tree, &[root_ref]).unwrap(); - buffer.clear(); - - c.bench_function("Serialize 10,000 Parts", |b| { - b.iter(|| { - rbx_binary::to_writer(&mut buffer, &tree, &[root_ref]).unwrap(); - buffer.clear(); - }) - }); -} - -criterion_group!(serializer, ser_folders_100, ser_parts_10000); -criterion_main!(serializer); diff --git a/rbx_binary/benches/suite/main.rs b/rbx_binary/benches/suite/main.rs new file mode 100644 index 000000000..a1e88e0de --- /dev/null +++ b/rbx_binary/benches/suite/main.rs @@ -0,0 +1,43 @@ +mod util; + +use crate::util::bench; +use criterion::{criterion_group, criterion_main, Criterion, SamplingMode}; + +pub fn folders_100(c: &mut Criterion) { + bench( + &mut c.benchmark_group("100 Folders"), + include_bytes!("../files/folders-100.rbxm"), + ) +} + +pub fn deep_folders_100(c: &mut Criterion) { + bench( + &mut c.benchmark_group("100 Deep Folders"), + include_bytes!("../files/deep-folders-100.rbxm"), + ) +} + +pub fn modulescripts_100_lines_100(c: &mut Criterion) { + bench( + &mut c.benchmark_group("100 100-line ModuleScripts"), + include_bytes!("../files/modulescripts-100-lines-100.rbxm"), + ) +} + +pub fn parts_1000(c: &mut Criterion) { + bench( + c.benchmark_group("1,000 Parts") + .sampling_mode(SamplingMode::Flat), + include_bytes!("../files/parts-1000.rbxm"), + ) +} + +criterion_group!( + bench_suite, + folders_100, + deep_folders_100, + modulescripts_100_lines_100, + parts_1000, +); + +criterion_main!(bench_suite); diff --git a/rbx_binary/benches/suite/util.rs b/rbx_binary/benches/suite/util.rs new file mode 100644 index 000000000..8e835132d --- /dev/null +++ b/rbx_binary/benches/suite/util.rs @@ -0,0 +1,37 @@ +use criterion::{measurement::Measurement, BatchSize, BenchmarkGroup, Throughput}; + +pub(crate) fn bench(group: &mut BenchmarkGroup, bench_file: &'static [u8]) { + serialize_bench(group, bench_file); + deserialize_bench(group, bench_file); +} + +fn serialize_bench(group: &mut BenchmarkGroup, buffer: &[u8]) { + let tree = rbx_binary::from_reader(buffer).unwrap(); + let root_ref = tree.root_ref(); + let mut buffer = Vec::new(); + + rbx_binary::to_writer(&mut buffer, &tree, &[root_ref]).unwrap(); + let buffer_len = buffer.len(); + + group + .throughput(Throughput::Bytes(buffer_len as u64)) + .bench_function("Serialize", |b| { + b.iter_batched( + || Vec::with_capacity(buffer_len), + |mut buffer: Vec| { + rbx_binary::to_writer(&mut buffer, &tree, &[root_ref]).unwrap(); + }, + BatchSize::SmallInput, + ) + }); +} + +fn deserialize_bench(group: &mut BenchmarkGroup, buffer: &[u8]) { + group + .throughput(Throughput::Bytes(buffer.len() as u64)) + .bench_function("Deserialize", |bencher| { + bencher.iter(|| { + rbx_binary::from_reader(buffer).unwrap(); + }); + }); +}