Skip to content

Commit

Permalink
fix: benches compile
Browse files Browse the repository at this point in the history
  • Loading branch information
XAMPPRocky committed Oct 11, 2024
1 parent c1e123d commit c0a4fe6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 61 deletions.
29 changes: 14 additions & 15 deletions benches/cluster_map.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![cfg(target_pointer_width = "64")]

use divan::Bencher;
use quilkin::net::cluster::ClusterMap;
use quilkin::{net::cluster::ClusterMap, xds::Resource};

mod shared;

Expand All @@ -16,21 +16,20 @@ mod serde {

fn serialize_to_protobuf(cm: &ClusterMap) -> Vec<Any> {
let mut resources = Vec::new();
let resource_type = quilkin::net::xds::ResourceType::Cluster;

for cluster in cm.iter() {
resources.push(
resource_type
.encode_to_any(&Cluster {
locality: cluster.key().clone().map(From::from),
endpoints: cluster
.endpoints
.iter()
.map(TryFrom::try_from)
.collect::<Result<_, _>>()
.unwrap(),
})
.unwrap(),
Resource::Cluster(Cluster {
locality: cluster.key().clone().map(From::from),
endpoints: cluster
.endpoints
.iter()
.map(TryFrom::try_from)
.collect::<Result<_, _>>()
.unwrap(),
})
.try_encode()
.unwrap(),
);
}

Expand All @@ -41,9 +40,9 @@ mod serde {
let cm = ClusterMap::default();

for any in pv {
let c = quilkin::net::xds::Resource::try_from(any).unwrap();
let c = quilkin::xds::Resource::try_decode(any).unwrap();

let quilkin::net::xds::Resource::Cluster(cluster) = c else {
let quilkin::xds::Resource::Cluster(cluster) = c else {
unreachable!()
};
cm.insert(
Expand Down
67 changes: 21 additions & 46 deletions benches/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

use divan::Bencher;
use prost_types::{value::Kind, Value};
use quilkin::net::cluster::proto::Endpoint as ProtoEndpoint;
use rand::SeedableRng;

use quilkin::{net::cluster::proto::Endpoint as ProtoEndpoint, xds::Resource};

mod shared;

Expand Down Expand Up @@ -146,16 +148,13 @@ trait GenResource: Default {
fn generate(&mut self, slim: bool) -> prost_types::Any;
}

use quilkin::net::xds::{Resource, ResourceType};
use rand::SeedableRng;

#[derive(Default)]
struct Listener {
_counter: usize,
}

impl GenResource for Listener {
fn generate(&mut self, slim: bool) -> prost_types::Any {
fn generate(&mut self, _slim: bool) -> prost_types::Any {
use quilkin::filters::{self, StaticFilter};
let filters = [
quilkin::config::Filter {
Expand Down Expand Up @@ -186,32 +185,17 @@ impl GenResource for Listener {
},
];

if slim {
ResourceType::FilterChain.encode_to_any(&quilkin::net::cluster::proto::FilterChain {
filters: filters
.into_iter()
.map(|f| quilkin::net::cluster::proto::Filter {
name: f.name,
label: f.label,
config: f.config.map(|c| c.to_string()),
})
.collect(),
})
} else {
ResourceType::Listener.encode_to_any(&quilkin::net::xds::listener::Listener {
filter_chains: vec![
quilkin::generated::envoy::config::listener::v3::FilterChain {
filters: filters
.into_iter()
.map(TryFrom::try_from)
.collect::<Result<_, quilkin::filters::CreationError>>()
.unwrap(),
..Default::default()
},
],
..Default::default()
})
}
Resource::FilterChain(quilkin::net::cluster::proto::FilterChain {
filters: filters
.into_iter()
.map(|f| quilkin::net::cluster::proto::Filter {
name: f.name,
label: f.label,
config: f.config.map(|c| c.to_string()),
})
.collect(),
})
.try_encode()
.unwrap()
}
}
Expand Down Expand Up @@ -242,28 +226,22 @@ impl GenResource for Cluster {
.collect(),
};

ResourceType::Cluster.encode_to_any(&msg).unwrap()
Resource::Cluster(msg).try_encode().unwrap()
}
}

// From Config::apply
fn deserialize(a: prost_types::Any) {
match Resource::try_from(a).unwrap() {
Resource::Listener(mut listener) => {
let chain: quilkin::filters::FilterChain = if listener.filter_chains.is_empty() {
match Resource::try_decode(a).unwrap() {
Resource::FilterChain(fc) => {
let chain: quilkin::filters::FilterChain = if fc.filters.is_empty() {
Default::default()
} else {
quilkin::filters::FilterChain::try_create_fallible(
listener.filter_chains.swap_remove(0).filters,
)
.unwrap()
quilkin::filters::FilterChain::try_create_fallible(fc.filters).unwrap()
};

drop(chain);
}
Resource::FilterChain(_fc) => {
unimplemented!("should not be used")
}
Resource::Datacenter(dc) => {
let _host: std::net::IpAddr = dc.host.parse().unwrap();
let _dc = quilkin::config::Datacenter {
Expand All @@ -285,10 +263,7 @@ fn deserialize(a: prost_types::Any) {
}

fn deserialize_faster(a: prost_types::Any) {
match Resource::try_from(a).unwrap() {
Resource::Listener(_listener) => {
unimplemented!("should not be used");
}
match Resource::try_decode(a).unwrap() {
Resource::FilterChain(fc) => {
quilkin::filters::FilterChain::try_create_fallible(fc.filters).unwrap();
}
Expand Down

0 comments on commit c0a4fe6

Please sign in to comment.