Skip to content

Commit

Permalink
Add ssl + hashlib benchmark
Browse files Browse the repository at this point in the history
```
$ python3 --version && uname -a
Python 3.10.12
Linux ip-172-31-89-138 6.2.0-1014-aws python#14~22.04.1-Ubuntu SMP Thu Oct  5 22:43:45 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux

$ ./python bench/benchmarks.py; echo; python3 bench/benchmarks.py
OPENSSL VERSION: AWS-LC 1.19.0
   ALGO     SIZE (B)        TIME (s)
   ====     ========        ========
TLS        0          0.0007833797931671143
TLS        1024       0.0007857415676116944
TLS        1048576    0.002678983211517334
md5        8          1.9640922546386718e-06
md5        1024       3.949642181396484e-06
md5        1048576    0.0019850776195526124
sha1       8          1.847982406616211e-06
sha1       1024       3.45301628112793e-06
sha1       1048576    0.0014366722106933594
sha256     8          1.9931793212890624e-06
sha256     1024       5.559206008911133e-06
sha256     1048576    0.0035069866180419923
sha384     8          2.08735466003418e-06
sha384     1024       4.766225814819336e-06
sha384     1048576    0.0024656279087066652
sha512     8          2.061605453491211e-06
sha512     1024       4.767894744873047e-06
sha512     1048576    0.0024675443172454833
sha3_256   8          2.3877620697021486e-06
sha3_256   1024       6.785154342651367e-06
sha3_256   1048576    0.004528818607330322
sha3_384   8          2.38800048828125e-06
sha3_384   1024       7.86590576171875e-06
sha3_384   1048576    0.0058933842182159425
sha3_512   8          2.4230480194091797e-06
sha3_512   1024       1.0911941528320313e-05
sha3_512   1048576    0.008463276624679565

OPENSSL VERSION: OpenSSL 3.0.2 15 Mar 2022
   ALGO     SIZE (B)        TIME (s)
   ====     ========        ========
TLS        0          0.0018935666084289552
TLS        1024       0.0019144091606140136
TLS        1048576    0.0029557681083679198
md5        8          1.8961429595947266e-06
md5        1024       3.826141357421875e-06
md5        1048576    0.0019944441318511964
sha1       8          1.855611801147461e-06
sha1       1024       3.2887458801269532e-06
sha1       1048576    0.001434556007385254
sha256     8          1.9848346710205076e-06
sha256     1024       5.077362060546875e-06
sha256     1048576    0.003089451551437378
sha384     8          2.099514007568359e-06
sha384     1024       4.216670989990234e-06
sha384     1048576    0.0020866355895996095
sha512     8          2.0647048950195313e-06
sha512     1024       4.217624664306641e-06
sha512     1048576    0.002087009906768799
sha3_256   8          2.408742904663086e-06
sha3_256   1024       5.91588020324707e-06
sha3_256   1048576    0.0037849726676940916
sha3_384   8          2.4039745330810547e-06
sha3_384   1024       6.922245025634765e-06
sha3_384   1048576    0.004906209945678711
sha3_512   8          2.3734569549560547e-06
sha3_512   1024       9.36126708984375e-06
sha3_512   1048576    0.007021739959716797
```
  • Loading branch information
WillChilds-Klein committed Mar 25, 2024
1 parent 6a8c3e7 commit 5554ee9
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 0 deletions.
1 change: 1 addition & 0 deletions bench/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# TODO
81 changes: 81 additions & 0 deletions bench/benchmarks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import csv
import hashlib
import multiprocessing
import socket
import ssl
import time


HOSTNAME = "127.0.0.1"
SERVER_PORT = 4433

HASHES = [
"md5",
"sha1",
"sha256",
"sha384",
"sha512",
"sha3_256",
"sha3_384",
"sha3_512",
]


def main():
server = multiprocessing.Process(target=start_server, daemon=True)
server.start()
time.sleep(0.5) # the server takes a little time to get going.
print(f"OPENSSL VERSION: {ssl.OPENSSL_VERSION}")
print(f"{'ALGO':^10} {'SIZE (B)':^10} {'TIME (s)':^20}")
print(f"{'====':^10} {'========':^10} {'========':^20}")
for size in 0, 1024, 1024**2:
times = [run_client(size) for _ in range(1000)]
print(f"{'TLS':<10} {size:<10} {sum(times)/len(times):<20}")
for h in HASHES:
for size in 8, 1024, 1024**2:
times = [do_hash(h, size) for _ in range(1000)]
print(f"{h:<10} {size:<10} {sum(times)/len(times):<20}")
print()


def do_hash(h: str, size: int) -> int:
start = time.time()
digest = hashlib.new(h)
digest.update(b"X" * size)
digest.digest()
end = time.time()
return end - start


def run_client(size: int) -> int:
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
ctx.load_verify_locations("./bench/certs/CA.crt")
ctx.check_hostname = False
start = time.time()
with ctx.wrap_socket(socket.socket(socket.AF_INET)) as conn:
conn.connect((HOSTNAME, SERVER_PORT))
if size > 0:
conn.sendall(b"X" * size)
end = time.time()
return end - start


