dalet-rs/examples/bench.rs

124 lines
3.2 KiB
Rust
Raw Normal View History

use dalet::{
2024-09-01 09:49:00 +03:00
daletl::DlPage,
daletpack::*,
typed::{Hl, TNullArg, Tag::*},
};
use flate2::Compression;
2024-09-01 09:49:00 +03:00
use serde::{Deserialize, Serialize};
2024-08-02 22:09:12 +03:00
use std::io::Write;
#[macro_export]
macro_rules! iprint {
($name:expr, $func:expr) => {{
let start = std::time::Instant::now();
let result = $func;
let elapsed = start.elapsed();
2024-09-01 09:49:00 +03:00
println!("{} ({:#?}): {} bytes", $name, elapsed, result.len());
result
}};
}
2024-09-01 09:49:00 +03:00
#[macro_export]
macro_rules! bench {
($name:expr, $func:expr) => {{
let res = iprint!($name, $func);
iprint!(
$name.to_owned() + " zstd",
utils::compress_zstd(&res).unwrap()
);
iprint!($name.to_owned() + " zlib", compress_zlib(&res).unwrap());
iprint!(
$name.to_owned() + " deflate",
compress_deflate(&res).unwrap()
);
println!();
res
}};
}
fn compress_deflate(data: &[u8]) -> std::io::Result<Vec<u8>> {
let mut c = flate2::write::DeflateEncoder::new(Vec::new(), Compression::default());
c.write_all(data)?;
c.finish()
}
2024-09-01 09:49:00 +03:00
fn compress_zlib(data: &[u8]) -> std::io::Result<Vec<u8>> {
let mut c = flate2::write::ZlibEncoder::new(Vec::new(), Compression::default());
c.write_all(data)?;
c.finish()
}
2024-09-01 09:49:00 +03:00
fn dlhn_serialize(page: &DlPage) -> Vec<u8> {
let mut output = Vec::new();
let mut serializer = dlhn::Serializer::new(&mut output);
page.serialize(&mut serializer).unwrap();
output
}
fn dlhn_deserialize(output: &Vec<u8>) -> DlPage {
let mut reader = output.as_slice();
let mut deserializer = dlhn::Deserializer::new(&mut reader);
DlPage::deserialize(&mut deserializer).unwrap()
}
2024-08-08 12:25:12 +03:00
fn main() {
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,
Code("Hello world".into(), TNullArg::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-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-03 12:47:33 +03:00
El(" dolor sit amet consequetur adipiscing elit".into()),
]
.into()),
Table(vec![
2024-08-12 20:30:46 +03:00
Tprow(vec![
2024-08-03 12:47:33 +03:00
El("Col 1".into()),
El("Col 2".into()),
El("Col 3".into()),
]),
2024-08-12 20:30:46 +03:00
Trow(vec![
2024-08-03 12:47:33 +03:00
El("Never gonna".into()),
El("give you".into()),
El("up".into()),
]),
]),
];
2024-08-05 18:43:18 +03:00
let dalet_page = page.into();
2024-09-01 09:49:00 +03:00
bench!("Markdown", include_str!("./bench.md").as_bytes().to_vec());
bench!("Daletpack", encode_no_compress(&dalet_page).unwrap());
bench!("Dlhn", dlhn_serialize(&dalet_page));
bench!("Messagepack", rmp_serde::to_vec(&dalet_page).unwrap());
bench!("Bincode", bincode::serialize(&dalet_page).unwrap());
}