Skip to content

Instantly share code, notes, and snippets.

@minrk
Created March 11, 2021 09:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save minrk/45ce7c14f71227809b2178bf05fd1562 to your computer and use it in GitHub Desktop.
Save minrk/45ce7c14f71227809b2178bf05fd1562 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
"""Measure the cost of _preload_content=True across kubernetes versions
This has grown over time, and is *super* expensive with kubernetes 12
and lots of pods.
CSV output is produced on stdout, so call with e.g.
./measure-preload-content | tee measurements.csv
"""
import json
import os
import sys
import time
import timeit
from functools import lru_cache, partial
from subprocess import run
from tempfile import TemporaryDirectory
kube_ctx = "prod"
kube_ns = "prod"
@lru_cache()
def kube_api():
"""Cached function to return a single kube API client"""
import kubernetes.client
import kubernetes.config
kubernetes.config.load_kube_config(context=kube_ctx)
return kubernetes.client.CoreV1Api()
def list_pods(_preload_content, label_selector):
"""List pods, return the number of pods returned"""
kube = kube_api()
r = kube.list_namespaced_pod(
kube_ns,
label_selector=label_selector,
_preload_content=_preload_content,
)
if _preload_content:
return len(r.items)
else:
return len(json.loads(r.read())["items"])
def list_user_pods(preload):
"""List user pods in a jupyterhub deployment (lots)"""
return list_pods(
_preload_content=preload,
label_selector="app=jupyterhub,component=singleuser-server",
)
def list_placeholder_pods(preload):
"""List placeholder pods in a jupyterhub deployment (several, fewer than users)"""
return list_pods(
_preload_content=preload,
label_selector="app=jupyterhub,component=user-placeholder",
)
def list_build_pods(preload):
"""List build pods in a binderhub deployment (a few)"""
return list_pods(
_preload_content=preload,
label_selector="component=binderhub-build",
)
def list_hub_pods(preload):
"""List hub pods in a jupyterhub deployment (always 1)"""
return list_pods(
_preload_content=preload,
label_selector="app=jupyterhub,component=hub",
)
def measure(f, repeat=7):
"""Measure how long it takes to call f()
- collects `repeat` data points
- For each data point, run f enough times
that each measurement takes at least 200ms,
but at least twice.
(lifted from IPython's %timeit)
Returns:
(number, times, counts):
number of calls per measurement
list of times (in seconds) per f() call): (measured time / number)
and number of pods returned for each call
"""
tic = time.perf_counter()
f()
toc = time.perf_counter()
# always run it at least 2 times
number = 2
t = toc - tic
# make sure we take at least 200ms
# lifted from IPython's %timeit
while t * number < 0.2:
number *= 10
times = []
counts = []
print(f"Measuring {f} with r={repeat},n={number}", file=sys.stderr)
for i in range(repeat):
tic = time.perf_counter()
count = 0
for j in range(number):
count = max(f(), count)
toc = time.perf_counter()
times.append((toc - tic) / number)
counts.append(count)
return number, times, counts
def run_one():
"""Run one collection of data
This function is called within the test environment
"""
import kubernetes
# ensure cached kube client is created
kube = kube_api()
for list_f in [list_user_pods, list_placeholder_pods, list_hub_pods]:
for preload in (True, False):
number, times, counts = measure(partial(list_f, preload))
for t, c in zip(times, counts):
print(
f"{list_f.__name__},{preload},{kubernetes.__version__},{number},{t},{c}"
)
def setup_env(kube_version, td):
"""Set up a virtual environment in which to run a test"""
env_dir = os.path.join(td, f"kube-{kube_version}")
run([sys.executable, "-m", "venv", env_dir], stdout=sys.stderr)
run(
[
os.path.join(env_dir, "bin", "python3"),
"-mpip",
"install",
f"kubernetes=={kube_version}.*",
],
stdout=sys.stderr,
)
return env_dir
def run_all():
"""Run all the data collection
- create a test environment for each major kubernetes version
- run `run_one()` in that env
"""
with TemporaryDirectory() as td:
print("fname,preload,kube_version,number,per_call,pods")
for kube_version in [7, 8, 9, 10, 11, 12]:
env = setup_env(kube_version, td)
run([os.path.join(env, "bin", "python3"), "-u", __file__, "one"])
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument(
"command",
help="command to run",
choices=["one", "all"],
default="all",
nargs="?",
)
opts = parser.parse_args()
if opts.command == "all":
run_all()
elif opts.command == "one":
run_one()
fname preload kube_version number per_call pods
list_user_pods True 7.0.1 2 2.9281939895 609
list_user_pods True 7.0.1 2 2.315616512000001 612
list_user_pods True 7.0.1 2 2.0714346074999987 613
list_user_pods True 7.0.1 2 2.0372694179999993 613
list_user_pods True 7.0.1 2 1.9461733540000008 603
list_user_pods True 7.0.1 2 2.356966524999999 602
list_user_pods True 7.0.1 2 2.255201481500002 596
list_user_pods False 7.0.1 2 0.5660382505000001 598
list_user_pods False 7.0.1 2 0.5181754585 599
list_user_pods False 7.0.1 2 0.5563345820000016 600
list_user_pods False 7.0.1 2 0.6250853525000011 600
list_user_pods False 7.0.1 2 0.5234939044999969 601
list_user_pods False 7.0.1 2 0.5567624169999981 604
list_user_pods False 7.0.1 2 0.5525619589999984 604
list_placeholder_pods True 7.0.1 2 0.30065865050000085 75
list_placeholder_pods True 7.0.1 2 0.27420454900000024 75
list_placeholder_pods True 7.0.1 2 0.44985104750000104 75
list_placeholder_pods True 7.0.1 2 0.3812616755000029 75
list_placeholder_pods True 7.0.1 2 0.3096765055000006 75
list_placeholder_pods True 7.0.1 2 0.6677113319999997 75
list_placeholder_pods True 7.0.1 2 0.2857590839999986 75
list_placeholder_pods False 7.0.1 2 0.21407958749999878 75
list_placeholder_pods False 7.0.1 2 0.29173731950000104 75
list_placeholder_pods False 7.0.1 2 0.21913255999999848 75
list_placeholder_pods False 7.0.1 2 0.19270956949999984 75
list_placeholder_pods False 7.0.1 2 0.18226485649999802 75
list_placeholder_pods False 7.0.1 2 0.21358396900000187 75
list_placeholder_pods False 7.0.1 2 0.2170765760000002 75
list_hub_pods True 7.0.1 2 0.20381828449999873 1
list_hub_pods True 7.0.1 2 0.2025866150000013 1
list_hub_pods True 7.0.1 2 0.2052724519999991 1
list_hub_pods True 7.0.1 2 0.3078311454999998 1
list_hub_pods True 7.0.1 2 0.2040010979999991 1
list_hub_pods True 7.0.1 2 0.20420077749999876 1
list_hub_pods True 7.0.1 2 0.20473953099999775 1
list_hub_pods False 7.0.1 2 0.20554468999999997 1
list_hub_pods False 7.0.1 2 0.20585724800000094 1
list_hub_pods False 7.0.1 2 0.20036112100000025 1
list_hub_pods False 7.0.1 2 0.22113530100000034 1
list_hub_pods False 7.0.1 2 0.1955112469999989 1
list_hub_pods False 7.0.1 2 0.20018960899999882 1
list_hub_pods False 7.0.1 2 0.20645871900000046 1
list_user_pods True 8.0.2 2 1.7679836664999997 617
list_user_pods True 8.0.2 2 1.8249567505000002 616
list_user_pods True 8.0.2 2 1.9689307805 615
list_user_pods True 8.0.2 2 1.6847051664999997 616
list_user_pods True 8.0.2 2 1.7637256700000012 617
list_user_pods True 8.0.2 2 1.8290379569999988 619
list_user_pods True 8.0.2 2 1.8103391369999997 622
list_user_pods False 8.0.2 2 0.583657263000001 621
list_user_pods False 8.0.2 2 0.5963919305000012 624
list_user_pods False 8.0.2 2 0.7379159180000023 624
list_user_pods False 8.0.2 2 0.7517552555000009 624
list_user_pods False 8.0.2 2 0.7281056554999985 625
list_user_pods False 8.0.2 2 0.563855221499999 626
list_user_pods False 8.0.2 2 0.6548937529999996 627
list_placeholder_pods True 8.0.2 2 0.3741333789999999 75
list_placeholder_pods True 8.0.2 2 0.3144640315000018 75
list_placeholder_pods True 8.0.2 2 0.3090009430000009 75
list_placeholder_pods True 8.0.2 2 0.3634348514999992 75
list_placeholder_pods True 8.0.2 2 0.3091736119999986 75
list_placeholder_pods True 8.0.2 2 0.2950449070000012 75
list_placeholder_pods True 8.0.2 2 0.28874817250000007 75
list_placeholder_pods False 8.0.2 2 0.20377486599999983 75
list_placeholder_pods False 8.0.2 2 0.21071785000000176 75
list_placeholder_pods False 8.0.2 2 0.25311327549999874 75
list_placeholder_pods False 8.0.2 2 0.22060176549999966 75
list_placeholder_pods False 8.0.2 2 0.21102411650000263 75
list_placeholder_pods False 8.0.2 2 0.19066929950000144 75
list_placeholder_pods False 8.0.2 2 0.18965004000000008 75
list_hub_pods True 8.0.2 2 0.19667674949999991 1
list_hub_pods True 8.0.2 2 0.20997296899999895 1
list_hub_pods True 8.0.2 2 0.20400278749999856 1
list_hub_pods True 8.0.2 2 0.19463998499999846 1
list_hub_pods True 8.0.2 2 0.1832771039999983 1
list_hub_pods True 8.0.2 2 0.1898032210000018 1
list_hub_pods True 8.0.2 2 0.20568523450000242 1
list_hub_pods False 8.0.2 2 0.2670698499999986 1
list_hub_pods False 8.0.2 2 0.17201485600000055 1
list_hub_pods False 8.0.2 2 0.19448062800000088 1
list_hub_pods False 8.0.2 2 0.17949278100000043 1
list_hub_pods False 8.0.2 2 0.20636777100000003 1
list_hub_pods False 8.0.2 2 0.23886712299999857 1
list_hub_pods False 8.0.2 2 0.25515286650000135 1
list_user_pods True 9.0.1 2 1.9886384034999995 629
list_user_pods True 9.0.1 2 2.0164976599999997 627
list_user_pods True 9.0.1 2 2.4013781729999994 629
list_user_pods True 9.0.1 2 2.523709780999999 629
list_user_pods True 9.0.1 2 2.422242928500001 629
list_user_pods True 9.0.1 2 2.1655502534999993 629
list_user_pods True 9.0.1 2 2.190657473 629
list_user_pods False 9.0.1 2 0.5614010354999976 627
list_user_pods False 9.0.1 2 0.668990249500002 627
list_user_pods False 9.0.1 2 0.5359714790000005 626
list_user_pods False 9.0.1 2 0.541440893499999 626
list_user_pods False 9.0.1 2 0.717704239499998 626
list_user_pods False 9.0.1 2 0.5676269330000032 628
list_user_pods False 9.0.1 2 0.5867882794999986 628
list_placeholder_pods True 9.0.1 2 0.40393615500000024 75
list_placeholder_pods True 9.0.1 2 0.3018978109999999 75
list_placeholder_pods True 9.0.1 2 0.3174388195000013 75
list_placeholder_pods True 9.0.1 2 0.31976264750000283 75
list_placeholder_pods True 9.0.1 2 0.3554860249999976 75
list_placeholder_pods True 9.0.1 2 0.28607634249999947 75
list_placeholder_pods True 9.0.1 2 0.3836781660000028 75
list_placeholder_pods False 9.0.1 2 0.33906556399999843 75
list_placeholder_pods False 9.0.1 2 0.2193349540000007 75
list_placeholder_pods False 9.0.1 2 0.20490837599999878 75
list_placeholder_pods False 9.0.1 2 0.2203888169999999 75
list_placeholder_pods False 9.0.1 2 0.1959388544999996 75
list_placeholder_pods False 9.0.1 2 0.19704970700000146 75
list_placeholder_pods False 9.0.1 2 0.20734941299999932 75
list_hub_pods True 9.0.1 2 0.26947708949999694 1
list_hub_pods True 9.0.1 2 0.19553568850000147 1
list_hub_pods True 9.0.1 2 0.19495532150000017 1
list_hub_pods True 9.0.1 2 0.20155420500000076 1
list_hub_pods True 9.0.1 2 0.21514406350000215 1
list_hub_pods True 9.0.1 2 0.23095290850000083 1
list_hub_pods True 9.0.1 2 0.2262131200000006 1
list_hub_pods False 9.0.1 2 0.22663710449999996 1
list_hub_pods False 9.0.1 2 0.18644341049999724 1
list_hub_pods False 9.0.1 2 0.20147711950000158 1
list_hub_pods False 9.0.1 2 0.19551678799999905 1
list_hub_pods False 9.0.1 2 0.20254800349999869 1
list_hub_pods False 9.0.1 2 0.1988409295000011 1
list_hub_pods False 9.0.1 2 0.19769516699999912 1
list_user_pods True 10.1.0 2 4.138999248499999 629
list_user_pods True 10.1.0 2 2.3659873759999996 630
list_user_pods True 10.1.0 2 1.8992802824999995 631
list_user_pods True 10.1.0 2 2.5617476564999997 629
list_user_pods True 10.1.0 2 2.5007337300000003 626
list_user_pods True 10.1.0 2 1.795952830500001 626
list_user_pods True 10.1.0 2 2.168823310999997 623
list_user_pods False 10.1.0 2 0.5738492915000002 626
list_user_pods False 10.1.0 2 0.4988702775 626
list_user_pods False 10.1.0 2 0.5414176055000013 626
list_user_pods False 10.1.0 2 0.537905913000003 626
list_user_pods False 10.1.0 2 0.5788137410000012 626
list_user_pods False 10.1.0 2 0.6017006824999989 627
list_user_pods False 10.1.0 2 0.5290839354999974 625
list_placeholder_pods True 10.1.0 2 0.3148544649999998 75
list_placeholder_pods True 10.1.0 2 0.3354153879999977 75
list_placeholder_pods True 10.1.0 2 0.27434660750000006 75
list_placeholder_pods True 10.1.0 2 0.2888217300000022 75
list_placeholder_pods True 10.1.0 2 0.37002422000000124 75
list_placeholder_pods True 10.1.0 2 0.36651240750000014 75
list_placeholder_pods True 10.1.0 2 0.3743973005000001 75
list_placeholder_pods False 10.1.0 2 0.20008069049999833 75
list_placeholder_pods False 10.1.0 2 0.20601001949999898 75
list_placeholder_pods False 10.1.0 2 0.2311735679999991 75
list_placeholder_pods False 10.1.0 2 0.2158789084999988 75
list_placeholder_pods False 10.1.0 2 0.28312686949999843 75
list_placeholder_pods False 10.1.0 2 0.19513664100000128 75
list_placeholder_pods False 10.1.0 2 0.2085949135000007 75
list_hub_pods True 10.1.0 2 0.1802443860000018 1
list_hub_pods True 10.1.0 2 0.20521912149999721 1
list_hub_pods True 10.1.0 2 0.21430423350000183 1
list_hub_pods True 10.1.0 2 0.2903187384999981 1
list_hub_pods True 10.1.0 2 0.18338546599999717 1
list_hub_pods True 10.1.0 2 0.1932628584999989 1
list_hub_pods True 10.1.0 2 0.20418062499999934 1
list_hub_pods False 10.1.0 2 0.21423108950000014 1
list_hub_pods False 10.1.0 2 0.21133510949999845 1
list_hub_pods False 10.1.0 2 0.23459994499999937 1
list_hub_pods False 10.1.0 2 0.18630023499999737 1
list_hub_pods False 10.1.0 2 0.2166232920000013 1
list_hub_pods False 10.1.0 2 0.18538184899999877 1
list_hub_pods False 10.1.0 2 0.1981034835000024 1
list_user_pods True 11.0.0 2 2.4072391185 623
list_user_pods True 11.0.0 2 2.6388823345 624
list_user_pods True 11.0.0 2 2.0410394215000007 622
list_user_pods True 11.0.0 2 1.7569386224999999 620
list_user_pods True 11.0.0 2 1.9425787440000004 620
list_user_pods True 11.0.0 2 2.4777308589999993 621
list_user_pods True 11.0.0 2 1.882728857 618
list_user_pods False 11.0.0 2 0.5066222350000018 616
list_user_pods False 11.0.0 2 0.5843361945000005 616
list_user_pods False 11.0.0 2 0.5914965589999994 616
list_user_pods False 11.0.0 2 0.6439111445000023 617
list_user_pods False 11.0.0 2 0.6576294300000001 619
list_user_pods False 11.0.0 2 0.5270436614999987 620
list_user_pods False 11.0.0 2 0.5026139709999988 620
list_placeholder_pods True 11.0.0 2 0.341356461500002 75
list_placeholder_pods True 11.0.0 2 0.3014716015000012 75
list_placeholder_pods True 11.0.0 2 0.40161969349999893 75
list_placeholder_pods True 11.0.0 2 0.4658679649999975 75
list_placeholder_pods True 11.0.0 2 0.43342966049999987 75
list_placeholder_pods True 11.0.0 2 0.4332465610000007 75
list_placeholder_pods True 11.0.0 2 0.34223859649999966 75
list_placeholder_pods False 11.0.0 2 0.21763544299999893 75
list_placeholder_pods False 11.0.0 2 0.20690713649999992 75
list_placeholder_pods False 11.0.0 2 0.3055963900000016 75
list_placeholder_pods False 11.0.0 2 0.20196344500000052 75
list_placeholder_pods False 11.0.0 2 0.21065338849999904 75
list_placeholder_pods False 11.0.0 2 0.21719450499999837 75
list_placeholder_pods False 11.0.0 2 0.2403865164999992 75
list_hub_pods True 11.0.0 2 0.20446753050000055 1
list_hub_pods True 11.0.0 2 0.24607453349999986 1
list_hub_pods True 11.0.0 2 0.2131592189999978 1
list_hub_pods True 11.0.0 2 0.18681870499999675 1
list_hub_pods True 11.0.0 2 0.19816803550000017 1
list_hub_pods True 11.0.0 2 0.19007636650000137 1
list_hub_pods True 11.0.0 2 0.1995402239999997 1
list_hub_pods False 11.0.0 2 0.2501185039999996 1
list_hub_pods False 11.0.0 2 0.19746809899999818 1
list_hub_pods False 11.0.0 2 0.19938087799999948 1
list_hub_pods False 11.0.0 2 0.18255185399999974 1
list_hub_pods False 11.0.0 2 0.19607260149999917 1
list_hub_pods False 11.0.0 2 0.1882215115000001 1
list_hub_pods False 11.0.0 2 0.2035696924999968 1
list_user_pods True 12.0.1 2 6.636533157000001 615
list_user_pods True 12.0.1 2 5.683028385500002 614
list_user_pods True 12.0.1 2 4.811378537500001 616
list_user_pods True 12.0.1 2 6.214444411500001 616
list_user_pods True 12.0.1 2 6.217237324000003 612
list_user_pods True 12.0.1 2 4.847866332499997 615
list_user_pods True 12.0.1 2 6.348836381000005 618
list_user_pods False 12.0.1 2 0.5706708439999986 619
list_user_pods False 12.0.1 2 0.5194660135000007 618
list_user_pods False 12.0.1 2 0.6051191725000038 618
list_user_pods False 12.0.1 2 0.589876236000002 618
list_user_pods False 12.0.1 2 0.6855215844999947 617
list_user_pods False 12.0.1 2 0.6092106615000006 617
list_user_pods False 12.0.1 2 0.5497697935000048 617
list_placeholder_pods True 12.0.1 2 0.49898719600000163 75
list_placeholder_pods True 12.0.1 2 0.4333112595000017 75
list_placeholder_pods True 12.0.1 2 0.4142677379999995 75
list_placeholder_pods True 12.0.1 2 0.4151980119999976 75
list_placeholder_pods True 12.0.1 2 0.4616726369999995 75
list_placeholder_pods True 12.0.1 2 0.3407637044999987 75
list_placeholder_pods True 12.0.1 2 0.3735264729999983 75
list_placeholder_pods False 12.0.1 2 0.2075982544999988 75
list_placeholder_pods False 12.0.1 2 0.2026971570000029 75
list_placeholder_pods False 12.0.1 2 0.2046601695000021 75
list_placeholder_pods False 12.0.1 2 0.3073322119999986 75
list_placeholder_pods False 12.0.1 2 0.1953501254999992 75
list_placeholder_pods False 12.0.1 2 0.19084775150000155 75
list_placeholder_pods False 12.0.1 2 0.22774040300000564 75
list_hub_pods True 12.0.1 2 0.19580701250000487 1
list_hub_pods True 12.0.1 2 0.19765379049999865 1
list_hub_pods True 12.0.1 2 0.20790823149999937 1
list_hub_pods True 12.0.1 2 0.2587011789999991 1
list_hub_pods True 12.0.1 2 0.2048131909999995 1
list_hub_pods True 12.0.1 2 0.19633855150000556 1
list_hub_pods True 12.0.1 2 0.26033642850000405 1
list_hub_pods False 12.0.1 2 0.2058970574999961 1
list_hub_pods False 12.0.1 2 0.2045729829999985 1
list_hub_pods False 12.0.1 2 0.2561360060000055 1
list_hub_pods False 12.0.1 2 0.2042208664999947 1
list_hub_pods False 12.0.1 2 0.1889860284999969 1
list_hub_pods False 12.0.1 2 0.1872986945000008 1
list_hub_pods False 12.0.1 2 0.18762709749999829 1
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment