Debian Codegraph @

crt/s2n/bindings/rust/standard/benchmarks/benches/handshake.rs

raw symbols SCIP
1 // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 use benchmarks::*;
5 use criterion::{
6 criterion_group, criterion_main, measurement::WallTime, BatchSize, BenchmarkGroup, Criterion,
7 };
8 use strum::IntoEnumIterator;
9 use tls_harness::{
10 cohort::{OpenSslConnection, RustlsConnection, S2NConnection},
11 harness::TlsInfo,
12 Mode, SigType, TlsConnPair, TlsConnection,
13 };
14
15 fn bench_handshake_for_library<T>(
16 bench_group: &mut BenchmarkGroup<WallTime>,
17 handshake_type: HandshakeType,
18 kx_group: KXGroup,
19 sig_type: SigType,
20 ) where
21 T: TlsConnection + TlsInfo,
22 T::Config: TlsBenchConfig,
23 {
24 let crypto_config = CryptoConfig::new(CipherSuite::default(), kx_group, sig_type);
25 let client_config =
26 &T::Config::make_config(Mode::Client, crypto_config, handshake_type).unwrap();
27 let server_config =
28 &T::Config::make_config(Mode::Server, crypto_config, handshake_type).unwrap();
29
30 // generate all harnesses (TlsConnPair structs) beforehand so that benchmarks
31 // only include negotiation and not config/connection initialization
32 bench_group.bench_function(T::name(), |b| {
33 b.iter_batched_ref(
34 || -> TlsConnPair<T, T> {
35 if handshake_type == HandshakeType::Resumption {
36 // generate a session ticket to store on the config
37 let mut pair = TlsConnPair::<T, T>::from_configs(client_config, server_config);
38 pair.handshake().unwrap();
39 pair.round_trip_transfer(&mut [0]).unwrap();
40 pair.shutdown().unwrap();
41 }
42 TlsConnPair::from_configs(client_config, server_config)
43 },
44 |conn_pair| {
45 conn_pair.handshake().unwrap();
46 match handshake_type {
47 HandshakeType::ServerAuth | HandshakeType::MutualAuth => {
48 assert!(!conn_pair.server.resumed_connection())
49 }
50 HandshakeType::Resumption => assert!(conn_pair.server.resumed_connection()),
51 }
52 },
53 // Use "PerIteration" benchmarking, because of the way that session
54 // ticket setup interacts with shared configs.
55 // > In testing, the maximum measurement overhead from benchmarking
56 // > with PerIteration is on the order of 350 nanoseconds
57 BatchSize::PerIteration,
58 )
59 });
60 }
61
62 fn bench_handshake_with_params(
63 bench_group: &mut BenchmarkGroup<WallTime>,
64 handshake_type: HandshakeType,
65 kx_group: KXGroup,
66 sig_type: SigType,
67 ) {
68 bench_handshake_for_library::<S2NConnection>(bench_group, handshake_type, kx_group, sig_type);
69 bench_handshake_for_library::<RustlsConnection>(
70 bench_group,
71 handshake_type,
72 kx_group,
73 sig_type,
74 );
75 bench_handshake_for_library::<OpenSslConnection>(
76 bench_group,
77 handshake_type,
78 kx_group,
79 sig_type,
80 );
81 }
82
83 pub fn bench_handshake_types(c: &mut Criterion) {
84 for handshake_type in HandshakeType::iter() {
85 let mut bench_group = c.benchmark_group(format!("handshake-{handshake_type:?}"));
86 bench_handshake_with_params(
87 &mut bench_group,
88 handshake_type,
89 KXGroup::default(),
90 SigType::default(),
91 );
92 }
93 }
94
95 pub fn bench_handshake_kx_groups(c: &mut Criterion) {
96 for kx_group in KXGroup::iter() {
97 let mut bench_group = c.benchmark_group(format!("handshake-{kx_group:?}"));
98 bench_handshake_with_params(
99 &mut bench_group,
100 HandshakeType::default(),
101 kx_group,
102 SigType::default(),
103 );
104 }
105 }
106
107 pub fn bench_handshake_sig_types(c: &mut Criterion) {
108 for sig_type in SigType::iter() {
109 let mut bench_group = c.benchmark_group(format!("handshake-{sig_type:?}"));
110 bench_handshake_with_params(
111 &mut bench_group,
112 HandshakeType::default(),
113 KXGroup::default(),
114 sig_type,
115 );
116 }
117 }
118
119 criterion_group! {
120 benches, bench_handshake_types, bench_handshake_kx_groups, bench_handshake_sig_types
121 }
122 criterion_main!(benches);