Skip to content

Instantly share code, notes, and snippets.

@ClaireNeveu
Last active August 29, 2015 14:04
Show Gist options
  • Save ClaireNeveu/680537d7b05602a0539c to your computer and use it in GitHub Desktop.
Save ClaireNeveu/680537d7b05602a0539c to your computer and use it in GitHub Desktop.
Benchmark
from __future__ import absolute_import, division, print_function
import requests
import csv
import time
import argparse
## Installation Instructions
#1 Install Python (2 or 3)
#2 Install Pip
#3 Pip Install Requests
def main():
parser = argparse.ArgumentParser(
description="Repeatedly request a url, recording stats.")
parser.add_argument("url", help="Url to request.")
parser.add_argument("-n", "--number",
help="Number of times to request url.",
default=100,
type=int)
parser.add_argument("-o", "--output",
help="File to output to",
default=None)
parser.add_argument("-H", "--host",
help="Host header",
default=None)
args = parser.parse_args()
if not args.output:
print("Response time\tStatus\tTime")
for i in range(args.number):
try:
if args.host:
headers = {"Host": args.host}
else:
headers = {}
resp = requests.get(args.url, headers=headers)
if args.output:
with open(args.output, "a") as file:
writer = csv.writer(file, delimiter=",")
data = [[resp.elapsed,
resp.status_code,
int(time.time())]]
writer.writerows(data)
else:
print(resp.elapsed, "\t",
resp.status_code, "\t",
int(time.time()))
except Exception as e:
z = e
print(z)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment