-
Notifications
You must be signed in to change notification settings - Fork 27
/
lzma.rs
129 lines (111 loc) · 3.67 KB
/
lzma.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#![feature(test)]
extern crate test;
use std::io::Read;
use test::Bencher;
fn compress_bench(x: &[u8], b: &mut Bencher) {
b.iter(|| {
let mut compressed: Vec<u8> = Vec::new();
lzma_rs::lzma_compress(&mut std::io::BufReader::new(x), &mut compressed).unwrap();
compressed
});
}
fn decompress_after_compress_bench(x: &[u8], b: &mut Bencher) {
let mut compressed: Vec<u8> = Vec::new();
lzma_rs::lzma_compress(&mut std::io::BufReader::new(x), &mut compressed).unwrap();
b.iter(|| {
let mut bf = std::io::BufReader::new(compressed.as_slice());
let mut decomp: Vec<u8> = Vec::new();
lzma_rs::lzma_decompress(&mut bf, &mut decomp).unwrap();
decomp
});
}
fn decompress_bench(compressed: &[u8], b: &mut Bencher) {
b.iter(|| {
let mut bf = std::io::BufReader::new(compressed);
let mut decomp: Vec<u8> = Vec::new();
lzma_rs::lzma_decompress(&mut bf, &mut decomp).unwrap();
decomp
});
}
#[cfg(feature = "stream")]
fn decompress_stream_bench(compressed: &[u8], b: &mut Bencher) {
use std::io::Write;
b.iter(|| {
let mut stream = lzma_rs::decompress::Stream::new(Vec::new());
stream.write_all(compressed).unwrap();
stream.finish().unwrap()
});
}
fn decompress_bench_file(compfile: &str, b: &mut Bencher) {
let mut f = std::fs::File::open(compfile).unwrap();
let mut compressed = Vec::new();
f.read_to_end(&mut compressed).unwrap();
decompress_bench(&compressed, b);
}
#[cfg(feature = "stream")]
fn decompress_stream_bench_file(compfile: &str, b: &mut Bencher) {
let mut f = std::fs::File::open(compfile).unwrap();
let mut compressed = Vec::new();
f.read_to_end(&mut compressed).unwrap();
decompress_stream_bench(&compressed, b);
}
#[bench]
fn compress_empty(b: &mut Bencher) {
#[cfg(feature = "enable_logging")]
let _ = env_logger::try_init();
compress_bench(b"", b);
}
#[bench]
fn decompress_after_compress_empty(b: &mut Bencher) {
#[cfg(feature = "enable_logging")]
let _ = env_logger::try_init();
decompress_after_compress_bench(b"", b);
}
#[bench]
fn compress_hello(b: &mut Bencher) {
#[cfg(feature = "enable_logging")]
let _ = env_logger::try_init();
compress_bench(b"Hello world", b);
}
#[bench]
fn decompress_after_compress_hello(b: &mut Bencher) {
#[cfg(feature = "enable_logging")]
let _ = env_logger::try_init();
decompress_after_compress_bench(b"Hello world", b);
}
#[bench]
fn compress_65536(b: &mut Bencher) {
#[cfg(feature = "enable_logging")]
let _ = env_logger::try_init();
compress_bench(&[0; 0x10000], b);
}
#[bench]
fn decompress_after_compress_65536(b: &mut Bencher) {
#[cfg(feature = "enable_logging")]
let _ = env_logger::try_init();
decompress_after_compress_bench(&[0; 0x10000], b);
}
#[bench]
fn decompress_big_file(b: &mut Bencher) {
#[cfg(feature = "enable_logging")]
let _ = env_logger::try_init();
decompress_bench_file("tests/files/foo.txt.lzma", b);
}
#[cfg(feature = "stream")]
#[bench]
fn decompress_stream_big_file(b: &mut Bencher) {
#[cfg(feature = "enable_logging")]
let _ = env_logger::try_init();
decompress_stream_bench_file("tests/files/foo.txt.lzma", b);
}
#[bench]
fn decompress_huge_dict(b: &mut Bencher) {
#[cfg(feature = "enable_logging")]
let _ = env_logger::try_init();
let compressed: &[u8] = b"\x5d\x7f\x7f\x7f\x7f\xff\xff\xff\
\xff\xff\xff\xff\xff\x00\x24\x19\
\x49\x98\x6f\x10\x19\xc6\xd7\x31\
\xeb\x36\x50\xb2\x98\x48\xff\xfe\
\xa5\xb0\x00";
decompress_bench(&compressed, b);
}