Skip to content

Commit 8f5135e

Browse files
committed
add seperate benchmark binary
1 parent 9f4f79d commit 8f5135e

File tree

6 files changed

+141
-39
lines changed

6 files changed

+141
-39
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
Cargo.lock
33
my-prof.profile
44
Session.vim
5+
/benchmarks/bench_files

Cargo.toml

+6-6
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ name = "crit_bench"
1717
path = "benches/crit_bench.rs"
1818

1919
[dev-dependencies]
20-
criterion = "0.3.5"
21-
lzzzz = "1.0.2"
20+
criterion = "0.4.0"
21+
lzzzz = "1.0.4"
2222
lz4-compress = "0.1.1"
23-
more-asserts = "0.2.2"
24-
snap = "1.0.5"
25-
serde_json = "1.0.79"
23+
more-asserts = "0.3.1"
24+
snap = "1.1.0"
25+
serde_json = "1.0.91"
2626

2727
[dev-dependencies.lz-fear]
2828
git = "https://github.com/main--/rust-lz-fear"
@@ -44,7 +44,7 @@ frame = ["std", "dep:twox-hash"]
4444
std = []
4545

4646
[dependencies]
47-
twox-hash = { version = "1.6.2", default-features = false, optional = true }
47+
twox-hash = { version = "1.6.3", default-features = false, optional = true }
4848

4949
[profile.bench]
5050
codegen-units = 1

benches/crit_bench.rs

+23-33
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ const COMPRESSION10MB: &[u8] = include_bytes!("dickens.txt");
1717
const COMPRESSION95K_VERY_GOOD_LOGO: &[u8] = include_bytes!("../logo.jpg");
1818

1919
const ALL: &[&[u8]] = &[
20-
COMPRESSION1K as &[u8],
21-
COMPRESSION34K as &[u8],
22-
COMPRESSION65K as &[u8],
23-
COMPRESSION66K as &[u8],
24-
// COMPRESSION10MB as &[u8],
20+
//COMPRESSION1K as &[u8],
21+
//COMPRESSION34K as &[u8],
22+
//COMPRESSION65K as &[u8],
23+
//COMPRESSION66K as &[u8],
24+
COMPRESSION10MB as &[u8],
2525
// COMPRESSION95K_VERY_GOOD_LOGO as &[u8],
2626
];
2727

