Skip to content

Commit 152d9b9

Browse files
Add ssl + hashlib benchmark
``` $ 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 $ echo AWS-LC PYTHON; echo; ./python bench/benchmarks.py; echo; echo SYSTEM PYTHON; echo; python3 bench/benchmarks.py AWS-LC PYTHON ALGO SIZE (B) TIME (s) ==== ======== ======== TLS 0 0.000771021842956543 TLS 1024 0.0007800400257110595 TLS 1048576 0.0026435813903808595 md5 8 1.9125938415527344e-06 md5 1024 3.813743591308594e-06 md5 1048576 0.0019908883571624755 sha1 8 1.8870830535888672e-06 sha1 1024 3.3450126647949217e-06 sha1 1048576 0.001439645290374756 sha256 8 1.9631385803222656e-06 sha256 1024 5.374908447265625e-06 sha256 1048576 0.0034945359230041505 sha384 8 2.081632614135742e-06 sha384 1024 4.610776901245117e-06 sha384 1048576 0.002470192193984985 sha512 8 2.0771026611328124e-06 sha512 1024 4.634857177734375e-06 sha512 1048576 0.002472785234451294 sha3_256 8 2.4628639221191406e-06 sha3_256 1024 6.587982177734375e-06 sha3_256 1048576 0.0045334467887878415 sha3_384 8 2.4132728576660156e-06 sha3_384 1024 7.719039916992187e-06 sha3_384 1048576 0.005894896745681763 sha3_512 8 2.4335384368896486e-06 sha3_512 1024 1.061868667602539e-05 sha3_512 1048576 0.008451510906219482 SYSTEM PYTHON ALGO SIZE (B) TIME (s) ==== ======== ======== TLS 0 0.0019146842956542969 TLS 1024 0.0019197502136230468 TLS 1048576 0.00295937442779541 md5 8 1.884937286376953e-06 md5 1024 3.789663314819336e-06 md5 1048576 0.00199932861328125 sha1 8 1.8317699432373046e-06 sha1 1024 3.321409225463867e-06 sha1 1048576 0.0014382312297821045 sha256 8 2.0177364349365232e-06 sha256 1024 5.11932373046875e-06 sha256 1048576 0.003096806049346924 sha384 8 2.1207332611083983e-06 sha384 1024 4.274129867553711e-06 sha384 1048576 0.0020926313400268554 sha512 8 2.153873443603516e-06 sha512 1024 4.2979717254638674e-06 sha512 1048576 0.0020925924777984617 sha3_256 8 2.3796558380126953e-06 sha3_256 1024 5.854606628417969e-06 sha3_256 1048576 0.003778960466384888 sha3_384 8 2.38037109375e-06 sha3_384 1024 6.821155548095703e-06 sha3_384 1048576 0.004898728609085083 sha3_512 8 2.3713111877441406e-06 sha3_512 1024 9.174823760986328e-06 sha3_512 1048576 0.007007023334503174 ```
1 parent 86915ff commit 152d9b9

File tree

8 files changed

+122
-0
lines changed

8 files changed

+122
-0
lines changed

bench/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# TODO

bench/benchmarks.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import csv
2+
import hashlib
3+
import multiprocessing
4+
import socket
5+
import ssl
6+
import time
7+
8+
9+
HOSTNAME = "127.0.0.1"
10+
SERVER_PORT = 4433
11+
12+
HASHES = [
13+
"md5",
14+
"sha1",
15+
"sha256",
16+
"sha384",
17+
"sha512",
18+
"sha3_256",
19+
"sha3_384",
20+
"sha3_512",
21+
]
22+
23+
24+
def main():
25+
server = multiprocessing.Process(target=start_server, daemon=True)
26+
server.start()
27+
time.sleep(0.5) # the server takes a little time to get going.
28+
print(f"{'ALGO':^10} {'SIZE (B)':^10} {'TIME (s)':^20}")
29+
print(f"{'====':^10} {'========':^10} {'========':^20}")
30+
for size in 0, 1024, 1024**2:
31+
times = [run_client(size) for _ in range(1000)]
32+
print(f"{'TLS':<10} {size:<10} {sum(times)/len(times):<20}")
33+
for h in HASHES:
34+
for size in 8, 1024, 1024**2:
35+
times = [do_hash(h, size) for _ in range(1000)]
36+
print(f"{h:<10} {size:<10} {sum(times)/len(times):<20}")
37+
38+
39+
def do_hash(h: str, size: int) -> int:
40+
start = time.time()
41+
digest = hashlib.new(h)
42+
digest.update(b"X" * size)
43+
digest.digest()
44+
end = time.time()
45+
return end - start
46+
47+
48+
def run_client(size: int) -> int:
49+
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
50+
ctx.load_verify_locations("./bench/certs/CA.crt")
51+
ctx.check_hostname = False
52+
start = time.time()
53+
with ctx.wrap_socket(socket.socket(socket.AF_INET)) as conn:
54+
conn.connect((HOSTNAME, SERVER_PORT))
55+
if size > 0:
56+
conn.sendall(b"X" * size)
57+
end = time.time()
58+
return end - start
59+
60+
61+
def start_server():
62+
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
63+
ctx.load_cert_chain("./bench/certs/server.crt", "./bench/certs/server.key")
64+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0) as sock:
65+
sock.bind((HOSTNAME, SERVER_PORT))
66+
sock.listen()
67+
with ctx.wrap_socket(sock, server_side=True) as ssock:
68+
while True:
69+
try:
70+
conn, addr = ssock.accept()
71+
except ssl.SSLEOFError:
72+
continue
73+
data = conn.recv(1024)
74+
while data:
75+
data = conn.recv(1024)
76+
77+
78+
if __name__ == "__main__":
79+
main()

