|
| 1 | +// Copyright AGNTCY Contributors (https://github.com/agntcy) |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +use criterion::{Criterion, black_box, criterion_group, criterion_main}; |
| 5 | +use slim_datapath::api::{EncodedName, ProtoName, SlimHeader, StringName}; |
| 6 | +use slim_datapath::messages::encoder::Name; |
| 7 | +use slim_datapath::tables::SubscriptionTable; |
| 8 | +use slim_datapath::tables::subscription_table::SubscriptionTableImpl; |
| 9 | + |
| 10 | +fn make_proto_name() -> ProtoName { |
| 11 | + ProtoName { |
| 12 | + name: Some(EncodedName { |
| 13 | + component_0: 0x1234_5678_9abc_def0, |
| 14 | + component_1: 0xfeed_cafe_dead_beef, |
| 15 | + component_2: 0x0101_0101_0101_0101, |
| 16 | + component_3: u64::MAX, |
| 17 | + }), |
| 18 | + str_name: Some(StringName { |
| 19 | + str_component_0: "org".to_string(), |
| 20 | + str_component_1: "namespace".to_string(), |
| 21 | + str_component_2: "agent".to_string(), |
| 22 | + }), |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +/// Build a SlimHeader whose destination matches the subscription used in routing benchmarks |
| 27 | +/// (org/namespace/agent, id=42). Hashes are derived from Name::from_strings to ensure they |
| 28 | +/// match whatever SubscriptionTableImpl stores. |
| 29 | +fn make_slim_header() -> SlimHeader { |
| 30 | + let name = Name::from_strings(["org", "namespace", "agent"]).with_id(42); |
| 31 | + let c = name.components(); |
| 32 | + let proto_name = ProtoName { |
| 33 | + name: Some(EncodedName { |
| 34 | + component_0: c[0], |
| 35 | + component_1: c[1], |
| 36 | + component_2: c[2], |
| 37 | + component_3: c[3], |
| 38 | + }), |
| 39 | + str_name: Some(StringName { |
| 40 | + str_component_0: "org".to_string(), |
| 41 | + str_component_1: "namespace".to_string(), |
| 42 | + str_component_2: "agent".to_string(), |
| 43 | + }), |
| 44 | + }; |
| 45 | + SlimHeader { |
| 46 | + source: Some(proto_name.clone()), |
| 47 | + destination: Some(proto_name), |
| 48 | + identity: String::new(), |
| 49 | + fanout: 1, |
| 50 | + recv_from: None, |
| 51 | + forward_to: None, |
| 52 | + incoming_conn: None, |
| 53 | + error: None, |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +fn bench_name_from_strings(c: &mut Criterion) { |
| 58 | + c.bench_function("name_from_strings", |b| { |
| 59 | + b.iter(|| black_box(Name::from_strings(black_box(["org", "namespace", "agent"])))) |
| 60 | + }); |
| 61 | +} |
| 62 | + |
| 63 | +fn bench_name_from_proto(c: &mut Criterion) { |
| 64 | + let proto_name = make_proto_name(); |
| 65 | + c.bench_function("name_from_proto", |b| { |
| 66 | + b.iter(|| black_box(Name::from(black_box(&proto_name)))) |
| 67 | + }); |
| 68 | +} |
| 69 | + |
| 70 | +fn bench_name_clone_with_strings(c: &mut Criterion) { |
| 71 | + let name = Name::from_strings(["org", "namespace", "agent"]); |
| 72 | + c.bench_function("name_clone_with_strings", |b| { |
| 73 | + b.iter(|| black_box(name.clone())) |
| 74 | + }); |
| 75 | +} |
| 76 | + |
| 77 | +/// Baseline: full routing flow via the Name-based path. |
| 78 | +/// get_dst() allocates Arc<[String; 3]> + clones 3 Strings. |
| 79 | +fn bench_routing_via_name(c: &mut Criterion) { |
| 80 | + let table = SubscriptionTableImpl::default(); |
| 81 | + let sub = Name::from_strings(["org", "namespace", "agent"]).with_id(42); |
| 82 | + table.add_subscription(sub, 1, true, 1).unwrap(); |
| 83 | + let slim_header = make_slim_header(); |
| 84 | + |
| 85 | + c.bench_function("routing_via_name", |b| { |
| 86 | + b.iter(|| { |
| 87 | + let dst = black_box(slim_header.get_dst()); |
| 88 | + black_box(table.match_one(black_box(&EncodedName::from(&dst)), 0)) |
| 89 | + }) |
| 90 | + }); |
| 91 | +} |
| 92 | + |
| 93 | +/// Optimized: full routing flow via the EncodedName path. |
| 94 | +/// get_encoded_dst() is a Copy of 4 u64s — zero heap allocation. |
| 95 | +fn bench_routing_via_encoded_name(c: &mut Criterion) { |
| 96 | + let table = SubscriptionTableImpl::default(); |
| 97 | + let sub = Name::from_strings(["org", "namespace", "agent"]).with_id(42); |
| 98 | + table.add_subscription(sub, 1, true, 1).unwrap(); |
| 99 | + let slim_header = make_slim_header(); |
| 100 | + |
| 101 | + c.bench_function("routing_via_encoded_name", |b| { |
| 102 | + b.iter(|| { |
| 103 | + let encoded = black_box(slim_header.get_encoded_dst()); |
| 104 | + black_box(table.match_one(black_box(&encoded), 0)) |
| 105 | + }) |
| 106 | + }); |
| 107 | +} |
| 108 | + |
| 109 | +criterion_group!( |
| 110 | + benches, |
| 111 | + bench_name_from_strings, |
| 112 | + bench_name_from_proto, |
| 113 | + bench_name_clone_with_strings, |
| 114 | + bench_routing_via_name, |
| 115 | + bench_routing_via_encoded_name, |
| 116 | +); |
| 117 | + |
| 118 | +criterion_main!(benches); |
0 commit comments