use divan::black_box;
use divan::counter::ItemsCount;
use flate2::Compression;
use flate2::write::ZlibEncoder;
use std::collections::HashSet;
use std::io::Write;
use wacore_binary::NodeStream;
use wacore_binary::builder::NodeBuilder;
use wacore_binary::marshal::{
marshal, marshal_auto, marshal_exact, marshal_ref, marshal_ref_auto, marshal_ref_exact,
marshal_shallow, marshal_to, unmarshal_ref,
};
use wacore_binary::node::Node;
use wacore_binary::util::{FORMAT_COMPRESSED, unpack};
fn main() {
divan::main();
}
const BATCH: usize = 256;
fn bench_batched<I, O>(
bencher: divan::Bencher,
batch: usize,
setup: impl Fn() -> I + Sync,
op: impl Fn(&I) -> O + Sync,
) {
bencher
.counter(ItemsCount::new(batch))
.with_inputs(setup)
.bench_refs(|input| {
for _ in 0..batch {
black_box(op(black_box(&*input)));
}
});
}
fn create_small_node() -> Node {
NodeBuilder::new("message")
.attr("to", "user@s.whatsapp.net")
.attr("id", "12345")
.attr("type", "text")
.build()
}
fn create_ack_node() -> Node {
NodeBuilder::new("ack")
.attr("to", "5511999990000@s.whatsapp.net")
.attr("id", "3EB0A1B2C3D4E5F60718")
.attr("class", "message")
.build()
}
fn create_fanout_node() -> Node {
let devices: Vec<Node> = (0..8)
.map(|i| {
NodeBuilder::new("to")
.attr("jid", format!("5511999990000:{i}@s.whatsapp.net"))
.children(vec![
NodeBuilder::new("enc")
.attr("v", "2")
.attr("type", "msg")
.bytes(vec![0xAB; 128])
.build(),
])
.build()
})
.collect();
NodeBuilder::new("message")
.attr("to", "5511999990000@g.us")
.attr("id", "3EB0A1B2C3D4E5F60718")
.attr("type", "text")
.children(vec![
NodeBuilder::new("participants").children(devices).build(),
])
.build()
}
fn create_large_node() -> Node {
NodeBuilder::new("iq")
.attr("to", "server@s.whatsapp.net")
.attr("id", "abcdef")
.attr("type", "get")
.attr("xmlns", "usync")
.children(vec![
NodeBuilder::new("usync")
.attr("sid", "message:1")
.attr("mode", "query")
.attr("last", "true")
.children(vec![
NodeBuilder::new("query")
.children(vec![NodeBuilder::new("business").build()])
.build(),
])
.build(),
NodeBuilder::new("list")
.children((0..20).map(|i| {
NodeBuilder::new("item")
.attr("index", i.to_string())
.bytes(vec![i as u8; 32])
.build()
}))
.build(),
])
.build()
}
fn create_attr_node() -> Node {
NodeBuilder::new("iq")
.attr("xmlns", "test:ns")
.attr("type", "result")
.attr("id", "abc123")
.attr("from", "server@s.whatsapp.net")
.attr("has_flag", "true")
.attr("timestamp", "1700000000")
.build()
}
fn create_long_string_node() -> Node {
let base_pattern = "Lorem ipsum with email user@example.com in text. ";
let long_text = base_pattern.repeat(11);
NodeBuilder::new("message")
.attr("to", "1234567890@s.whatsapp.net")
.attr("id", "ABC123DEF456")
.attr("type", "text")
.string_content(long_text)
.build()
}
fn create_usync_like_node() -> Node {
NodeBuilder::new("iq")
.attr("type", "result")
.children(vec![
NodeBuilder::new("usync")
.children(vec![
NodeBuilder::new("list")
.children((0..50).map(|i| {
NodeBuilder::new("user")
.attr("jid", format!("{}@s.whatsapp.net", i))
.children(vec![
NodeBuilder::new("devices")
.children(vec![
NodeBuilder::new("device-list")
.children((0..3).map(|d| {
NodeBuilder::new("device")
.attr("id", d.to_string())
.build()
}))
.build(),
])
.build(),
NodeBuilder::new("contact").attr("type", "in").build(),
])
.build()
}))
.build(),
])
.build(),
])
.build()
}
fn create_jid_heavy_node() -> Node {
NodeBuilder::new("message")
.attr("from", "15551234567@s.whatsapp.net")
.attr("to", "15559876543@s.whatsapp.net")
.attr("participant", "15555555555@s.whatsapp.net")
.attr("recipient", "15556666666@s.whatsapp.net")
.attr("notify", "15557777777@s.whatsapp.net")
.attr("id", "ABCDEF123456")
.attr("type", "text")
.build()
}
fn create_huge_bytes_node() -> Node {
NodeBuilder::new("message")
.attr("to", "server@s.whatsapp.net")
.attr("id", "huge-binary")
.bytes(vec![0x5A; 256 * 1024])
.build()
}
fn create_many_children_node() -> Node {
NodeBuilder::new("iq")
.attr("to", "server@s.whatsapp.net")
.attr("id", "many-children")
.children((0..2048).map(|i| {
NodeBuilder::new("item")
.attr("index", i.to_string())
.attr("type", "entry")
.string_content("ok")
.build()
}))
.build()
}
#[divan::bench]
fn bench_marshal_auto_small(bencher: divan::Bencher) {
bench_batched(bencher, BATCH, create_small_node, |node| {
marshal_auto(node).unwrap()
});
}
#[divan::bench]
fn bench_marshal_auto_large(bencher: divan::Bencher) {
bench_batched(bencher, BATCH, create_large_node, |node| {
marshal_auto(node).unwrap()
});
}
#[divan::bench]
fn bench_marshal_auto_long_string(bencher: divan::Bencher) {
bench_batched(bencher, BATCH, create_long_string_node, |node| {
marshal_auto(node).unwrap()
});
}
#[divan::bench]
fn bench_marshal_auto_huge_bytes(bencher: divan::Bencher) {
bencher
.with_inputs(create_huge_bytes_node)
.bench_refs(|node| black_box(marshal_auto(black_box(node)).unwrap()));
}
#[divan::bench]
fn bench_marshal_auto_many_children(bencher: divan::Bencher) {
bencher
.with_inputs(create_many_children_node)
.bench_refs(|node| black_box(marshal_auto(black_box(node)).unwrap()));
}
#[divan::bench]
fn bench_marshal_exact_many_children(bencher: divan::Bencher) {
bencher
.with_inputs(create_many_children_node)
.bench_refs(|node| black_box(marshal_exact(black_box(node)).unwrap()));
}
#[divan::bench]
fn bench_marshal_shallow_many_children(bencher: divan::Bencher) {
bencher
.with_inputs(create_many_children_node)
.bench_refs(|node| black_box(marshal_shallow(black_box(node)).expect("marshal_shallow")));
}
#[divan::bench]
fn bench_marshal_plain_large(bencher: divan::Bencher) {
bench_batched(bencher, BATCH, create_large_node, |node| {
marshal(node).unwrap()
});
}
#[divan::bench]
fn bench_marshal_exact_large(bencher: divan::Bencher) {
bench_batched(bencher, BATCH, create_large_node, |node| {
marshal_exact(node).unwrap()
});
}
#[divan::bench]
fn bench_marshal_shallow_large(bencher: divan::Bencher) {
bench_batched(bencher, BATCH, create_large_node, |node| {
marshal_shallow(node).expect("marshal_shallow")
});
}
#[divan::bench]
fn bench_marshal_to_reused_buffer_large(bencher: divan::Bencher) {
bencher
.counter(ItemsCount::new(BATCH))
.with_inputs(|| (create_large_node(), Vec::with_capacity(4096)))
.bench_refs(|(node, buffer)| {
for _ in 0..BATCH {
buffer.clear();
marshal_to(black_box(&*node), buffer).unwrap();
black_box(buffer.as_slice());
}
});
}
fn node_bytes(node: &Node) -> Vec<u8> {
unpack(&marshal(node).unwrap()).unwrap().into_owned()
}
fn setup_small_node_bytes() -> Vec<u8> {
node_bytes(&create_small_node())
}
fn setup_large_node_bytes() -> Vec<u8> {
node_bytes(&create_large_node())
}
#[divan::bench]
fn bench_unmarshal_small(bencher: divan::Bencher) {
bench_batched(bencher, BATCH, setup_small_node_bytes, |bytes| {
black_box(unmarshal_ref(bytes).unwrap());
});
}
fn setup_ack_node_bytes() -> Vec<u8> {
node_bytes(&create_ack_node())
}
fn setup_fanout_node_bytes() -> Vec<u8> {
node_bytes(&create_fanout_node())
}
#[divan::bench]
fn bench_unmarshal_ack(bencher: divan::Bencher) {
bench_batched(bencher, BATCH, setup_ack_node_bytes, |bytes| {
black_box(unmarshal_ref(bytes).unwrap());
});
}
#[divan::bench]
fn bench_unmarshal_fanout(bencher: divan::Bencher) {
bench_batched(bencher, BATCH, setup_fanout_node_bytes, |bytes| {
black_box(unmarshal_ref(bytes).unwrap());
});
}
#[divan::bench]
fn bench_unmarshal_large(bencher: divan::Bencher) {
bench_batched(bencher, BATCH, setup_large_node_bytes, |bytes| {
black_box(unmarshal_ref(bytes).unwrap());
});
}
fn setup_uncompressed_payload() -> Vec<u8> {
marshal(&create_large_node()).unwrap()
}
fn setup_compressed_payload() -> Vec<u8> {
let body = node_bytes(&create_usync_like_node());
let mut payload = vec![FORMAT_COMPRESSED];
let mut encoder = ZlibEncoder::new(Vec::new(), Compression::default());
encoder.write_all(&body).unwrap();
payload.extend_from_slice(&encoder.finish().unwrap());
payload
}
#[divan::bench]
fn bench_unpack_uncompressed(bencher: divan::Bencher) {
bench_batched(bencher, 16 * BATCH, setup_uncompressed_payload, |payload| {
black_box(unpack(payload).unwrap());
});
}
#[divan::bench]
fn bench_unpack_compressed(bencher: divan::Bencher) {
bench_batched(bencher, BATCH, setup_compressed_payload, |payload| {
black_box(unpack(payload).unwrap());
});
}
fn setup_attr_node_bytes() -> Vec<u8> {
node_bytes(&create_attr_node())
}
#[divan::bench]
fn bench_attr_parser(bencher: divan::Bencher) {
bench_batched(bencher, BATCH, setup_attr_node_bytes, |bytes| {
let node_ref = unmarshal_ref(bytes).unwrap();
let mut parser = node_ref.attrs();
black_box(parser.optional_string("xmlns"));
black_box(parser.optional_string("type"));
black_box(parser.optional_jid("from"));
black_box(parser.optional_bool("has_flag"));
black_box(parser.optional_u64("timestamp"));
black_box(parser.finish().is_ok());
});
}
#[divan::bench]
fn bench_roundtrip_small(bencher: divan::Bencher) {
bench_batched(bencher, BATCH, setup_small_node_bytes, |bytes| {
let node_ref = unmarshal_ref(bytes).unwrap();
marshal_ref(&node_ref).unwrap()
});
}
#[divan::bench]
fn bench_roundtrip_large(bencher: divan::Bencher) {
bench_batched(bencher, BATCH, setup_large_node_bytes, |bytes| {
let node_ref = unmarshal_ref(bytes).unwrap();
marshal_ref(&node_ref).unwrap()
});
}
#[divan::bench]
fn bench_roundtrip_auto_small(bencher: divan::Bencher) {
bench_batched(bencher, BATCH, setup_small_node_bytes, |bytes| {
let node_ref = unmarshal_ref(bytes).unwrap();
marshal_ref_auto(&node_ref).unwrap()
});
}
#[divan::bench]
fn bench_roundtrip_auto_large(bencher: divan::Bencher) {
bench_batched(bencher, BATCH, setup_large_node_bytes, |bytes| {
let node_ref = unmarshal_ref(bytes).unwrap();
marshal_ref_auto(&node_ref).unwrap()
});
}
#[divan::bench]
fn bench_roundtrip_exact_small(bencher: divan::Bencher) {
bench_batched(bencher, BATCH, setup_small_node_bytes, |bytes| {
let node_ref = unmarshal_ref(bytes).unwrap();
marshal_ref_exact(&node_ref).unwrap()
});
}
#[divan::bench]
fn bench_roundtrip_exact_large(bencher: divan::Bencher) {
bench_batched(bencher, BATCH, setup_large_node_bytes, |bytes| {
let node_ref = unmarshal_ref(bytes).unwrap();
marshal_ref_exact(&node_ref).unwrap()
});
}
#[divan::bench]
fn bench_get_children_by_tag(bencher: divan::Bencher) {
bench_batched(bencher, BATCH, create_usync_like_node, |node| {
let usync = node.get_optional_child("usync").unwrap();
let list = usync.get_optional_child("list").unwrap();
let mut count = 0;
for user in black_box(list.get_children_by_tag("user")) {
if let Some(devices) = user.get_optional_child("devices")
&& let Some(device_list) = devices.get_optional_child("device-list")
{
for _device in black_box(device_list.get_children_by_tag("device")) {
count += 1;
}
}
}
count
});
}
fn setup_jid_heavy_node_bytes() -> Vec<u8> {
node_bytes(&create_jid_heavy_node())
}
#[divan::bench]
fn bench_jid_to_owned_access(bencher: divan::Bencher) {
bench_batched(bencher, BATCH, setup_jid_heavy_node_bytes, |bytes| {
let node_ref = unmarshal_ref(bytes).unwrap();
let node = node_ref.to_owned();
let mut parser = node.attrs();
black_box(parser.optional_jid("from"));
black_box(parser.optional_jid("to"));
black_box(parser.optional_jid("participant"));
black_box(parser.optional_jid("recipient"));
black_box(parser.optional_jid("notify"));
black_box(parser.optional_string("id"));
black_box(parser.optional_string("type"));
node
});
}
const PROPS_CATALOG_CODES: u32 = 2_600;
fn create_props_catalog_node() -> Node {
let props: Vec<Node> = (0..PROPS_CATALOG_CODES)
.map(|i| {
let value = match i % 4 {
0 => "true".to_string(),
1 => "false".to_string(),
2 => (i * 7).to_string(),
_ => format!(
"{:016x}{:016x}",
u64::from(i) * 0x9E37,
u64::from(i) * 0x79B9
),
};
NodeBuilder::new("prop")
.attr("config_code", (1000 + i).to_string())
.attr("config_value", value)
.build()
})
.collect();
NodeBuilder::new("iq")
.attr("from", "s.whatsapp.net")
.attr("type", "result")
.attr("id", "1234-5")
.children(vec![
NodeBuilder::new("props")
.attr("ab_key", "abcdef0123456789")
.attr("hash", "1a2b3c4d")
.attr("refresh", "86400")
.children(props)
.build(),
])
.build()
}
fn setup_props_catalog_payload() -> Vec<u8> {
let body = node_bytes(&create_props_catalog_node());
let mut payload = vec![FORMAT_COMPRESSED];
let mut encoder = ZlibEncoder::new(Vec::new(), Compression::default());
encoder.write_all(&body).unwrap();
payload.extend_from_slice(&encoder.finish().unwrap());
payload
}
fn watched_codes() -> HashSet<u32> {
(0..PROPS_CATALOG_CODES)
.step_by(97)
.map(|i| 1000 + i)
.collect()
}
#[divan::bench]
fn bench_props_catalog_tree(bencher: divan::Bencher) {
let watched = watched_codes();
bencher
.with_inputs(setup_props_catalog_payload)
.bench_refs(|payload| {
let bytes = unpack(black_box(payload)).unwrap();
let root = unmarshal_ref(&bytes).unwrap();
let props = root.get_optional_child("props").unwrap();
let mut kept = 0usize;
for child in props.children().unwrap_or(&[]) {
if child.tag != "prop" {
continue;
}
let Some(code) = child.get_attr("config_code") else {
continue;
};
if let Ok(code) = code.as_str().parse::<u32>()
&& watched.contains(&code)
&& child.get_attr("config_value").is_some()
{
kept += 1;
}
}
black_box(kept)
});
}
#[divan::bench]
fn bench_props_catalog_stream(bencher: divan::Bencher) {
let watched = watched_codes();
bencher
.with_inputs(setup_props_catalog_payload)
.bench_refs(|payload| {
let mut stream = NodeStream::from_packed(black_box(payload)).unwrap();
stream.open().unwrap().unwrap();
let mut kept = 0usize;
loop {
let Some(head) = stream.open().unwrap() else {
break;
};
if head.tag != "props" {
stream.close().unwrap();
continue;
}
while let Some(child) = stream.next_child().unwrap() {
if child.tag != "prop" {
continue;
}
let Some(code) = child.get_attr("config_code") else {
continue;
};
if let Ok(code) = code.as_str().parse::<u32>()
&& watched.contains(&code)
&& child.get_attr("config_value").is_some()
{
kept += 1;
}
}
stream.close().unwrap();
}
stream.finish().unwrap();
black_box(kept)
});
}