-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabfile.py
93 lines (73 loc) · 3.67 KB
/
fabfile.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# -*- encoding: utf-8 -*-
import os
import datetime
from fabric.api import run, put, sudo, local, cd, settings, get
# Sempre executa o fabric no diretório raiz do repositório
os.chdir(os.path.dirname(__file__))
REMOTE_ROOT = '/srv/load_testing'
CONFIG_DIR = REMOTE_ROOT + '/config'
NGINX_CONFIG = CONFIG_DIR + '/load_testing.vhost'
def benchmark(url, base_filename, requests=500, concurrency=1):
filename = '{0}-{1}'.format(datetime.datetime.now().strftime('%Y%m%d_%H-%M-%S'), base_filename)
remote_file_path = os.path.join('/tmp', filename)
local_file_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'data', filename)
with settings(warn_only=True):
run('ab -n {requests} -c {concurrency} -r {url} > {remote_file_path}'.format(**locals()))
get(remote_file_path, local_file_path)
run('rm {0}'.format(remote_file_path))
def benchmark_all():
base_url = 'http://192.168.132.97'
test_cases = [
('/static_html/index.html', 'static_html'),
('/wsgi/simple_wsgi.py', 'simple_wsgi'),
('/wsgi/disk_read/wsgi_read_from_disk.py', 'wsgi_read_from_disk'),
('/wsgi/db/connect/wsgi_connect_to_db.py', 'wsgi_connect_to_db'),
('/wsgi/db/fetch/wsgi_fetch_from_db.py', 'wsgi_fetch_from_db'),
('/wsgi/django_test/direct_response/', 'django_direct_response'),
('/wsgi/django_test/response_from_disk/', 'django_response_from_disk'),
('/wsgi/django_test/db/raw/connect/', 'django_db_raw_connect'),
('/wsgi/django_test/db/raw/fetch/', 'django_db_raw_fetch'),
('/wsgi/django_test/db/orm/fetch/', 'django_db_orm_fetch'),
('/wsgi/django_with_middlewares/direct_response/', 'django_middleware_direct_response'),
('/wsgi/django_with_middlewares/response_from_disk/', 'django_middleware_response_from_disk'),
('/wsgi/django_with_middlewares/db/raw/connect/', 'django_middleware_db_raw_connect'),
('/wsgi/django_with_middlewares/db/raw/fetch/', 'django_middleware_db_raw_fetch'),
('/wsgi/django_with_middlewares/db/orm/fetch/', 'django_middleware_db_orm_fetch'),
]
for path, name in test_cases:
url = '{0}{1}'.format(base_url, path)
filename = '{0}.txt'.format(name)
benchmark(url, filename)
def deploy(revision):
local_archive_path = remote_archive_path = _create_git_archive(revision)
put(local_archive_path, remote_archive_path)
run('tar jxf %s -C %s' % (remote_archive_path, REMOTE_ROOT))
run('rm -v %s' % remote_archive_path)
sudo('ln -f -s %s /etc/nginx/sites-enabled/001-load_testing' % NGINX_CONFIG)
create_upstart_job('simple_wsgi')
create_upstart_job('wsgi_read_from_disk')
create_upstart_job('wsgi_connect_to_db')
create_upstart_job('wsgi_fetch_from_db')
create_upstart_job('django')
create_upstart_job('django_with_middlewares')
sudo('/etc/init.d/nginx restart')
sudo("init Q")
run('/usr/bin/python %s/test_cases/wsgi/django_test/manage.py syncdb' % REMOTE_ROOT)
reload_server('simple_wsgi')
reload_server('wsgi_read_from_disk')
reload_server('wsgi_connect_to_db')
reload_server('wsgi_fetch_from_db')
reload_server('django')
reload_server('django_with_middlewares')
def _create_git_archive(revision):
rev = local('git rev-parse %s' % revision, capture=True)
archive_path = '/tmp/%s.tar.bz2' % rev
local('git archive --format=tar %s | bzip2 -c > %s' % (rev, archive_path))
return archive_path
def reload_server(name):
with settings(warn_only=True):
sudo('initctl stop %s' % name)
sudo('initctl start %s' % name)
def create_upstart_job(name):
with cd('/etc/init/'):
sudo('cp -f %s/%s.conf %s.conf' % (CONFIG_DIR, name, name))