Klaus Demo bjoern / 1fe9b00
Whoops, I accidentally Alexander's tests Jonas Haag 9 years ago
2 changed file(s) with 98 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 """The sample slow + fast client.
1
2 The client can work fine if server is localhost. Otherwise time_request
3 client usually takes the same time as long_request.
4 """
5
6 import sys
7 import socket
8 import time
9 import threading
10
11 if len(sys.argv) > 2:
12 print "usage: %s [host[:port]]" % sys.argv[0]
13 sys.exit(1)
14
15 hostname = 'localhost'
16 port = 8080
17 if len(sys.argv) == 2:
18 if ':' in sys.argv[1]:
19 hostname, port = sys.argv[1].split(':')
20 port = int(port)
21 else:
22 hostname = sys.argv[1]
23 headers = '\r\nHost: %s\r\n\r\n' % hostname
24
25 def timed(func):
26 def wrapper(*args, **kwargs):
27 start = time.time()
28 try:
29 return func(*args, **kwargs)
30 finally:
31 print '%s took %.2f sec' % (repr(func), time.time() - start)
32 return wrapper
33
34 lock = threading.Lock()
35 @timed
36 def long_request():
37 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
38 s.connect((hostname, port))
39 s.send('GET /huge HTTP/1.0' + headers)
40 for i in range(1):
41 s.recv(80)
42 time.sleep(0.1)
43 lock.release()
44 print 'release'
45 for i in range(10):
46 s.recv(80)
47 time.sleep(0.1)
48 s.close()
49
50 @timed
51 def time_request():
52 lock.acquire()
53 print 'acquire'
54 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
55 s.connect((hostname, port))
56 s.send('GET /tuple HTTP/1.0' + headers)
57 s.recv(900)
58 s.close()
59
60 lock.acquire()
61 fl = threading.Thread(target=long_request)
62 tl = threading.Thread(target=time_request)
63 fl.start()
64 tl.start()
65 tl.join()
66 fl.join()
0 #!/usr/bin/env python
1 # -*- coding: utf-8 -*-
2
3 from time import time
4 import bjoern
5
6 def start():
7 def return_tuple(environ, start_response):
8 start_response('200 OK', [('Content-Type','text/plain')])
9 print 'tuple'
10 return ('Hello,', " it's me, ", 'Bob!')
11
12 def return_huge_answer(environ, start_response):
13 start_response('200 OK', [('Content-Type','text/plain')])
14 return ['x'*(1024*1024)]
15
16 def return_404(environ, start_response):
17 start_response('404 Not Found', (('Content-Type','text/plain'), ))
18 return "URL %s not found" % environ.get('PATH_INFO', 'UNKNOWN')
19
20 dispatch = {
21 '/tuple': return_tuple,
22 '/huge': return_huge_answer,
23 }
24
25 def choose(environ, start_response):
26 return dispatch.get(environ.get('PATH_INFO'), return_404)(environ, start_response)
27 bjoern.run(choose, '0.0.0.0', 8080)
28
29 if __name__=="__main__":
30 start()