-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathcluster_map.rs
More file actions
157 lines (125 loc) · 4.45 KB
/
Copy pathcluster_map.rs
File metadata and controls
157 lines (125 loc) · 4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#![cfg(target_pointer_width = "64")]
use divan::Bencher;
use quilkin::{net::cluster::ClusterMap, xds::Resource};
mod shared;
use shared::TokenKind;
#[divan::bench_group(sample_count = 10)]
mod serde {
use super::*;
use prost_types::Any;
use quilkin::net::cluster::proto::Cluster;
use shared::gen_cluster_map;
fn serialize_to_protobuf(cm: &ClusterMap) -> Vec<Any> {
let mut resources = Vec::new();
for resource in cm.iter_with(|locality, endpoint_set| {
Resource::Cluster(Cluster {
locality: locality.clone().map(From::from),
endpoints: endpoint_set
.endpoints
.iter()
.map(TryFrom::try_from)
.collect::<Result<_, _>>()
.unwrap(),
})
}) {
resources.push(resource.try_encode().unwrap());
}
resources
}
fn deserialize_from_protobuf(pv: Vec<Any>) -> ClusterMap {
let cm = ClusterMap::default();
for any in pv {
let c = quilkin::xds::Resource::try_decode(any).unwrap();
let quilkin::xds::Resource::Cluster(cluster) = c else {
unreachable!()
};
cm.insert(
None,
cluster.locality.map(From::from),
cluster
.endpoints
.into_iter()
.map(TryFrom::try_from)
.collect::<Result<_, _>>()
.unwrap(),
);
}
cm
}
fn serialize_to_json(cm: &ClusterMap) -> serde_json::Value {
serde_json::to_value(cm).unwrap()
}
fn deserialize_from_json(json: serde_json::Value) -> ClusterMap {
serde_json::from_value(json.clone()).unwrap()
}
#[divan::bench(consts = SEEDS)]
fn serialize_proto<const S: u64>(b: Bencher<'_, '_>) {
let gc = gen_cluster_map::<S>(TokenKind::None);
b.counter(gc.total_endpoints)
.bench(|| divan::black_box(serialize_to_protobuf(&gc.cm)));
}
#[divan::bench(consts = SEEDS)]
fn serialize_json<const S: u64>(b: Bencher<'_, '_>) {
let gc = gen_cluster_map::<S>(TokenKind::None);
b.counter(gc.total_endpoints)
.bench(|| divan::black_box(serialize_to_json(&gc.cm)));
}
#[divan::bench(consts = SEEDS)]
fn deserialize_json<const S: u64>(b: Bencher<'_, '_>) {
let gc = gen_cluster_map::<S>(TokenKind::None);
let json = serialize_to_json(&gc.cm);
b.with_inputs(|| json.clone())
.counter(gc.total_endpoints)
.bench_values(|json| divan::black_box(deserialize_from_json(json)));
}
#[divan::bench(consts = SEEDS)]
fn deserialize_proto<const S: u64>(b: Bencher<'_, '_>) {
let gc = gen_cluster_map::<S>(TokenKind::None);
let pv = serialize_to_protobuf(&gc.cm);
b.with_inputs(|| pv.clone())
.counter(gc.total_endpoints)
.bench_values(|pv| divan::black_box(deserialize_from_protobuf(pv)));
}
}
const SEEDS: &[u64] = &[100, 200, 300, 400, 500];
#[divan::bench_group(sample_count = 10)]
mod ops {
use super::*;
use shared::{GenCluster, gen_cluster_map};
fn compute_hash<const S: u64>(gc: &GenCluster) -> usize {
let total_endpoints = gc
.cm
.iter_with(|_locality, endpoint_set| endpoint_set.len())
.iter()
.sum();
assert_eq!(total_endpoints, gc.total_endpoints);
total_endpoints
}
// #[allow(clippy::eq_op)]
// fn is_equal(gc: &GenCluster) -> usize {
// assert_eq!(gc.cm, gc.cm);
// gc.total_endpoints
// }
#[divan::bench(consts = SEEDS)]
fn iterate<const S: u64>(b: Bencher<'_, '_>) {
let cm = gen_cluster_map::<S>(TokenKind::None);
b.counter(cm.total_endpoints)
.bench_local(|| divan::black_box(compute_hash::<S>(&cm)));
drop(cm);
}
#[divan::bench(consts = SEEDS)]
fn iterate_par<const S: u64>(b: Bencher<'_, '_>) {
let cm = gen_cluster_map::<S>(TokenKind::None);
b.counter(cm.total_endpoints)
.bench(|| divan::black_box(compute_hash::<S>(&cm)));
}
// #[divan::bench(consts = SEEDS)]
// fn partial_eq<const S: u64>(b: Bencher) {
// let cm = gen_cluster_map::<S>();
// b.counter(cm.total_endpoints)
// .bench(|| divan::black_box(is_equal(&cm)))
// }
}
fn main() {
divan::main();
}