Skip to content

Commit

Permalink
Fixed the bin fire you had going there
Browse files Browse the repository at this point in the history
  • Loading branch information
ReCore-sys committed Sep 12, 2024
1 parent 4e2ac0b commit 2f5ea5a
Show file tree
Hide file tree
Showing 6 changed files with 340 additions and 293 deletions.
59 changes: 59 additions & 0 deletions compression_tester.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import bz2
import gzip
import lzma
import time
import zlib

results = {}

def compress_zlib_max(data):
return zlib.compress(data, 9)

def compress_zlib_min(data):
return zlib.compress(data, 1)

def compress_gzip_max(data):
return gzip.compress(data, 5)

def compress_gzip_min(data):
return gzip.compress(data, 1)

def compress_lzma(data):
return lzma.compress(data)

def compress_bzip2_max(data):
return bz2.compress(data, 9)

def compress_bzip2_min(data):
return bz2.compress(data, 1)

def compress_none(data):
return data


with open(".etc/chunk.nbt", "rb") as f:
data = f.read()
original_size = len(data)
for algo in [compress_zlib_max, compress_zlib_min, compress_gzip_max, compress_gzip_min, compress_lzma, compress_bzip2_max, compress_bzip2_min, compress_none]:
print(f"Testing {algo.__name__}...")
times = []
sizediff = 0
for iteration in range(1, 3000):
start = time.time_ns()
compressed = algo(data)
end = time.time_ns()
times.append(end - start)
sizediff = len(compressed) / original_size
average_time = (sum(times) / len(times) / 1000) / 1000
results[algo.__name__] = {
"average_time": average_time,
"size_decrease_percentage": sizediff
}
for result in results:
name = result
average_time = results[result]["average_time"]
sizediff = results[result]["size_decrease_percentage"]
print(f"Algorithm: {name}")
print(f"Average Time: {average_time:.3f} ms")
print(f"Compressed Size: {sizediff*100:.2f}% of original size")
print()
27 changes: 13 additions & 14 deletions src/benches/bench_nbt_ser_de.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use std::io::{Cursor, Write};

use criterion::{black_box, criterion_group, criterion_main, Criterion};
use nbt_lib::{read_tag, NBTDeserialize, NBTDeserialize, NBTSerialize, NBTSerialize};
use std::io::Cursor;

use crate::test_simd_de_data::MinecraftChunk;
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use nbt_lib::{read_tag, NBTSerialize};

mod test_de_data {
use super::*;
use nbt_lib::{NBTDeserialize, NBTSerialize};

#[derive(NBTSerialize, NBTDeserialize, Debug, Clone)]
#[nbt(is_root)]
Expand Down Expand Up @@ -78,7 +77,7 @@ mod test_de_data {
language: String,
}

pub fn create_test_player() -> Player {
pub fn _create_test_player() -> Player {
Player {
name: "SuperPlayer123".to_string(),
age: 25,
Expand Down Expand Up @@ -174,7 +173,7 @@ mod test_de_data {
}
}
mod test_simd_de_data {
use super::*;
use nbt_lib::{NBTDeserialize, NBTSerialize};

#[derive(NBTSerialize, NBTDeserialize, Debug)]
#[nbt(rename = "Level")]
Expand Down Expand Up @@ -286,7 +285,7 @@ fn benchmark_serialization(c: &mut Criterion) {
c.bench_function("serialize", |b| {
b.iter(|| {
let mut buffer = Vec::with_capacity(10240);
black_box(world.serialize(&mut buffer)).unwrap();
black_box(world.nbt_serialize(&mut buffer)).unwrap();
})
});
}
Expand All @@ -306,7 +305,7 @@ fn benchmark_serialization(c: &mut Criterion) {
}
*/

fn get_nbt_buffer() -> Vec<u8> {
fn _get_nbt_buffer() -> Vec<u8> {
/*let mut buffer = std::fs::read(".etc/TheAIguy_.nbt").unwrap();
// decompress gzip
Expand All @@ -323,13 +322,13 @@ fn get_nbt_buffer() -> Vec<u8> {
let data = MinecraftChunk::create_test_instance();

let mut buffer = Vec::with_capacity(2048);
data.serialize(&mut buffer).unwrap();
data.nbt_serialize(&mut buffer).unwrap();

buffer
}

fn benchmark_raw_deserialization(c: &mut Criterion) {
let buffer = get_nbt_buffer();
fn _benchmark_raw_deserialization(c: &mut Criterion) {
let buffer = _get_nbt_buffer();
let mut cursor = Cursor::new(buffer);

c.bench_function("world_chunk_deser_raw", |b| {
Expand All @@ -342,8 +341,8 @@ fn benchmark_raw_deserialization(c: &mut Criterion) {
})
});
}
fn benchmark_simdnbt_deserialization(c: &mut Criterion) {
let buffer = get_nbt_buffer();
fn _benchmark_simdnbt_deserialization(c: &mut Criterion) {
let buffer = _get_nbt_buffer();

let buffer = buffer.as_slice();

Expand Down
Loading

0 comments on commit 2f5ea5a

Please sign in to comment.