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
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
#[cfg(feature = "openssl")]
use bench::OpenSslConnection;
#[cfg(feature = "rustls")]
use bench::RustlsConnection;
use bench::{
harness::TlsBenchConfig, CipherSuite, ConnectedBuffer, CryptoConfig, HandshakeType, KXGroup,
Mode, S2NConnection, SigType, TlsConnPair, TlsConnection, PROFILER_FREQUENCY,
};
use criterion::{
criterion_group, criterion_main, measurement::WallTime, BatchSize, BenchmarkGroup, Criterion,
Throughput,
};
use pprof::criterion::{Output, PProfProfiler};
use std::error::Error;
use strum::IntoEnumIterator;
fn bench_throughput_for_library<T>(
bench_group: &mut BenchmarkGroup<WallTime>,
shared_buf: &mut [u8],
cipher_suite: CipherSuite,
) where
T: TlsConnection,
T::Config: TlsBenchConfig,
{
let crypto_config = CryptoConfig::new(cipher_suite, KXGroup::default(), SigType::default());
let client_config = T::Config::make_config(Mode::Client, crypto_config, HandshakeType::default());
let server_config = T::Config::make_config(Mode::Server, crypto_config, HandshakeType::default());
bench_group.bench_function(T::name(), |b| {
b.iter_batched_ref(
|| -> Result<TlsConnPair<T, T>, Box<dyn Error>> {
if let (Ok(client_config), Ok(server_config)) =
(client_config.as_ref(), server_config.as_ref())
{
let connected_buffer = ConnectedBuffer::default();
let client =
T::new_from_config(client_config, connected_buffer.clone_inverse())?;
let server = T::new_from_config(server_config, connected_buffer)?;
let mut conn_pair = TlsConnPair::wrap(client, server);
conn_pair.handshake()?;
Ok(conn_pair)
} else {
Err("invalid configs".into())
}
},
|conn_pair| {
if let Ok(conn_pair) = conn_pair {
let _ = conn_pair.round_trip_transfer(shared_buf);
}
},
BatchSize::SmallInput,
)
});
}
pub fn bench_throughput_cipher_suites(c: &mut Criterion) {
// arbitrarily large to cut across TLS record boundaries
let mut shared_buf = [0u8; 100000];
for cipher_suite in CipherSuite::iter() {
let mut bench_group = c.benchmark_group(format!("throughput-{:?}", cipher_suite));
bench_group.throughput(Throughput::Bytes(shared_buf.len() as u64));
bench_throughput_for_library::<S2NConnection>(
&mut bench_group,
&mut shared_buf,
cipher_suite,
);
#[cfg(feature = "rustls")]
bench_throughput_for_library::<RustlsConnection>(
&mut bench_group,
&mut shared_buf,
cipher_suite,
);
#[cfg(feature = "openssl")]
bench_throughput_for_library::<OpenSslConnection>(
&mut bench_group,
&mut shared_buf,
cipher_suite,
);
}
}
criterion_group! {
name = benches;
// profile 100 samples/sec
config = Criterion::default().with_profiler(PProfProfiler::new(PROFILER_FREQUENCY, Output::Flamegraph(None)));
targets = bench_throughput_cipher_suites
}
criterion_main!(benches);
|