| 1 | #!/usr/bin/env perl | ||||||||||
| 2 | 1 | ▶ | use v5.36; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 1 | ▶ | use FindBin qw($Bin); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use Getopt::Long qw(GetOptions); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use JSON::PP qw(encode_json); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use Time::HiRes qw(clock_gettime CLOCK_MONOTONIC CLOCK_PROCESS_CPUTIME_ID); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 1 | ▶ | use Linux::Event::IO::Sock::Stream; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 1 | ▶ | use Linux::Event::TLS; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | { | ||||||||||
| 15 | package Linux::Event::TLS::Bench::ServerStream; | ||||||||||
| 16 | 1 | ▶ | use parent 'Linux::Event::IO::Sock::Stream'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | |||||||||||
| 18 | 1 | ▶ | sub tls_defaults ($class) { | ||||||||
|
Subroutine
tls_defaults: called CC=1 SCAR=0.0
| |||||||||||
| 19 | 1 | return alpn => ['tls-setup-bench/1']; | |||||||||
| 20 | } | ||||||||||
| 21 | |||||||||||
| 22 | 0 | ▶ | sub on_data ($stream, $bytes) { return } | ||||||||
|
Subroutine
on_data: not called CC=1 SCAR=6.9
| |||||||||||
| 23 | } | ||||||||||
| 24 | |||||||||||
| 25 | 1 | my $iterations = 1_000; | |||||||||
| 26 | 1 | my $repeats = 7; | |||||||||
| 27 | 1 | my $json_file; | |||||||||
| 28 | 1 | my $cert_file = "$Bin/../t/tls-certs/server-cert.pem"; | |||||||||
| 29 | 1 | my $key_file = "$Bin/../t/tls-certs/server-key.pem"; | |||||||||
| 30 | |||||||||||
| 31 | 1 | ▶ | GetOptions( | ||||||||
|
Branch: 1 branch
| |||||||||||
| 32 | 'iterations=i' => \$iterations, | ||||||||||
| 33 | 'repeats=i' => \$repeats, | ||||||||||
| 34 | 'json=s' => \$json_file, | ||||||||||
| 35 | 'cert-file=s' => \$cert_file, | ||||||||||
| 36 | 'key-file=s' => \$key_file, | ||||||||||
| 37 | ) or die "bad options\n"; | ||||||||||
| 38 | |||||||||||
| 39 | 1 | ▶ | die "iterations must be > 0\n" if $iterations <= 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 40 | 1 | ▶ | die "repeats must be > 0\n" if $repeats <= 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | 1 | ▶ | die "TLS certificate file not found: $cert_file\n" if !-f $cert_file; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | 1 | ▶ | die "TLS private-key file not found: $key_file\n" if !-f $key_file; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | |||||||||||
| 44 | 1 | my $template_wall_start = clock_gettime(CLOCK_MONOTONIC); | |||||||||
| 45 | 1 | my $template_cpu_start = clock_gettime(CLOCK_PROCESS_CPUTIME_ID); | |||||||||
| 46 | 1 | my $template = Linux::Event::TLS->_prepare_listener_server( | |||||||||
| 47 | 'Linux::Event::TLS::Bench::ServerStream', | ||||||||||
| 48 | { | ||||||||||
| 49 | cert_file => $cert_file, | ||||||||||
| 50 | key_file => $key_file, | ||||||||||
| 51 | }, | ||||||||||
| 52 | ); | ||||||||||
| 53 | 1 | my $template_cpu = clock_gettime(CLOCK_PROCESS_CPUTIME_ID) - $template_cpu_start; | |||||||||
| 54 | 1 | my $template_wall = clock_gettime(CLOCK_MONOTONIC) - $template_wall_start; | |||||||||
| 55 | |||||||||||
| 56 | 1 | my @systems = qw(fresh_context prepared_clone); | |||||||||
| 57 | 1 | my @rows; | |||||||||
| 58 | |||||||||||
| 59 | 1 | say 'TLS accepted-connection setup benchmark'; | |||||||||
| 60 | 1 | say "iterations=$iterations repeats=$repeats"; | |||||||||
| 61 | 1 | printf "prepared server context once: %.3f ms wall %.3f ms cpu\n", | |||||||||
| 62 | $template_wall * 1_000, $template_cpu * 1_000; | ||||||||||
| 63 | |||||||||||
| 64 | 1 | for my $repeat (1 .. $repeats) { | |||||||||
| 65 | 1 | ▶ | my @order = $repeat % 2 ? @systems : reverse @systems; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | 1 | for my $system (@order) { | |||||||||
| 67 | 2 | my $wall_start = clock_gettime(CLOCK_MONOTONIC); | |||||||||
| 68 | 2 | my $cpu_start = clock_gettime(CLOCK_PROCESS_CPUTIME_ID); | |||||||||
| 69 | |||||||||||
| 70 | 2 | ▶ | if ($system eq 'fresh_context') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | 1 | for (1 .. $iterations) { | |||||||||
| 72 | 4 | my $transport = Linux::Event::TLS->server( | |||||||||
| 73 | cert_file => $cert_file, | ||||||||||
| 74 | key_file => $key_file, | ||||||||||
| 75 | alpn => ['tls-setup-bench/1'], | ||||||||||
| 76 | ); | ||||||||||
| 77 | 4 | undef $transport; | |||||||||
| 78 | } | ||||||||||
| 79 | } else { | ||||||||||
| 80 | 1 | for (1 .. $iterations) { | |||||||||
| 81 | 4 | my $transport = Linux::Event::TLS->_listener_server_connection( | |||||||||
| 82 | $template, | ||||||||||
| 83 | ); | ||||||||||
| 84 | 4 | undef $transport; | |||||||||
| 85 | } | ||||||||||
| 86 | } | ||||||||||
| 87 | |||||||||||
| 88 | 2 | my $cpu = clock_gettime(CLOCK_PROCESS_CPUTIME_ID) - $cpu_start; | |||||||||
| 89 | 2 | my $wall = clock_gettime(CLOCK_MONOTONIC) - $wall_start; | |||||||||
| 90 | 2 | my $row = { | |||||||||
| 91 | system => $system, | ||||||||||
| 92 | repeat => $repeat, | ||||||||||
| 93 | iterations => $iterations, | ||||||||||
| 94 | wall_seconds => $wall, | ||||||||||
| 95 | cpu_seconds => $cpu, | ||||||||||
| 96 | setups_per_second => $iterations / $wall, | ||||||||||
| 97 | wall_us_per_setup => $wall * 1_000_000 / $iterations, | ||||||||||
| 98 | cpu_us_per_setup => $cpu * 1_000_000 / $iterations, | ||||||||||
| 99 | }; | ||||||||||
| 100 | 2 | push @rows, $row; | |||||||||
| 101 | printf "%s repeat=%d %.1f setup/s wall=%.3f us/setup cpu=%.3f us/setup\n", | ||||||||||
| 102 | $system, $repeat, $row->{setups_per_second}, | ||||||||||
| 103 | 2 | $row->{wall_us_per_setup}, $row->{cpu_us_per_setup}; | |||||||||
| 104 | } | ||||||||||
| 105 | } | ||||||||||
| 106 | |||||||||||
| 107 | 1 | say "\nMedian TLS setup summary"; | |||||||||
| 108 | 1 | printf "%-15s %14s %16s %16s\n", | |||||||||
| 109 | 'system', 'setup/s', 'wall us/setup', 'cpu us/setup'; | ||||||||||
| 110 | 1 | for my $system (@systems) { | |||||||||
| 111 | 2 | my @set = grep { $_->{system} eq $system } @rows; | |||||||||
| 112 | printf "%-15s %14.1f %16.3f %16.3f\n", | ||||||||||
| 113 | $system, | ||||||||||
| 114 | 2 | median(map { $_->{setups_per_second} } @set), | |||||||||
| 115 | 2 | median(map { $_->{wall_us_per_setup} } @set), | |||||||||
| 116 | 2 | median(map { $_->{cpu_us_per_setup} } @set); | |||||||||
| 117 | } | ||||||||||
| 118 | |||||||||||
| 119 | 1 | ▶ | if (defined $json_file) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 120 | 0 | ▶ | open my $json_fh, '>', $json_file | ||||||||
|
Branch: 1 branch
| |||||||||||
| 121 | or die "open $json_file: $!\n"; | ||||||||||
| 122 | 0 | print {$json_fh} encode_json({ | |||||||||
| 123 | benchmark => 'linux-event-tls-accepted-connection-setup', | ||||||||||
| 124 | iterations => $iterations, | ||||||||||
| 125 | repeats => $repeats, | ||||||||||
| 126 | prepared_context_wall_seconds => $template_wall, | ||||||||||
| 127 | prepared_context_cpu_seconds => $template_cpu, | ||||||||||
| 128 | rows => \@rows, | ||||||||||
| 129 | }); | ||||||||||
| 130 | 0 | print {$json_fh} "\n"; | |||||||||
| 131 | 0 | ▶ | close $json_fh or die "close $json_file: $!\n"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 132 | } | ||||||||||
| 133 | |||||||||||
| 134 | 1 | say "\nfresh_context constructs and loads a server SSL_CTX for every connection."; | |||||||||
| 135 | 1 | say "prepared_clone reuses one Listener-style prepared SSL_CTX and allocates"; | |||||||||
| 136 | 1 | say "only independent per-connection TLS state. No handshake is timed."; | |||||||||
| 137 | |||||||||||
| 138 | 6 | ▶ | sub median (@values) { | ||||||||
|
Subroutine
median: called CC=2 SCAR=9.2
| |||||||||||
| 139 | 0 | @values = sort { $a <=> $b } @values; | |||||||||
| 140 | 6 | ▶ | return $values[int(@values / 2)] if @values % 2; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 141 | 0 | return ($values[@values / 2 - 1] + $values[@values / 2]) / 2; | |||||||||
| 142 | } | ||||||||||