Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / test / stress.py @ 53f980c2

History | View | Annotate | Download (6.7 kB)

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