@@ -152,20 +152,15 @@ fn bench_block_compression_throughput(c: &mut Criterion) {
152152
|b, i| b.iter(|| lz4_flex::compress(i)),
153153
);
154154
// an empty slice that the compiler can't infer the size
155-
let empty_vec = std::env::args()
156-
.nth(1000000)
157-
.unwrap_or_default()
158-
.into_bytes();
159-
group.bench_with_input(
160-
BenchmarkId::new("lz4_flex_rust_with_dict", input_bytes),
161-
&input,
162-
|b, i| b.iter(|| lz4_flex::block::compress_with_dict(i, &empty_vec)),
163-
);
164-
// group.bench_with_input(
165-
// BenchmarkId::new("lz4_flex_rust_master", input_bytes),
166-
// &input,
167-
// |b, i| b.iter(|| lz4_flex_master::compress(&i)),
168-
// );
155+
//let empty_vec = std::env::args()
156+
//.nth(1000000)
157+
//.unwrap_or_default()
158+
//.into_bytes();
159+
//group.bench_with_input(
160+
//BenchmarkId::new("lz4_flex_rust_with_dict", input_bytes),
161+
//&input,
162+
//|b, i| b.iter(|| lz4_flex::block::compress_with_dict(i, &empty_vec)),
163+
//);
169164
group.bench_with_input(
170165
BenchmarkId::new("lz4_redox_rust", input_bytes),
171166
&input,
@@ -207,20 +202,15 @@ fn bench_block_decompression_throughput(c: &mut Criterion) {
207202
|b, i| b.iter(|| lz4_flex::decompress(i, input.len())),
208203
);
209204
// an empty slice that the compiler can't infer the size
210-
let empty_vec = std::env::args()
211-
.nth(1000000)
212-
.unwrap_or_default()
213-
.into_bytes();
214-
group.bench_with_input(
215-
BenchmarkId::new("lz4_flex_rust_with_dict", input_bytes),
216-
&comp_lz4,
217-
|b, i| b.iter(|| lz4_flex::block::decompress_with_dict(i, input.len(), &empty_vec)),
218-
);
219-
// group.bench_with_input(
220-
// BenchmarkId::new("lz4_flex_rust_master", input_bytes),
221-
// &comp_lz4,
222-
// |b, i| b.iter(|| lz4_flex_master::decompress(&i, input.len())),
223-
// );
205+
//let empty_vec = std::env::args()
206+
//.nth(1000000)
207+
//.unwrap_or_default()
208+
//.into_bytes();
209+
//group.bench_with_input(
210+
//BenchmarkId::new("lz4_flex_rust_with_dict", input_bytes),
211+
//&comp_lz4,
212+
//|b, i| b.iter(|| lz4_flex::block::decompress_with_dict(i, input.len(), &empty_vec)),
213+
//);
224214
group.bench_with_input(
225215
BenchmarkId::new("lz4_redox_rust", input_bytes),
226216
&comp_lz4,

benchmarks/Cargo.toml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "benchmarks"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
glob = "0.3.1"
10+
lz4_flex = { path = "../", features = [] }
11+

benchmarks/download_corpus_bench.sh

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
3+
mkdir bench_files
4+
cd bench_files || exit
5+
wget https://sun.aei.polsl.pl//~sdeor/corpus/silesia.zip
6+
unzip ./silesia.zip
7+
rm silesia.zip
8+

benchmarks/src/main.rs

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
use glob::glob;
2+
use std::time::Duration;
3+
use std::{io, time::Instant};
4+
5+
fn main() {
6+
for entry in glob("bench_files/*").expect("Failed to read glob pattern") {
7+
let file_name = entry.unwrap().to_str().unwrap().to_string();
8+
bench_file(&file_name).unwrap();
9+
}
10+
}
11+
12+
fn bench_file(file: &str) -> io::Result<()> {
13+
bench_compression(file)?;
14+
bench_decompression(file)?;
15+
Ok(())
16+
}
17+
18+
fn bench_compression(file: &str) -> io::Result<()> {
19+
let file_content = std::fs::read(file)?;
20+
let mb = file_content.len() as f32 / 1_000_000 as f32;
21+
22+
let mut out = Vec::new();
23+
let print_info = format!("{file} - Compression");
24+
25+
bench(mb, &print_info, || {
26+
out.clear();
27+
compress(&file_content, &mut out);
28+
});
29+
30+
Ok(())
31+
}
32+
33+
fn bench_decompression(file: &str) -> io::Result<()> {
34+
let file_content = std::fs::read(file)?;
35+
let mb = file_content.len() as f32 / 1_000_000 as f32;
36+
37+
let mut compressed = Vec::new();
38+
compress(&file_content, &mut compressed);
39+
40+
let mut out = Vec::new();
41+
let print_info = format!("{file} - Decompression");
42+
43+
bench(mb, &print_info, || {
44+
out.clear();
45+
decompress(&compressed, &mut out);
46+
});
47+
48+
Ok(())
49+
}
50+
51+
fn decompress(input: &[u8], out: &mut Vec<u8>) {
52+
let mut rdr = lz4_flex::frame::FrameDecoder::new(input);
53+
io::copy(&mut rdr, out).expect("I/O operation failed");
54+
}
55+
56+
fn compress(input: &[u8], out: &mut Vec<u8>) {
57+
let mut wtr = lz4_flex::frame::FrameEncoder::new(out);
58+
59+
io::copy(&mut &input[..], &mut wtr).expect("I/O operation failed");
60+
wtr.finish().unwrap();
61+
}
62+
63+
fn bench<F>(mb: f32, print_info: &str, mut do_stuff: F)
64+
where
65+
F: FnMut(),
66+
{
67+
let start = Instant::now();
68+
let mut last_print = Instant::now();
69+
70+
let mut loops = 0;
71+
loop {
72+
let start_loop = Instant::now();
73+
74+
do_stuff();
75+
76+
let after_comp = Instant::now();
77+
78+
let elapsed_since_last_print: Duration = after_comp - last_print;
79+
if elapsed_since_last_print.as_secs_f32() > 1.0 {
80+
let elapsed_since_loop_start: Duration = after_comp - start_loop;
81+
let through_put = mb as f32 / elapsed_since_loop_start.as_secs_f32();
82+
println!("{print_info} {:.2} Mb/s", through_put);
83+
last_print = Instant::now();
84+
}
85+
86+
loops += 1;
87+
let secs_since_start = (Instant::now() - start).as_secs_f32();
88+
if secs_since_start > 5.0 || loops > 1000 {
89+
break;
90+
}
91+
}
92+
}

0 commit comments

Comments
 (0)