From 55c9f58219df0fa4a7bdcc2dd34cd71b172f77ea Mon Sep 17 00:00:00 2001 From: mat Date: Sun, 19 Dec 2021 13:07:54 -0600 Subject: nbt tests --- azalea-nbt/Cargo.toml | 10 ++++++ azalea-nbt/benches/my_benchmark.rs | 40 ++++++++++++++++++++++++ azalea-nbt/src/decode.rs | 3 -- azalea-nbt/src/encode.rs | 2 +- azalea-nbt/src/tag.rs | 24 +++++++-------- azalea-nbt/tests/decode.rs | 62 -------------------------------------- azalea-nbt/tests/tests.rs | 62 ++++++++++++++++++++++++++++++++++++++ 7 files changed, 125 insertions(+), 78 deletions(-) create mode 100644 azalea-nbt/benches/my_benchmark.rs delete mode 100644 azalea-nbt/tests/decode.rs create mode 100644 azalea-nbt/tests/tests.rs (limited to 'azalea-nbt') 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::("tests/simple_player.dat", c); + // bench_serialize::("tests/complex_player.dat", c); + // bench_serialize::("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 { - 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::(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/decode.rs deleted file mode 100644 index 2c69745b..00000000 --- a/azalea-nbt/tests/decode.rs +++ /dev/null @@ -1,62 +0,0 @@ -use azalea_nbt::Tag; -use flate2::{ - read::{GzDecoder, ZlibDecoder}, - write::{GzEncoder, ZlibEncoder}, -}; -use std::{ - collections::HashMap, - io::{Cursor, Read}, -}; - -#[test] -fn test_decode_hello_world() { - // read hello_world.nbt - let mut file = std::fs::File::open("tests/hello_world.nbt").unwrap(); - let tag = Tag::read(&mut file).unwrap(); - assert_eq!( - tag, - Tag::Compound(HashMap::from_iter(vec![( - "hello world".to_string(), - Tag::Compound(HashMap::from_iter(vec![( - "name".to_string(), - Tag::String("Bananrama".to_string()), - )])) - )])) - ); -} - -#[test] -fn test_roundtrip_hello_world() { - let mut file = std::fs::File::open("tests/hello_world.nbt").unwrap(); - let mut original = Vec::new(); - file.read_to_end(&mut original).unwrap(); - - let mut original_stream = Cursor::new(original.clone()); - let tag = Tag::read(&mut original_stream).unwrap(); - - println!("ok read {:?}", tag); - - // write hello_world.nbt - let mut result = Cursor::new(Vec::new()); - tag.write(&mut result).unwrap(); - - assert_eq!(result.into_inner(), original); -} - -#[test] -fn test_bigtest() { - // read bigtest.nbt - let mut file = std::fs::File::open("tests/bigtest.nbt").unwrap(); - let mut original = Vec::new(); - file.read_to_end(&mut original).unwrap(); - - let mut original_stream = Cursor::new(original.clone()); - let original_tag = Tag::read_gzip(&mut original_stream).unwrap(); - - let mut result = Vec::new(); - original_tag.write(&mut result).unwrap(); - - let decoded_tag = Tag::read(&mut Cursor::new(result)).unwrap(); - - assert_eq!(decoded_tag, original_tag); -} diff --git a/azalea-nbt/tests/tests.rs b/azalea-nbt/tests/tests.rs new file mode 100644 index 00000000..2c69745b --- /dev/null +++ b/azalea-nbt/tests/tests.rs @@ -0,0 +1,62 @@ +use azalea_nbt::Tag; +use flate2::{ + read::{GzDecoder, ZlibDecoder}, + write::{GzEncoder, ZlibEncoder}, +}; +use std::{ + collections::HashMap, + io::{Cursor, Read}, +}; + +#[test] +fn test_decode_hello_world() { + // read hello_world.nbt + let mut file = std::fs::File::open("tests/hello_world.nbt").unwrap(); + let tag = Tag::read(&mut file).unwrap(); + assert_eq!( + tag, + Tag::Compound(HashMap::from_iter(vec![( + "hello world".to_string(), + Tag::Compound(HashMap::from_iter(vec![( + "name".to_string(), + Tag::String("Bananrama".to_string()), + )])) + )])) + ); +} + +#[test] +fn test_roundtrip_hello_world() { + let mut file = std::fs::File::open("tests/hello_world.nbt").unwrap(); + let mut original = Vec::new(); + file.read_to_end(&mut original).unwrap(); + + let mut original_stream = Cursor::new(original.clone()); + let tag = Tag::read(&mut original_stream).unwrap(); + + println!("ok read {:?}", tag); + + // write hello_world.nbt + let mut result = Cursor::new(Vec::new()); + tag.write(&mut result).unwrap(); + + assert_eq!(result.into_inner(), original); +} + +#[test] +fn test_bigtest() { + // read bigtest.nbt + let mut file = std::fs::File::open("tests/bigtest.nbt").unwrap(); + let mut original = Vec::new(); + file.read_to_end(&mut original).unwrap(); + + let mut original_stream = Cursor::new(original.clone()); + let original_tag = Tag::read_gzip(&mut original_stream).unwrap(); + + let mut result = Vec::new(); + original_tag.write(&mut result).unwrap(); + + let decoded_tag = Tag::read(&mut Cursor::new(result)).unwrap(); + + assert_eq!(decoded_tag, original_tag); +} -- cgit v1.2.3