bench/certs/CA.crt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-----BEGIN CERTIFICATE-----
2+
MIIBljCCATugAwIBAgIUUlYFwDn054PXqbIMrrSUQ6HU8MowCgYIKoZIzj0EAwIw
3+
IDEeMBwGA1UEAwwVT1FTIHRlc3QgZWNkc2FwMjU2IENBMB4XDTIzMTIxMjE3MDkw
4+
MVoXDTI0MTIxMTE3MDkwMVowIDEeMBwGA1UEAwwVT1FTIHRlc3QgZWNkc2FwMjU2
5+
IENBMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEJFgg8kGusiGvm+C5QeYhV0UZ
6+
qlMdkzHge7zAV3YRTA/g5exs5EujCVOY2tGIy0iKuEGP1M+toMbYmi7Vxb8HqKNT
7+
MFEwHQYDVR0OBBYEFDIAMFug9yRwdAANnGCwu6mzGSagMB8GA1UdIwQYMBaAFDIA
8+
MFug9yRwdAANnGCwu6mzGSagMA8GA1UdEwEB/wQFMAMBAf8wCgYIKoZIzj0EAwID
9+
SQAwRgIhAMIgAP5l7af9+gi1Yt1SR/fT/9PzAs/O0Tsapif3/RNgAiEAwe6MuVYu
10+
GdrEYpxX97yBSe2wioXrocda7CgDpUUIkaA=
11+
-----END CERTIFICATE-----

bench/certs/CA.key

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-----BEGIN PRIVATE KEY-----
2+
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgFiXZaFCibucFMdxr
3+
WLkS9cLZor7Xxs3x4mi3RvyvbcShRANCAAQkWCDyQa6yIa+b4LlB5iFXRRmqUx2T
4+
MeB7vMBXdhFMD+Dl7GzkS6MJU5ja0YjLSIq4QY/Uz62gxtiaLtXFvweo
5+
-----END PRIVATE KEY-----

bench/certs/server.crt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-----BEGIN CERTIFICATE-----
2+
MIIBOTCB4AIUZcuEo7Ou/R/+TLY3VtGOplZZqlgwCgYIKoZIzj0EAwIwIDEeMBwG
3+
A1UEAwwVT1FTIHRlc3QgZWNkc2FwMjU2IENBMB4XDTIzMTIxMjE3MDkwMVoXDTI0
4+
MTIxMTE3MDkwMVowHzEdMBsGA1UEAwwUb3FzdGVzdCBDQSBlY2RzYXAyNTYwWTAT
5+
BgcqhkjOPQIBBggqhkjOPQMBBwNCAASszcOdq5lgz1JdaYnCcdRVS0ELj2+37PpS
6+
ypNGmxuF5WDjMj/519xylbhOWQkx4/T3ZTNEDoVda5YFsJ4AJx/xMAoGCCqGSM49
7+
BAMCA0gAMEUCICMcvqeV5ygmyIv7Zbaq+kKPUE5cA48jlHNQwQTh17VxAiEA+pZ5
8+
FnKRW0xI90QHYL6Sy+B2gUpDA6bbRXs7EypeVA0=
9+
-----END CERTIFICATE-----

bench/certs/server.csr

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-----BEGIN CERTIFICATE REQUEST-----
2+
MIHZMIGBAgEAMB8xHTAbBgNVBAMMFG9xc3Rlc3QgQ0EgZWNkc2FwMjU2MFkwEwYH
3+
KoZIzj0CAQYIKoZIzj0DAQcDQgAErM3DnauZYM9SXWmJwnHUVUtBC49vt+z6UsqT
4+
RpsbheVg4zI/+dfccpW4TlkJMeP092UzRA6FXWuWBbCeACcf8aAAMAoGCCqGSM49
5+
BAMCA0cAMEQCICCwHKUCevppkp8mIJ/i0+H1zR2SmmL6XDJ2DuDpIG6ZAiA3ihSC
6+
bTIXJe527hRxruB5937YwYlq1SVDqSjOyDh3Zw==
7+
-----END CERTIFICATE REQUEST-----

bench/certs/server.key

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-----BEGIN PRIVATE KEY-----
2+
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgRqyThi/VpmwRI5Wm
3+
845G8JK2hQo5aR+F323suEOIkhKhRANCAASszcOdq5lgz1JdaYnCcdRVS0ELj2+3
4+
7PpSypNGmxuF5WDjMj/519xylbhOWQkx4/T3ZTNEDoVda5YFsJ4AJx/x
5+
-----END PRIVATE KEY-----

bench/run_benchmarks.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env bash
2+
3+
BUILD_DIR='build/bench/'
4+
5+
./python bench/benchmarks.py

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy