diff options
| author | mat <github@matdoes.dev> | 2021-12-19 13:07:54 -0600 |
|---|---|---|
| committer | mat <github@matdoes.dev> | 2021-12-19 13:07:54 -0600 |
| commit | 55c9f58219df0fa4a7bdcc2dd34cd71b172f77ea (patch) | |
| tree | 118c9db496099b0c8fab02e65dc9a0b7a7e5310e /azalea-nbt | |
| parent | b030b9de9345d7b1cfef205e5b9a1e2c7dc6025e (diff) | |
| download | azalea-drasl-55c9f58219df0fa4a7bdcc2dd34cd71b172f77ea.tar.xz | |
nbt tests
Diffstat (limited to 'azalea-nbt')
| -rw-r--r-- | azalea-nbt/Cargo.toml | 10 | ||||
| -rw-r--r-- | azalea-nbt/benches/my_benchmark.rs | 40 | ||||
| -rw-r--r-- | azalea-nbt/src/decode.rs | 3 | ||||
| -rw-r--r-- | azalea-nbt/src/encode.rs | 2 | ||||
| -rw-r--r-- | azalea-nbt/src/tag.rs | 24 | ||||
| -rw-r--r-- | azalea-nbt/tests/tests.rs (renamed from azalea-nbt/tests/decode.rs) | 0 |
6 files changed, 63 insertions, 16 deletions
diff --git a/azalea-nbt/Cargo.toml b/azalea-nbt/Cargo.toml index 2e9fdb3b..bdc4208c 100644 --- a/azalea-nbt/Cargo.toml +++ b/azalea-nbt/Cargo.toml @@ -10,3 +10,13 @@ byteorder = "^1.4.3" flate2 = "^1.0.22" num-derive = "^0.3.3" num-traits = "^0.2.14" + +[dev-dependencies] +criterion = {version = "^0.3.5", features = ["html_reports"]} + +[profile.release] +lto = true + +[[bench]] +harness = false +name = "my_benchmark" diff --git a/azalea-nbt/benches/my_benchmark.rs b/azalea-nbt/benches/my_benchmark.rs new file mode 100644 index 00000000..30de196c --- /dev/null +++ b/azalea-nbt/benches/my_benchmark.rs @@ -0,0 +1,40 @@ +use azalea_nbt::Tag; +use criterion::{criterion_group, criterion_main, Criterion, Throughput}; +use std::{ + fs::File, + io::{self, Read, Seek, SeekFrom}, +}; + +fn bench_serialize(filename: &str, c: &mut Criterion) { + let mut file = File::open(filename).unwrap(); + let mut contents = Vec::new(); + file.read_to_end(&mut contents).unwrap(); + let mut src = std::io::Cursor::new(&contents[..]); + file.seek(SeekFrom::Start(0)).unwrap(); + let nbt = Tag::read_gzip(&mut file).unwrap(); + + let mut group = c.benchmark_group(filename); + group.throughput(Throughput::Bytes(contents.len() as u64)); + group.bench_function("Deserialize As Blob", |b| { + b.iter(|| { + src.seek(SeekFrom::Start(0)).unwrap(); + Tag::read_gzip(&mut src).unwrap(); + }) + }); + group.bench_function("Serialize As Blob", |b| { + b.iter(|| { + nbt.write(&mut io::sink()).unwrap(); + }) + }); + group.finish(); +} + +fn bench(c: &mut Criterion) { + bench_serialize("tests/bigtest.nbt", c); + // bench_serialize::<data::PlayerData>("tests/simple_player.dat", c); + // bench_serialize::<data::PlayerData>("tests/complex_player.dat", c); + // bench_serialize::<data::Level>("tests/level.dat", c); +} + +criterion_group!(benches, bench); +criterion_main!(benches); diff --git a/azalea-nbt/src/decode.rs b/azalea-nbt/src/decode.rs index f3fbae54..a0a4ba8c 100644 --- a/azalea-nbt/src/decode.rs +++ b/azalea-nbt/src/decode.rs @@ -6,7 +6,6 @@ use std::{collections::HashMap, io::Read}; impl Tag { fn read_known(stream: &mut impl Read, id: u8) -> Result<Tag, Error> { - println!("read_known: id={}", id); let tag = match id { // Signifies the end of a TAG_Compound. It is only ever used inside // a TAG_Compound, and is not named despite being in a TAG_Compound @@ -67,9 +66,7 @@ impl Tag { 10 => { let mut map = HashMap::new(); loop { - println!("compound loop"); let tag_id = stream.read_u8().unwrap_or(0); - println!("compound loop tag_id={}", tag_id); if tag_id == 0 { break; } diff --git a/azalea-nbt/src/encode.rs b/azalea-nbt/src/encode.rs index d787e15f..2347bbf3 100644 --- a/azalea-nbt/src/encode.rs +++ b/azalea-nbt/src/encode.rs @@ -42,7 +42,7 @@ impl Tag { } Tag::List(value) => { // we just get the type from the first item, or default the type to END - let type_id = value.first().and_then(|f| Some(f.id())).unwrap_or(0); + let type_id = value.first().map(|f| f.id()).unwrap_or(0); writer.write_u8(type_id).map_err(|_| Error::WriteError)?; writer .write_i32::<BE>(value.len() as i32) diff --git a/azalea-nbt/src/tag.rs b/azalea-nbt/src/tag.rs index 7c59ea87..53ec6680 100644 --- a/azalea-nbt/src/tag.rs +++ b/azalea-nbt/src/tag.rs @@ -21,18 +21,18 @@ impl Tag { pub fn id(&self) -> u8 { match self { Tag::End => 0, - Tag::Byte(value) => 1, - Tag::Short(value) => 2, - Tag::Int(value) => 3, - Tag::Long(value) => 4, - Tag::Float(value) => 5, - Tag::Double(value) => 6, - Tag::ByteArray(value) => 7, - Tag::String(value) => 8, - Tag::List(value) => 9, - Tag::Compound(value) => 10, - Tag::IntArray(value) => 11, - Tag::LongArray(value) => 12, + Tag::Byte(_) => 1, + Tag::Short(_) => 2, + Tag::Int(_) => 3, + Tag::Long(_) => 4, + Tag::Float(_) => 5, + Tag::Double(_) => 6, + Tag::ByteArray(_) => 7, + Tag::String(_) => 8, + Tag::List(_) => 9, + Tag::Compound(_) => 10, + Tag::IntArray(_) => 11, + Tag::LongArray(_) => 12, } } } diff --git a/azalea-nbt/tests/decode.rs b/azalea-nbt/tests/tests.rs index 2c69745b..2c69745b 100644 --- a/azalea-nbt/tests/decode.rs +++ b/azalea-nbt/tests/tests.rs |
