Commits

Erik van Zijst committed 966c1e2

Initial version.

Comments (0)

Files changed (1)

+#!/usr/bin/env python
+#import ssl
+#from ssl import socket
+
+from interruptingcow import timeout
+import getopt
+from requests import sessions
+import sys
+import time
+
+class TimeoutException(Exception):
+    pass
+
+options, remainder = getopt.getopt(sys.argv[1:], 't:', ['time='])
+duration = 1
+
+def die(message=None):
+    if message:
+        print >> sys.stderr, message
+    else:
+        print >> sys.stderr, """Request a URL.
+
+Usage: %s OPTIONS url
+
+OPTIONS
+    -t, --time      length of the test (seconds, float)
+""" % sys.argv[0]
+    exit(1)
+
+for opt, arg in options:
+    if opt in ('-t', '--time'):
+        try:
+            duration = float(arg)
+            if duration < 0.0:
+                raise ValueError
+        except ValueError:
+            die('Error: invalid duration: %s' % arg)
+    else:
+        die()
+if len(remainder) is not 1:
+    die()
+url = remainder[0]
+
+start = time.time()
+count = 0
+try:
+    with sessions.session() as conn:
+        with timeout(duration, TimeoutException):
+            while True:
+                result = conn.get(url, verify=False)
+                count += 1
+except TimeoutException:
+    end = time.time()
+    print 'Made %d requests in %.3f seconds.' % (count, end - start)
+    print 'Rate: %.3f requests/sec' % (count / (end - start))
+