def start_server():
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
ctx.load_cert_chain("./bench/certs/server.crt", "./bench/certs/server.key")
with socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) as sock:
sock.bind((HOSTNAME, SERVER_PORT))
sock.listen()
with ctx.wrap_socket(sock, server_side=True) as ssock:
while True:
try:
conn, addr = ssock.accept()
except ssl.SSLEOFError:
continue
data = conn.recv(1024)
while data:
data = conn.recv(1024)


if __name__ == "__main__":
main()
11 changes: 11 additions & 0 deletions bench/certs/CA.crt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-----BEGIN CERTIFICATE-----
MIIBljCCATugAwIBAgIUUlYFwDn054PXqbIMrrSUQ6HU8MowCgYIKoZIzj0EAwIw
IDEeMBwGA1UEAwwVT1FTIHRlc3QgZWNkc2FwMjU2IENBMB4XDTIzMTIxMjE3MDkw
MVoXDTI0MTIxMTE3MDkwMVowIDEeMBwGA1UEAwwVT1FTIHRlc3QgZWNkc2FwMjU2
IENBMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEJFgg8kGusiGvm+C5QeYhV0UZ
qlMdkzHge7zAV3YRTA/g5exs5EujCVOY2tGIy0iKuEGP1M+toMbYmi7Vxb8HqKNT
MFEwHQYDVR0OBBYEFDIAMFug9yRwdAANnGCwu6mzGSagMB8GA1UdIwQYMBaAFDIA
MFug9yRwdAANnGCwu6mzGSagMA8GA1UdEwEB/wQFMAMBAf8wCgYIKoZIzj0EAwID
SQAwRgIhAMIgAP5l7af9+gi1Yt1SR/fT/9PzAs/O0Tsapif3/RNgAiEAwe6MuVYu
GdrEYpxX97yBSe2wioXrocda7CgDpUUIkaA=
-----END CERTIFICATE-----
5 changes: 5 additions & 0 deletions bench/certs/CA.key
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgFiXZaFCibucFMdxr
WLkS9cLZor7Xxs3x4mi3RvyvbcShRANCAAQkWCDyQa6yIa+b4LlB5iFXRRmqUx2T
MeB7vMBXdhFMD+Dl7GzkS6MJU5ja0YjLSIq4QY/Uz62gxtiaLtXFvweo
-----END PRIVATE KEY-----
9 changes: 9 additions & 0 deletions bench/certs/server.crt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-----BEGIN CERTIFICATE-----
MIIBOTCB4AIUZcuEo7Ou/R/+TLY3VtGOplZZqlgwCgYIKoZIzj0EAwIwIDEeMBwG
A1UEAwwVT1FTIHRlc3QgZWNkc2FwMjU2IENBMB4XDTIzMTIxMjE3MDkwMVoXDTI0
MTIxMTE3MDkwMVowHzEdMBsGA1UEAwwUb3FzdGVzdCBDQSBlY2RzYXAyNTYwWTAT
BgcqhkjOPQIBBggqhkjOPQMBBwNCAASszcOdq5lgz1JdaYnCcdRVS0ELj2+37PpS
ypNGmxuF5WDjMj/519xylbhOWQkx4/T3ZTNEDoVda5YFsJ4AJx/xMAoGCCqGSM49
BAMCA0gAMEUCICMcvqeV5ygmyIv7Zbaq+kKPUE5cA48jlHNQwQTh17VxAiEA+pZ5
FnKRW0xI90QHYL6Sy+B2gUpDA6bbRXs7EypeVA0=
-----END CERTIFICATE-----
7 changes: 7 additions & 0 deletions bench/certs/server.csr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-----BEGIN CERTIFICATE REQUEST-----
MIHZMIGBAgEAMB8xHTAbBgNVBAMMFG9xc3Rlc3QgQ0EgZWNkc2FwMjU2MFkwEwYH
KoZIzj0CAQYIKoZIzj0DAQcDQgAErM3DnauZYM9SXWmJwnHUVUtBC49vt+z6UsqT
RpsbheVg4zI/+dfccpW4TlkJMeP092UzRA6FXWuWBbCeACcf8aAAMAoGCCqGSM49
BAMCA0cAMEQCICCwHKUCevppkp8mIJ/i0+H1zR2SmmL6XDJ2DuDpIG6ZAiA3ihSC
bTIXJe527hRxruB5937YwYlq1SVDqSjOyDh3Zw==
-----END CERTIFICATE REQUEST-----
5 changes: 5 additions & 0 deletions bench/certs/server.key
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgRqyThi/VpmwRI5Wm
845G8JK2hQo5aR+F323suEOIkhKhRANCAASszcOdq5lgz1JdaYnCcdRVS0ELj2+3
7PpSypNGmxuF5WDjMj/519xylbhOWQkx4/T3ZTNEDoVda5YFsJ4AJx/x
-----END PRIVATE KEY-----
5 changes: 5 additions & 0 deletions bench/run_benchmarks.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env bash

BUILD_DIR='build/bench/'

./python bench/benchmarks.py

0 comments on commit 5554ee9

Please sign in to comment.