Skip to content

Commit

Permalink
wip: migrate benchmarks to use PayloadValue
Browse files Browse the repository at this point in the history
  • Loading branch information
kelnos committed Mar 9, 2024
1 parent ddf322b commit cbeff71
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 38 deletions.
39 changes: 20 additions & 19 deletions socketioxide/benches/packet_decode.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use bytes::Bytes;
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use engineioxide::sid::Sid;
use socketioxide::{
packet::{Packet, PacketData},
ProtocolVersion,
PayloadValue, ProtocolVersion,
};
fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("Decode packet connect on /", |b| {
Expand All @@ -26,7 +27,7 @@ fn criterion_benchmark(c: &mut Criterion) {
const DATA: &str = r#"{"_placeholder":true,"num":0}"#;
const BINARY: [u8; 10] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
c.bench_function("Decode packet event on /", |b| {
let data = serde_json::to_value(DATA).unwrap();
let data = PayloadValue::from_data(DATA).unwrap();
let packet: String =
Packet::event(black_box("/"), black_box("event"), black_box(data.clone()))
.try_into()
Expand All @@ -35,7 +36,7 @@ fn criterion_benchmark(c: &mut Criterion) {
});

c.bench_function("Decode packet event on /custom_nsp", |b| {
let data = serde_json::to_value(DATA).unwrap();
let data = PayloadValue::from_data(DATA).unwrap();
let packet: String = Packet::event(
black_box("custom_nsp"),
black_box("event"),
Expand All @@ -47,7 +48,7 @@ fn criterion_benchmark(c: &mut Criterion) {
});

c.bench_function("Decode packet event with ack on /", |b| {
let data = serde_json::to_value(DATA).unwrap();
let data = PayloadValue::from_data(DATA).unwrap();
let packet: Packet =
Packet::event(black_box("/"), black_box("event"), black_box(data.clone()));
match packet.inner {
Expand All @@ -59,7 +60,7 @@ fn criterion_benchmark(c: &mut Criterion) {
});

c.bench_function("Decode packet event with ack on /custom_nsp", |b| {
let data = serde_json::to_value(DATA).unwrap();
let data = PayloadValue::from_data(DATA).unwrap();
let packet = Packet::event(
black_box("/custom_nsp"),
black_box("event"),
Expand All @@ -75,15 +76,15 @@ fn criterion_benchmark(c: &mut Criterion) {
});

c.bench_function("Decode packet ack on /", |b| {
let data = serde_json::to_value(DATA).unwrap();
let data = PayloadValue::from_data(DATA).unwrap();
let packet: String = Packet::ack(black_box("/"), black_box(data.clone()), black_box(0))
.try_into()
.unwrap();
b.iter(|| Packet::try_from(packet.clone()).unwrap())
});

c.bench_function("Decode packet ack on /custom_nsp", |b| {
let data = serde_json::to_value(DATA).unwrap();
let data = PayloadValue::from_data(DATA).unwrap();
let packet: String = Packet::ack(
black_box("/custom_nsp"),
black_box(data.clone()),
Expand All @@ -95,51 +96,51 @@ fn criterion_benchmark(c: &mut Criterion) {
});

c.bench_function("Decode packet binary event (b64) on /", |b| {
let data = serde_json::to_value(DATA).unwrap();
let packet: String = Packet::bin_event(
let data = PayloadValue::from_data(DATA).unwrap();
let packet: String = Packet::bin_event_with_payloads(
black_box("/"),
black_box("event"),
black_box(data.clone()),
black_box(vec![BINARY.to_vec().clone()]),
black_box(vec![Bytes::from_static(&BINARY)]),
)
.try_into()
.unwrap();
b.iter(|| Packet::try_from(packet.clone()).unwrap())
});

c.bench_function("Decode packet binary event (b64) on /custom_nsp", |b| {
let data = serde_json::to_value(DATA).unwrap();
let packet: String = Packet::bin_event(
let data = PayloadValue::from_data(DATA).unwrap();
let packet: String = Packet::bin_event_with_payloads(
black_box("/custom_nsp"),
black_box("event"),
black_box(data.clone()),
black_box(vec![BINARY.to_vec().clone()]),
black_box(vec![Bytes::from_static(&BINARY)]),
)
.try_into()
.unwrap();
b.iter(|| Packet::try_from(packet.clone()).unwrap())
});

c.bench_function("Decode packet binary ack (b64) on /", |b| {
let data = serde_json::to_value(DATA).unwrap();
let packet: String = Packet::bin_ack(
let data = PayloadValue::from_data(DATA).unwrap();
let packet: String = Packet::bin_ack_with_payloads(
black_box("/"),
black_box(data.clone()),
black_box(vec![BINARY.to_vec().clone()]),
black_box(0),
black_box(vec![Bytes::from_static(&BINARY)]),
)
.try_into()
.unwrap();
b.iter(|| Packet::try_from(packet.clone()).unwrap())
});

c.bench_function("Decode packet binary ack (b64) on /custom_nsp", |b| {
let data = serde_json::to_value(DATA).unwrap();
let packet: String = Packet::bin_ack(
let data = PayloadValue::from_data(DATA).unwrap();
let packet: String = Packet::bin_ack_with_payloads(
black_box("/custom_nsp"),
black_box(data.clone()),
black_box(vec![BINARY.to_vec().clone()]),
black_box(0),
black_box(vec![Bytes::from_static(&BINARY)]),
)
.try_into()
.unwrap();
Expand Down
39 changes: 20 additions & 19 deletions socketioxide/benches/packet_encode.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use bytes::Bytes;
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use engineioxide::sid::Sid;
use socketioxide::{
packet::{Packet, PacketData},
ProtocolVersion,
PayloadValue, ProtocolVersion,
};
fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("Encode packet connect on /", |b| {
Expand All @@ -25,15 +26,15 @@ fn criterion_benchmark(c: &mut Criterion) {
const DATA: &str = r#"{"_placeholder":true,"num":0}"#;
const BINARY: [u8; 10] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
c.bench_function("Encode packet event on /", |b| {
let data = serde_json::to_value(DATA).unwrap();
let data = PayloadValue::from_data(DATA).unwrap();
let packet = Packet::event(black_box("/"), black_box("event"), black_box(data.clone()));
b.iter(|| {
let _: String = packet.clone().try_into().unwrap();
})
});

c.bench_function("Encode packet event on /custom_nsp", |b| {
let data = serde_json::to_value(DATA).unwrap();
let data = PayloadValue::from_data(DATA).unwrap();
let packet = Packet::event(
black_box("custom_nsp"),
black_box("event"),
Expand All @@ -45,7 +46,7 @@ fn criterion_benchmark(c: &mut Criterion) {
});

c.bench_function("Encode packet event with ack on /", |b| {
let data = serde_json::to_value(DATA).unwrap();
let data = PayloadValue::from_data(DATA).unwrap();
let packet = Packet::event(black_box("/"), black_box("event"), black_box(data.clone()));
match packet.inner {
PacketData::Event(_, _, mut ack) => ack.insert(black_box(0)),
Expand All @@ -57,7 +58,7 @@ fn criterion_benchmark(c: &mut Criterion) {
});

c.bench_function("Encode packet event with ack on /custom_nsp", |b| {
let data = serde_json::to_value(DATA).unwrap();
let data = PayloadValue::from_data(DATA).unwrap();
let packet = Packet::event(
black_box("/custom_nsp"),
black_box("event"),
Expand All @@ -73,15 +74,15 @@ fn criterion_benchmark(c: &mut Criterion) {
});

c.bench_function("Encode packet ack on /", |b| {
let data = serde_json::to_value(DATA).unwrap();
let data = PayloadValue::from_data(DATA).unwrap();
let packet = Packet::ack(black_box("/"), black_box(data.clone()), black_box(0));
b.iter(|| {
let _: String = packet.clone().try_into().unwrap();
})
});

c.bench_function("Encode packet ack on /custom_nsp", |b| {
let data = serde_json::to_value(DATA).unwrap();
let data = PayloadValue::from_data(DATA).unwrap();
let packet = Packet::ack(
black_box("/custom_nsp"),
black_box(data.clone()),
Expand All @@ -93,51 +94,51 @@ fn criterion_benchmark(c: &mut Criterion) {
});

c.bench_function("Encode packet binary event (b64) on /", |b| {
let data = serde_json::to_value(DATA).unwrap();
let packet = Packet::bin_event(
let data = PayloadValue::from_data(DATA).unwrap();
let packet = Packet::bin_event_with_payloads(
black_box("/"),
black_box("event"),
black_box(data.clone()),
black_box(vec![BINARY.to_vec().clone()]),
black_box(vec![Bytes::from_static(&BINARY)]),
);
b.iter(|| {
let _: String = packet.clone().try_into().unwrap();
})
});

c.bench_function("Encode packet binary event (b64) on /custom_nsp", |b| {
let data = serde_json::to_value(DATA).unwrap();
let packet = Packet::bin_event(
let data = PayloadValue::from_data(DATA).unwrap();
let packet = Packet::bin_event_with_payloads(
black_box("/custom_nsp"),
black_box("event"),
black_box(data.clone()),
black_box(vec![BINARY.to_vec().clone()]),
black_box(vec![Bytes::from_static(&BINARY)]),
);
b.iter(|| {
let _: String = packet.clone().try_into().unwrap();
})
});

c.bench_function("Encode packet binary ack (b64) on /", |b| {
let data = serde_json::to_value(DATA).unwrap();
let packet = Packet::bin_ack(
let data = PayloadValue::from_data(DATA).unwrap();
let packet = Packet::bin_ack_with_payloads(
black_box("/"),
black_box(data.clone()),
black_box(vec![BINARY.to_vec().clone()]),
black_box(0),
black_box(vec![Bytes::from_static(&BINARY)]),
);
b.iter(|| {
let _: String = packet.clone().try_into().unwrap();
})
});

c.bench_function("Encode packet binary ack (b64) on /custom_nsp", |b| {
let data = serde_json::to_value(DATA).unwrap();
let packet = Packet::bin_ack(
let data = PayloadValue::from_data(DATA).unwrap();
let packet = Packet::bin_ack_with_payloads(
black_box("/custom_nsp"),
black_box(data.clone()),
black_box(vec![BINARY.to_vec().clone()]),
black_box(0),
black_box(vec![Bytes::from_static(&BINARY)]),
);
b.iter(|| {
let _: String = packet.clone().try_into().unwrap();
Expand Down
34 changes: 34 additions & 0 deletions socketioxide/src/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,23 @@ impl<'a> Packet<'a> {
}
}

/// Create a binary event packet for the given namespace, with binary payloads
pub fn bin_event_with_payloads(
ns: impl Into<Cow<'a, str>>,
e: impl Into<Cow<'a, str>>,
data: PayloadValue,
bins: Vec<Bytes>,
) -> Self {
let mut packet = BinaryPacket::outgoing(data);
for bin in bins {
packet.add_payload(bin);
}
Self {
inner: PacketData::BinaryEvent(e.into(), packet, None),
ns: ns.into(),
}
}

/// Create an ack packet for the given namespace
pub fn ack(ns: &'a str, data: PayloadValue, ack: i64) -> Self {
Self {
Expand All @@ -119,6 +136,23 @@ impl<'a> Packet<'a> {
}
}

/// Create a binary ack packet for the given namespace, with binary payloads
pub fn bin_ack_with_payloads(
ns: &'a str,
data: PayloadValue,
ack: i64,
bins: Vec<Bytes>,
) -> Self {
let mut packet = BinaryPacket::outgoing(data);
for bin in bins {
packet.add_payload(bin);
}
Self {
inner: PacketData::BinaryAck(packet, ack),
ns: Cow::Borrowed(ns),
}
}

/// Get the max size the packet could have when serialized
/// This is used to pre-allocate a buffer for the packet
///
Expand Down

0 comments on commit cbeff71

Please sign in to comment.