root / snf-astakos-app / astakos / test / stress.py @ b095201e
History | View | Annotate | Download (6.7 kB)
1 |
#!/usr/bin/env python
|
---|---|
2 |
# -*- coding: utf-8 -*-
|
3 |
|
4 |
# Copyright 2013 GRNET S.A. All rights reserved.
|
5 |
#
|
6 |
# Redistribution and use in source and binary forms, with or
|
7 |
# without modification, are permitted provided that the following
|
8 |
# conditions are met:
|
9 |
#
|
10 |
# 1. Redistributions of source code must retain the above
|
11 |
# copyright notice, this list of conditions and the following
|
12 |
# disclaimer.
|
13 |
#
|
14 |
# 2. Redistributions in binary form must reproduce the above
|
15 |
# copyright notice, this list of conditions and the following
|
16 |
# disclaimer in the documentation and/or other materials
|
17 |
# provided with the distribution.
|
18 |
#
|
19 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
20 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
21 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
22 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
23 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
24 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
25 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
26 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
27 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
28 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
29 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
30 |
# POSSIBILITY OF SUCH DAMAGE.
|
31 |
#
|
32 |
# The views and conclusions contained in the software and
|
33 |
# documentation are those of the authors and should not be
|
34 |
# interpreted as representing official policies, either expressed
|
35 |
# or implied, of GRNET S.A.
|
36 |
|
37 |
import os |
38 |
import sys |
39 |
from optparse import OptionParser |
40 |
from time import sleep |
41 |
import threading |
42 |
import datetime |
43 |
from random import choice, randint |
44 |
import logging |
45 |
|
46 |
path = os.path.dirname(os.path.realpath(__file__)) |
47 |
os.environ['SYNNEFO_SETTINGS_DIR'] = path + '/settings' |
48 |
os.environ['DJANGO_SETTINGS_MODULE'] = 'synnefo.settings' |
49 |
|
50 |
from astakos.im.functions import get_chain_of_application_id |
51 |
from views import submit, approve, join, leave |
52 |
from snf_django.lib.db.transaction import commit_on_success_strict |
53 |
|
54 |
USERS = {} |
55 |
PROJECTS = {} |
56 |
|
57 |
logger = logging.getLogger(__name__) |
58 |
logger.setLevel(logging.INFO) |
59 |
|
60 |
|
61 |
def random_name(): |
62 |
alphabet = u'abcdef_123490αβγδεζ'
|
63 |
length = randint(1, 15) |
64 |
return ''.join(choice(alphabet) for _ in xrange(length)) |
65 |
|
66 |
|
67 |
def random_email(): |
68 |
alphabet = u'abcdef_123490'
|
69 |
length = randint(1, 10) |
70 |
first = ''.join(choice(alphabet) for _ in xrange(length)) |
71 |
|
72 |
alphabet = u'abcdef'
|
73 |
length = randint(2, 4) |
74 |
last = ''.join(choice(alphabet) for _ in xrange(length)) |
75 |
return first + '@' + last |
76 |
|
77 |
|
78 |
def new_user(): |
79 |
email = random_email() |
80 |
u = AstakosUser(email=random_email(), first_name=random_name(), |
81 |
last_name=random_name(), is_active=True)
|
82 |
u.save() |
83 |
return u.id, u.email
|
84 |
|
85 |
|
86 |
@commit_on_success_strict()
|
87 |
def new_users(count): |
88 |
for i in range(count): |
89 |
uid, email = new_user() |
90 |
USERS[uid] = email |
91 |
|
92 |
|
93 |
class SubmitApproveT(threading.Thread): |
94 |
def __init__(self, *args, **kwargs): |
95 |
self.repeat = kwargs.pop('repeat', 1) |
96 |
threading.Thread.__init__(self, *args, **kwargs)
|
97 |
|
98 |
def run(self): |
99 |
owner = choice(USERS.keys()) |
100 |
p_name = random_name() |
101 |
submit_and_approve(p_name, owner, None, self.repeat, |
102 |
prefix=self.name)
|
103 |
|
104 |
|
105 |
def submit_and_approve(name, user_id, prec, repeat, prefix=""): |
106 |
if prefix:
|
107 |
prefix += ' '
|
108 |
|
109 |
for i in range(repeat): |
110 |
try:
|
111 |
now = datetime.datetime.now() |
112 |
logger.info('%s%s: submitting with precursor %s'
|
113 |
% (prefix, now, prec)) |
114 |
app_id = submit(name, user_id, prec) |
115 |
prec = app_id |
116 |
except Exception as e: |
117 |
logger.exception(e) |
118 |
try:
|
119 |
now = datetime.datetime.now() |
120 |
pid = get_chain_of_application_id(app_id) |
121 |
logger.info('%s%s: approving application %s of project %s'
|
122 |
% (prefix, now, app_id, pid)) |
123 |
approve(app_id) |
124 |
PROJECTS[pid] = True
|
125 |
except Exception as e: |
126 |
logger.exception(e) |
127 |
|
128 |
|
129 |
class JoinLeaveT(threading.Thread): |
130 |
def __init__(self, *args, **kwargs): |
131 |
self.repeat = kwargs.pop('repeat', 1) |
132 |
threading.Thread.__init__(self, *args, **kwargs)
|
133 |
|
134 |
def run(self): |
135 |
owner = choice(USERS.keys()) |
136 |
while True: |
137 |
projects = PROJECTS.keys() |
138 |
if projects:
|
139 |
pid = choice(projects) |
140 |
break
|
141 |
sleep(0.1)
|
142 |
join_and_leave(pid, owner, self.repeat, prefix=self.name) |
143 |
|
144 |
|
145 |
def join_and_leave(proj_id, user_id, repeat, prefix=""): |
146 |
if prefix:
|
147 |
prefix += ' '
|
148 |
|
149 |
for i in range(repeat): |
150 |
try:
|
151 |
now = datetime.datetime.now() |
152 |
logger.info('%s%s: user %s joining project %s'
|
153 |
% (prefix, now, user_id, proj_id)) |
154 |
join(proj_id, user_id) |
155 |
except Exception as e: |
156 |
logger.exception(e) |
157 |
|
158 |
try:
|
159 |
now = datetime.datetime.now() |
160 |
logger.info('%s%s: user %s leaving project %s'
|
161 |
% (prefix, now, user_id, proj_id)) |
162 |
leave(proj_id, user_id) |
163 |
except Exception as e: |
164 |
logger.exception(e) |
165 |
|
166 |
|
167 |
def test(users, projects, memb, repeat): |
168 |
logging.basicConfig() |
169 |
|
170 |
new_users(users) |
171 |
|
172 |
for i in range(projects): |
173 |
SubmitApproveT(repeat=repeat).start() |
174 |
|
175 |
for i in range(memb): |
176 |
JoinLeaveT(repeat=repeat).start() |
177 |
|
178 |
for thread in threading.enumerate(): |
179 |
if thread is not threading.currentThread(): |
180 |
thread.join() |
181 |
|
182 |
|
183 |
def main(): |
184 |
parser = OptionParser() |
185 |
parser.add_option('--users',
|
186 |
dest='users',
|
187 |
default=2,
|
188 |
help="Number of users (default=2)")
|
189 |
parser.add_option('--projects',
|
190 |
dest='projects',
|
191 |
default=2,
|
192 |
help="Number of projects (default=2)")
|
193 |
parser.add_option('--memb',
|
194 |
dest='memb',
|
195 |
default=2,
|
196 |
help="Number of membership requests (default=2)")
|
197 |
parser.add_option('--repeat',
|
198 |
dest='repeat',
|
199 |
default=20,
|
200 |
help="Number of iterations (default=20)")
|
201 |
parser.add_option('-q', '--quiet', |
202 |
action='store_true',
|
203 |
dest='quiet',
|
204 |
default=False,
|
205 |
help="Print only errors")
|
206 |
|
207 |
(options, args) = parser.parse_args() |
208 |
|
209 |
if options.quiet:
|
210 |
logger.setLevel(logging.WARNING) |
211 |
|
212 |
users = int(options.users)
|
213 |
projects = int(options.projects)
|
214 |
memb = int(options.memb)
|
215 |
repeat = int(options.repeat)
|
216 |
test(users, projects, memb, repeat) |
217 |
|
218 |
|
219 |
if __name__ == "__main__": |
220 |
main() |