2024-08-02 17:49:52 +03:00
|
|
|
use dalet::{
|
|
|
|
daletpack::*,
|
2024-08-06 10:16:24 +03:00
|
|
|
typed::{Hl, TNArg, Tag::*},
|
2024-08-02 17:49:52 +03:00
|
|
|
};
|
|
|
|
use flate2::Compression;
|
2024-08-02 22:09:12 +03:00
|
|
|
use std::io::Write;
|
2024-08-02 17:49:52 +03:00
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! iprint {
|
|
|
|
($name:expr, $func:expr) => {{
|
|
|
|
let start = std::time::Instant::now();
|
|
|
|
let result = $func;
|
|
|
|
let elapsed = start.elapsed();
|
2024-08-06 10:16:24 +03:00
|
|
|
println!("{} ({:#?}): {}", $name, elapsed, result.len());
|
2024-08-02 17:49:52 +03:00
|
|
|
|
|
|
|
result
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn compress_deflate(data: &Vec<u8>) -> std::io::Result<Vec<u8>> {
|
|
|
|
let mut c = flate2::write::DeflateEncoder::new(Vec::new(), Compression::default());
|
|
|
|
c.write(data)?;
|
|
|
|
c.finish()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn compress_zlib(data: &Vec<u8>) -> std::io::Result<Vec<u8>> {
|
|
|
|
let mut c = flate2::write::ZlibEncoder::new(Vec::new(), Compression::default());
|
|
|
|
c.write(data)?;
|
|
|
|
c.finish()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn bench() {
|
2024-08-05 18:43:18 +03:00
|
|
|
let page = vec![
|
2024-08-06 10:16:24 +03:00
|
|
|
H("Heading 1".into(), Hl::One),
|
2024-08-03 12:47:33 +03:00
|
|
|
H("Heading 2".into(), Hl::Two),
|
2024-08-03 14:29:15 +03:00
|
|
|
P(vec![
|
2024-08-03 12:47:33 +03:00
|
|
|
El("Some ".into()),
|
|
|
|
B("bold".into()),
|
|
|
|
I("italic".into()),
|
|
|
|
S("strike".into()),
|
|
|
|
]
|
|
|
|
.into()),
|
|
|
|
Br,
|
2024-08-03 14:29:15 +03:00
|
|
|
Code("Hello world".into(), TNArg::Null),
|
2024-08-03 12:47:33 +03:00
|
|
|
Br,
|
|
|
|
Ul(vec![
|
|
|
|
El("abc".into()),
|
|
|
|
El(vec![
|
|
|
|
El("def".into()),
|
|
|
|
Ul(vec![El("defabc".into()), El("defdef".into())]),
|
|
|
|
]
|
|
|
|
.into()),
|
|
|
|
El("xyz".into()),
|
2024-08-02 17:49:52 +03:00
|
|
|
]),
|
2024-08-03 12:47:33 +03:00
|
|
|
Br,
|
2024-08-03 14:29:15 +03:00
|
|
|
P(vec![
|
2024-08-03 12:47:33 +03:00
|
|
|
El("Lorem ipsum ".into()),
|
|
|
|
Link(
|
|
|
|
vec![Img("https://my-picture".into())].into(),
|
|
|
|
"https://some-link".into(),
|
2024-08-02 17:49:52 +03:00
|
|
|
),
|
2024-08-03 12:47:33 +03:00
|
|
|
El(" dolor sit amet consequetur adipiscing elit".into()),
|
|
|
|
]
|
|
|
|
.into()),
|
|
|
|
Table(vec![
|
|
|
|
Tpcol(vec![
|
|
|
|
El("Col 1".into()),
|
|
|
|
El("Col 2".into()),
|
|
|
|
El("Col 3".into()),
|
2024-08-02 17:49:52 +03:00
|
|
|
]),
|
2024-08-03 12:47:33 +03:00
|
|
|
Tcol(vec![
|
|
|
|
El("Never gonna".into()),
|
|
|
|
El("give you".into()),
|
|
|
|
El("up".into()),
|
2024-08-02 17:49:52 +03:00
|
|
|
]),
|
|
|
|
]),
|
|
|
|
];
|
|
|
|
|
2024-08-05 18:43:18 +03:00
|
|
|
let dalet_page = page.into();
|
2024-08-02 17:49:52 +03:00
|
|
|
|
|
|
|
let markdown = iprint!("Markdown", include_str!("./bench.md").as_bytes().to_vec());
|
|
|
|
let daletpack = iprint!("Daletpack", encode_no_compress(&dalet_page).unwrap());
|
|
|
|
let messagepack = iprint!("Messagepack", rmp_serde::to_vec(&dalet_page).unwrap());
|
|
|
|
let bincode = iprint!("Bincode", bincode::serialize(&dalet_page).unwrap());
|
|
|
|
|
|
|
|
println!();
|
|
|
|
|
|
|
|
iprint!("Markdown zstd", utils::compress_zstd(&markdown).unwrap());
|
2024-08-06 10:16:24 +03:00
|
|
|
let daletpack = iprint!("Daletpack zstd", utils::compress_zstd(&daletpack).unwrap());
|
2024-08-02 17:49:52 +03:00
|
|
|
iprint!(
|
|
|
|
"Messagepack zstd",
|
|
|
|
utils::compress_zstd(&messagepack).unwrap()
|
|
|
|
);
|
|
|
|
iprint!("Bincode zstd", utils::compress_zstd(&bincode).unwrap());
|
|
|
|
|
|
|
|
println!();
|
|
|
|
|
|
|
|
iprint!("Markdown Zlib", compress_zlib(&markdown).unwrap());
|
|
|
|
iprint!("Daletpack Zlib", compress_zlib(&daletpack).unwrap());
|
|
|
|
iprint!("Messagepack Zlib", compress_zlib(&messagepack).unwrap());
|
|
|
|
iprint!("Bincode Zlib", compress_zlib(&bincode).unwrap());
|
|
|
|
|
|
|
|
println!();
|
|
|
|
|
|
|
|
iprint!("Markdown deflate", compress_deflate(&markdown).unwrap());
|
|
|
|
iprint!("Daletpack deflate", compress_deflate(&daletpack).unwrap());
|
|
|
|
iprint!(
|
|
|
|
"Messagepack deflate",
|
|
|
|
compress_deflate(&messagepack).unwrap()
|
|
|
|
);
|
|
|
|
iprint!("Bincode deflate", compress_deflate(&bincode).unwrap());
|
2024-08-06 10:16:24 +03:00
|
|
|
|
|
|
|
println!();
|
|
|
|
|
|
|
|
let decoded = iprint!(
|
|
|
|
"Daletpack decode",
|
|
|
|
Decoder::new(&daletpack).unwrap().decode().unwrap().data
|
|
|
|
);
|
|
|
|
|
|
|
|
println!("{:#?}", decoded);
|
2024-08-02 17:49:52 +03:00
|
|
|
}
|