2 # -*- coding: iso-8859-1 -*-
7 from threading import Thread, RLock
9 from cStringIO import StringIO
11 from StringIO import StringIO
14 # We should ignore SIGPIPE when using pycurl.NOSIGNAL - see
15 # the libcurl tutorial for more info.
18 from signal import SIGPIPE, SIG_IGN
19 signal.signal(signal.SIGPIPE, signal.SIG_IGN)
23 # The conclusion is: the multi interface is fastest!
27 assert NUM_PAGES % NUM_THREADS == 0
29 ##URL = "http://pycurl.sourceforge.net/tests/testgetvars.php?%d"
30 URL = "http://pycurl.sourceforge.net/tests/teststaticpage.html?%d"
38 def __init__(self, url):
40 self.body = StringIO()
43 self._curl = pycurl.Curl()
44 self._curl.setopt(pycurl.URL, self.url)
45 self._curl.setopt(pycurl.WRITEFUNCTION, self.body.write)
46 self._curl.setopt(pycurl.NOSIGNAL, 1)
52 self.http_code = self._curl.getinfo(pycurl.HTTP_CODE)
56 def print_result(items):
60 data = c.body.getvalue()
62 print "**********", c.url, "**********"
65 print "%-60s %3d %6d" % (c.url, c.http_code, len(data))
77 m = pycurl.CurlMulti()
78 for i in range(NUM_PAGES):
85 # stir state machine into action
87 ret, num_handles = m.perform()
88 if ret != pycurl.E_CALL_MULTI_PERFORM:
95 ret, num_handles = m.perform()
96 if ret != pycurl.E_CALL_MULTI_PERFORM:
107 print "multi interface: %d pages: perform %5.2f secs, total %5.2f secs" % (NUM_PAGES, clock3 - clock2, clock4 - clock1)
110 print_result(handles)
119 def __init__(self, lock=None):
120 Thread.__init__(self)
132 def test_threads(lock=None):
135 # create and start threads, but block them
139 # init (FIXME - this is ugly)
143 for i in range(NUM_PAGES):
144 if i % (NUM_PAGES / NUM_THREADS) == 0:
152 assert len(handles) == NUM_PAGES
153 assert len(threads) == NUM_THREADS
159 # release lock to let the blocked threads run
165 # wait for threads to finish
177 print "thread interface [lock]: %d pages: perform %5.2f secs, total %5.2f secs" % (NUM_PAGES, clock3 - clock2, clock4 - clock1)
179 print "thread interface: %d pages: perform %5.2f secs, total %5.2f secs" % (NUM_PAGES, clock3 - clock2, clock4 - clock1)
182 print_result(handles)
187 ### 3) thread - threads grab curl objects on demand from a shared pool
190 class TestPool(Thread):
191 def __init__(self, lock, pool):
192 Thread.__init__(self)
208 def test_thread_pool(lock):
213 for i in range(NUM_PAGES):
217 # create and start threads, but block them
220 pool = handles[:] # shallow copy of the list, shared for pop()
221 for i in range(NUM_THREADS):
222 t = TestPool(lock, pool)
225 assert len(pool) == NUM_PAGES
226 assert len(threads) == NUM_THREADS
230 # release lock to let the blocked threads run
233 # wait for threads to finish
244 print "thread interface [pool]: %d pages: perform %5.2f secs, total %5.2f secs" % (NUM_PAGES, clock3 - clock2, clock4 - clock1)
247 print_result(handles)
256 test_thread_pool(lock)
258 test_thread_pool(lock)