Skip to content

Instantly share code, notes, and snippets.

@baverman
Last active October 12, 2017 12:12
Show Gist options
  • Save baverman/2273f6743dcb793d3bfd8ce1077c9c17 to your computer and use it in GitHub Desktop.
Save baverman/2273f6743dcb793d3bfd8ce1077c9c17 to your computer and use it in GitHub Desktop.
Shows histogram of TLS connect times
#!/usr/bin/env python
import time
import sys
import socket
import ssl
import math
def percentile(N, percent, key=lambda x:x):
if not N:
return None
k = (len(N)-1) * percent / 100.0
f = math.floor(k)
c = math.ceil(k)
if f == c:
return key(N[int(k)])
d0 = key(N[int(f)]) * (c-k)
d1 = key(N[int(c)]) * (k-f)
return d0+d1
def main(name):
host, sep, port = name.rpartition(':')
if not sep:
host, port = port, 443
port = int(port)
for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM):
af, socktype, proto, canonname, sa = res
if af == socket.AF_INET:
break
else:
assert False, 'Can\'t find IPv4 address'
data = []
for _ in range(100):
t = time.time()
sock = socket.socket(af, socktype, proto)
ssock = ssl.wrap_socket(sock, ssl_version=ssl.PROTOCOL_TLSv1_2)
ssock.connect(sa)
duration = time.time() - t
data.append(duration)
data.sort()
for p in (50, 75, 95, 99, 100):
print(f'{p}: {percentile(data, p)}')
if __name__ == '__main__':
main(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment