Skip to content

Commit

Permalink
Move throughput calls to utils
Browse files Browse the repository at this point in the history
Serialization would not be accurate before, because the serialized
size will not necessarily the same as the input file
  • Loading branch information
kennethloeffler committed Oct 31, 2024
1 parent ddc847a commit 1261e9b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 36 deletions.
26 changes: 9 additions & 17 deletions rbx_binary/benches/suite/main.rs
Original file line number Diff line number Diff line change
@@ -1,42 +1,34 @@
mod util;

use crate::util::bench;
use criterion::{criterion_group, criterion_main, Criterion, SamplingMode, Throughput};
use criterion::{criterion_group, criterion_main, Criterion, SamplingMode};

pub fn folders_100(c: &mut Criterion) {
let bytes = include_bytes!("../files/folders-100.rbxm");
bench(
c.benchmark_group("100 Folders")
.throughput(Throughput::Bytes(bytes.len() as u64)),
bytes,
&mut c.benchmark_group("100 Folders"),
include_bytes!("../files/folders-100.rbxm"),
)
}

pub fn deep_folders_100(c: &mut Criterion) {
let bytes = include_bytes!("../files/deep-folders-100.rbxm");
bench(
c.benchmark_group("100 Deep Folders")
.throughput(Throughput::Bytes(bytes.len() as u64)),
bytes,
&mut c.benchmark_group("100 Deep Folders"),
include_bytes!("../files/deep-folders-100.rbxm"),
)
}

pub fn modulescripts_100_lines_100(c: &mut Criterion) {
let bytes = include_bytes!("../files/modulescripts-100-lines-100.rbxm");
bench(
c.benchmark_group("100 100-line ModuleScripts")
.throughput(Throughput::Bytes(bytes.len() as u64)),
bytes,
&mut c.benchmark_group("100 100-line ModuleScripts"),
include_bytes!("../files/modulescripts-100-lines-100.rbxm"),
)
}

pub fn parts_1000(c: &mut Criterion) {
let bytes = include_bytes!("../files/parts-1000.rbxm");
bench(
c.benchmark_group("1,000 Parts")
.sampling_mode(SamplingMode::Flat)
.throughput(Throughput::Bytes(bytes.len() as u64)),
bytes,
.sampling_mode(SamplingMode::Flat),
include_bytes!("../files/parts-1000.rbxm"),
)
}

Expand Down
41 changes: 22 additions & 19 deletions rbx_binary/benches/suite/util.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
use criterion::{measurement::Measurement, BatchSize, BenchmarkGroup};
use rbx_dom_weak::WeakDom;
use criterion::{measurement::Measurement, BatchSize, BenchmarkGroup, Throughput};

pub(crate) fn bench<T: Measurement>(group: &mut BenchmarkGroup<T>, bench_file: &'static [u8]) {
let tree = rbx_binary::from_reader(bench_file).unwrap();
serialize_bench(group, &tree);
serialize_bench(group, bench_file);
deserialize_bench(group, bench_file);
}

fn serialize_bench<T: Measurement>(group: &mut BenchmarkGroup<T>, tree: &WeakDom) {
fn serialize_bench<T: Measurement>(group: &mut BenchmarkGroup<T>, 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();
rbx_binary::to_writer(&mut buffer, &tree, &[root_ref]).unwrap();
let buffer_len = buffer.len();

group.bench_function("Serialize", |b| {
b.iter_batched(
|| Vec::with_capacity(buffer_len),
|mut buffer: Vec<u8>| {
rbx_binary::to_writer(&mut buffer, tree, &[root_ref]).unwrap();
},
BatchSize::SmallInput,
)
});
group
.throughput(Throughput::Bytes(buffer_len as u64))
.bench_function("Serialize", |b| {
b.iter_batched(
|| Vec::with_capacity(buffer_len),
|mut buffer: Vec<u8>| {
rbx_binary::to_writer(&mut buffer, &tree, &[root_ref]).unwrap();
},
BatchSize::SmallInput,
)
});
}

fn deserialize_bench<T: Measurement>(group: &mut BenchmarkGroup<T>, buffer: &[u8]) {
group.bench_function("Deserialize", |bencher| {
bencher.iter(|| {
rbx_binary::from_reader(buffer).unwrap();
group
.throughput(Throughput::Bytes(buffer.len() as u64))
.bench_function("Deserialize", |bencher| {
bencher.iter(|| {
rbx_binary::from_reader(buffer).unwrap();
});
});
});
}

0 comments on commit 1261e9b

Please sign in to comment.