| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
n |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
use s2n_tls::{ |
| 13 |
config::Config, |
| 14 |
security::DEFAULT_TLS13, |
| 15 |
testing::{TestPair, build_config, config_builder}, |
| 16 |
}; |
| 17 |
use s2n_tls_metrics_subscriber::{ |
| 18 |
AggregatedMetricsSubscriber, Attribution, MetricRecord, TelemetrySink, |
| 19 |
}; |
| 20 |
use tabled::{Table, Tabled, settings::Style}; |
| 21 |
|
| 22 |
#[global_allocator] |
| 23 |
static ALLOC: dhat::Alloc = dhat::Alloc; |
| 24 |
|
| 25 |
const FUZZY_TOLERANCE: i64 = 100; |
| 26 |
|
| 27 |
fn fuzzy_equals(actual: i64, expected: i64) -> bool { |
| 28 |
(actual - expected).abs() <= FUZZY_TOLERANCE |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
#[derive(Clone, Copy, Default)] |
| 34 |
struct NullSink; |
| 35 |
|
| 36 |
impl TelemetrySink for NullSink { |
| 37 |
fn export_record(&self, _record: &MetricRecord) {} |
| 38 |
} |
| 39 |
|
| 40 |
fn attribution() -> Attribution { |
| 41 |
Attribution { |
| 42 |
service: "memtest".to_owned(), |
| 43 |
resource: "memtest".to_owned(), |
| 44 |
component: "memtest".to_owned(), |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
fn run_handshake(client_config: &Config, server_config: &Config) { |
| 49 |
let mut pair = TestPair::from_configs(client_config, server_config); |
| 50 |
pair.handshake().unwrap(); |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
#[derive(Clone, Copy)] |
| 56 |
struct AllocDelta { |
| 57 |
blocks: i64, |
| 58 |
bytes: i64, |
| 59 |
} |
| 60 |
|
| 61 |
impl AllocDelta { |
| 62 |
fn from_total_delta(before: &dhat::HeapStats, after: &dhat::HeapStats) -> Self { |
| 63 |
Self { |
| 64 |
blocks: (after.total_blocks - before.total_blocks) as i64, |
| 65 |
bytes: (after.total_bytes - before.total_bytes) as i64, |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
fn from_curr_delta(before: &dhat::HeapStats, after: &dhat::HeapStats) -> Self { |
| 70 |
Self { |
| 71 |
blocks: after.curr_blocks as i64 - before.curr_blocks as i64, |
| 72 |
bytes: after.curr_bytes as i64 - before.curr_bytes as i64, |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
fn sub(&self, other: &Self) -> Self { |
| 77 |
Self { |
| 78 |
blocks: self.blocks - other.blocks, |
| 79 |
bytes: self.bytes - other.bytes, |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
fn div(&self, factor: i64) -> Self { |
| 84 |
Self { |
| 85 |
blocks: self.blocks / factor, |
| 86 |
bytes: self.bytes / factor, |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
fn measure(n: i64, work: impl Fn()) -> AllocDelta { |
| 93 |
let before = dhat::HeapStats::get(); |
| 94 |
for _ in 0..n { |
| 95 |
work(); |
| 96 |
} |
| 97 |
let after = dhat::HeapStats::get(); |
| 98 |
AllocDelta::from_total_delta(&before, &after).div(n) |
| 99 |
} |
| 100 |
|
| 101 |
#[derive(Tabled)] |
| 102 |
struct BudgetRow { |
| 103 |
phase: &'static str, |
| 104 |
metric: &'static str, |
| 105 |
expected: i64, |
| 106 |
observed: i64, |
| 107 |
delta: i64, |
| 108 |
} |
| 109 |
|
| 110 |
impl BudgetRow { |
| 111 |
fn pair(phase: &'static str, expected: AllocDelta, observed: AllocDelta) -> [Self; 2] { |
| 112 |
[ |
| 113 |
Self { |
| 114 |
phase, |
| 115 |
metric: "blocks", |
| 116 |
expected: expected.blocks, |
| 117 |
observed: observed.blocks, |
| 118 |
delta: observed.blocks - expected.blocks, |
| 119 |
}, |
| 120 |
Self { |
| 121 |
phase, |
| 122 |
metric: "bytes", |
| 123 |
expected: expected.bytes, |
| 124 |
observed: observed.bytes, |
| 125 |
delta: observed.bytes - expected.bytes, |
| 126 |
}, |
| 127 |
] |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
#[test] |
| 132 |
fn subscriber_allocation_budget() { |
| 133 |
let _profiler = dhat::Profiler::builder().testing().build(); |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
const N: i64 = 200; |
| 139 |
|
| 140 |
|
| 141 |
const WARMUP: i64 = 50; |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
const HANDSHAKE: AllocDelta = AllocDelta { |
| 147 |
blocks: 16, |
| 148 |
bytes: 1360, |
| 149 |
}; |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
const EXPORT: AllocDelta = AllocDelta { |
| 159 |
blocks: 5, |
| 160 |
bytes: 4966, |
| 161 |
}; |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
const RETAINED: AllocDelta = AllocDelta { |
| 166 |
blocks: 0, |
| 167 |
bytes: 0, |
| 168 |
}; |
| 169 |
|
| 170 |
let client_config = build_config(&DEFAULT_TLS13).unwrap(); |
| 171 |
let server_config_baseline = config_builder(&DEFAULT_TLS13).unwrap().build().unwrap(); |
| 172 |
let subscriber = AggregatedMetricsSubscriber::new(NullSink, attribution()); |
| 173 |
let server_config_with_sub = { |
| 174 |
let mut builder = config_builder(&DEFAULT_TLS13).unwrap(); |
| 175 |
builder.set_event_subscriber(subscriber.clone()).unwrap(); |
| 176 |
builder.build().unwrap() |
| 177 |
}; |
| 178 |
|
| 179 |
|
| 180 |
measure(WARMUP, || { |
| 181 |
run_handshake(&client_config, &server_config_baseline) |
| 182 |
}); |
| 183 |
measure(WARMUP, || { |
| 184 |
run_handshake(&client_config, &server_config_with_sub) |
| 185 |
}); |
| 186 |
measure(WARMUP, || subscriber.finish_record()); |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
let live_after_setup = dhat::HeapStats::get(); |
| 192 |
|
| 193 |
let baseline = measure(N, || run_handshake(&client_config, &server_config_baseline)); |
| 194 |
let with_sub = measure(N, || run_handshake(&client_config, &server_config_with_sub)); |
| 195 |
let hot_path = with_sub.sub(&baseline); |
| 196 |
|
| 197 |
let export = measure(N, || subscriber.finish_record()); |
| 198 |
|
| 199 |
let live_after_phases = dhat::HeapStats::get(); |
| 200 |
let retained = AllocDelta::from_curr_delta(&live_after_setup, &live_after_phases); |
| 201 |
|
| 202 |
|
| 203 |
as |
| 204 |
|
| 205 |
let rows: Vec<BudgetRow> = [ |
| 206 |
BudgetRow::pair("hot path", HANDSHAKE, hot_path), |
| 207 |
BudgetRow::pair("export", EXPORT, export), |
| 208 |
BudgetRow::pair("retained", RETAINED, retained), |
| 209 |
] |
| 210 |
.into_iter() |
| 211 |
.flatten() |
| 212 |
.collect(); |
| 213 |
let mut table = Table::new(rows); |
| 214 |
table.with(Style::markdown()); |
| 215 |
println!("{table}"); |
| 216 |
|
| 217 |
assert_eq!(hot_path.blocks, HANDSHAKE.blocks); |
| 218 |
assert!(fuzzy_equals(hot_path.bytes, HANDSHAKE.bytes)); |
| 219 |
assert_eq!(export.blocks, EXPORT.blocks); |
| 220 |
assert!(fuzzy_equals(export.bytes, EXPORT.bytes)); |
| 221 |
assert_eq!(retained.blocks, RETAINED.blocks); |
| 222 |
assert_eq!(retained.bytes, RETAINED.bytes); |
| 223 |
} |