Skip to content

Instantly share code, notes, and snippets.

@robertknight
Last active November 16, 2018 17:44
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 robertknight/4f2f0b51424ebd65171179c496656ca6 to your computer and use it in GitHub Desktop.
Save robertknight/4f2f0b51424ebd65171179c496656ca6 to your computer and use it in GitHub Desktop.
Rate limiting test script
#!/usr/bin/env python3
from binascii import hexlify
import time
from random import randint
import requests
regular_url = "http://localhost:5000/_status"
asset_url = "http://localhost:5000/assets/images/icons/logo.svg"
# Generate a random auth header so this run is not counted as the same user as
# any previous run.
auth_token = hexlify(bytearray([randint(0, 16) for _ in range(10)]))
auth_header = f"Bearer {auth_token}"
def send_request(url):
r = requests.get(url, headers={"Authorization": auth_header})
if r.status_code != 200:
print(f"Request {url} failed with status {r.status_code}")
return r
def send_request_burst(url, burst_count):
print(f"Issuing a burst of {burst_count} requests")
burst_start = time.time()
for _ in range(burst_count):
send_request(url)
burst_end = time.time()
print(f"Request burst took {burst_end - burst_start}s")
send_request_burst(asset_url, 100)
send_request(regular_url)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment