This repository was archived by the owner on Dec 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathclient.py
More file actions
51 lines (40 loc) · 1.32 KB
/
Copy pathclient.py
File metadata and controls
51 lines (40 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import sys
import gevent
import time
from gevent import monkey
monkey.patch_all()
import urllib2
def fetch_url(url):
""" Fetch a URL and return the total amount of time required.
"""
t0 = time.time()
try:
resp = urllib2.urlopen(url)
resp_code = resp.code
except urllib2.HTTPError, e:
resp_code = e.code
t1 = time.time()
print("\t@ %5.2fs got response [%d]" % (t1 - t0, resp_code))
return t1 - t0
def time_fetch_urls(url, num_jobs):
""" Fetch a URL `num_jobs` times in parallel and return the
total amount of time required.
"""
print("Sending %d requests for %s..." % (num_jobs, url))
t0 = time.time()
jobs = [gevent.spawn(fetch_url, url) for i in range(num_jobs)]
gevent.joinall(jobs)
t1 = time.time()
print("\t= %5.2fs TOTAL" % (t1 - t0))
return t1 - t0
if __name__ == '__main__':
try:
num_requests = int(sys.argv[1])
except IndexError:
num_requests = 5
# Fetch the URL that blocks with a `time.sleep`
t0 = time_fetch_urls("http://localhost:8000/sleep/python/", num_requests)
# Fetch the URL that blocks with a `pg_sleep`
t1 = time_fetch_urls("http://localhost:8000/sleep/postgres/", num_requests)
print("------------------------------------------")
print("SUM TOTAL = %.2fs" % (t0 + t1))