-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathfiletests.rs
72 lines (62 loc) · 2.21 KB
/
filetests.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//! Crate for testing whether the deserialize codegen is capable of handling the
//! sample NBT files in the test/ directory, which include real
//! Minecraft-generated files.
extern crate criterion;
#[macro_use]
extern crate serde_derive;
extern crate serde;
extern crate nbt;
use std::fs::File;
use std::io::{self, Read, Seek, SeekFrom};
use criterion::{criterion_group, criterion_main, Criterion, Throughput};
use nbt::de::from_gzip_reader;
use nbt::ser::to_writer;
mod data {
include!("../tests/data.rs.in");
}
fn bench_serialize<T>(filename: &str, c: &mut Criterion)
where
T: serde::de::DeserializeOwned + serde::ser::Serialize,
{
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_struct: T = from_gzip_reader(&mut file).unwrap();
file.seek(SeekFrom::Start(0)).unwrap();
let nbt_blob = nbt::Blob::from_gzip_reader(&mut file).unwrap();
let mut group = c.benchmark_group(filename);
group.throughput(Throughput::Bytes(contents.len() as u64));
group.bench_function("Deserialize As Struct", |b| {
b.iter(|| {
src.seek(SeekFrom::Start(0)).unwrap();
let _: T = from_gzip_reader(&mut src).unwrap();
})
});
group.bench_function("Deserialize As Blob", |b| {
b.iter(|| {
src.seek(SeekFrom::Start(0)).unwrap();
nbt::Blob::from_gzip_reader(&mut src).unwrap();
})
});
group.bench_function("Serialize As Struct", |b| {
b.iter(|| {
to_writer(&mut io::sink(), &nbt_struct, None).unwrap();
})
});
group.bench_function("Serialize As Blob", |b| {
b.iter(|| {
nbt_blob.to_writer(&mut io::sink()).unwrap();
})
});
group.finish();
}
fn bench(c: &mut Criterion) {
bench_serialize::<data::Big1>("tests/big1.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);