forked from cgbystrom/locust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.py
56 lines (42 loc) · 1.77 KB
/
example.py
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
52
53
54
55
import locust
import gevent
import random
from clients import HTTPClient
from gevent import wsgi
# Defines the behaviour of a locust (aka a website user :)
def website_user(name):
c = HTTPClient('http://localhost:8088')
for i in range(0, 10):
# Request the fast page, wait for the response
c.get('/fast', name='Fast page')
# Think for 5 seconds
gevent.sleep(5)
# Do a few more requests
c.get('/slow', name='Slow page')
c.get('/consistent', name='Consistent page')
# End of function, locust dies.
# Simple WSGI server simulating fast and slow responses.
def test_server(env, start_response):
if env['PATH_INFO'] == '/fast':
start_response('200 OK', [('Content-Type', 'text/html')])
gevent.sleep(random.choice([0.1, 0.2, 0.3]))
return ["This is a fast response"]
if env['PATH_INFO'] == '/slow':
start_response('200 OK', [('Content-Type', 'text/html')])
gevent.sleep(random.choice([0.5, 1, 1.5]))
return ["This is a slow response"]
if env['PATH_INFO'] == '/consistent':
start_response('200 OK', [('Content-Type', 'text/html')])
gevent.sleep(0.2)
return ["This is a consistent response"]
else:
start_response('404 Not Found', [('Content-Type', 'text/html')])
return ['Not Found']
# Start the test server that will be our website under test
wsgi.WSGIServer(('', 8088), test_server).start()
# Start a web server where you can control the swarming
# from a web-based UI (recommended).
# Open http://localhost:8089 to start the test.
locust.prepare_swarm_from_web(website_user, hatch_rate=5, max=20)
# Immediately start swarming and print statistics to stdout every 1s
#locust.swarm(website_user, hatch_rate=4, max=10)