2019-02-11 16:18:15 +00:00
|
|
|
import os
|
2020-11-02 12:05:07 +01:00
|
|
|
import random
|
2021-11-03 14:14:53 +00:00
|
|
|
import logging
|
2020-11-02 12:05:07 +01:00
|
|
|
|
2020-06-23 10:37:11 +01:00
|
|
|
from locust import HttpUser, task, between
|
2021-11-01 12:11:58 +00:00
|
|
|
from utilities.CSVWriter import CSVWriter
|
2018-02-13 12:35:08 +00:00
|
|
|
from random import choice
|
2018-08-21 14:47:19 +01:00
|
|
|
from random import randint
|
2021-11-01 12:11:58 +00:00
|
|
|
from sys import argv
|
|
|
|
from datetime import date
|
2018-02-06 15:51:43 -05:00
|
|
|
|
2020-06-23 10:37:11 +01:00
|
|
|
class UserBehavior(HttpUser):
|
|
|
|
wait_time = between(2, 10)
|
|
|
|
|
2020-11-02 13:27:29 +01:00
|
|
|
# source: https://tools.tracemyip.org/search--ip/list
|
2020-11-02 12:05:07 +01:00
|
|
|
fake_ip_addresses = [
|
|
|
|
# white house
|
|
|
|
"156.33.241.5",
|
|
|
|
# Hollywood
|
|
|
|
"34.196.93.245",
|
|
|
|
# Chicago
|
|
|
|
"98.142.103.241",
|
|
|
|
# Los Angeles
|
2020-11-02 13:27:29 +01:00
|
|
|
"192.241.230.151",
|
|
|
|
# Berlin
|
|
|
|
"46.114.35.116",
|
|
|
|
# Singapore
|
|
|
|
"52.77.99.130",
|
|
|
|
# Sydney
|
|
|
|
"60.242.161.215"
|
2020-11-02 12:05:07 +01:00
|
|
|
]
|
|
|
|
|
2021-11-03 15:31:24 +00:00
|
|
|
logger = None
|
|
|
|
rthandler = None
|
|
|
|
formatter = None
|
2021-11-01 12:11:58 +00:00
|
|
|
php_services_api_prefix = '/api/ratings/api'
|
|
|
|
php_service_rate = '/rate'
|
|
|
|
php_service_fetch = '/fetch'
|
2021-11-03 14:14:53 +00:00
|
|
|
php_fieldnames = ['REQTYPE', 'SERVICE', 'INPUT', 'HEADER', 'ERRFLAG']
|
2021-11-01 12:11:58 +00:00
|
|
|
my_csv_writer = None
|
|
|
|
|
2018-02-06 15:51:43 -05:00
|
|
|
def on_start(self):
|
|
|
|
""" on_start is called when a Locust start before any task is scheduled """
|
|
|
|
print('Starting')
|
2021-11-03 14:14:53 +00:00
|
|
|
print("ARGS ARE:\n\"")
|
|
|
|
print("\n".join(argv))
|
2021-11-03 15:31:24 +00:00
|
|
|
print('End of ARGS \n')
|
2021-11-01 12:11:58 +00:00
|
|
|
|
2021-11-03 14:14:53 +00:00
|
|
|
for handler in logging.root.handlers[:]:
|
|
|
|
logging.root.removeHandler(handler)
|
2021-11-03 15:31:24 +00:00
|
|
|
|
|
|
|
self.logger = logging.getLogger('simple_rotating_logger')
|
|
|
|
self.rthandler = logging.handlers.RotatingFileHandler(filename='logs/calls.log', mode='a', maxBytes=5242880, backupCount=20, encoding='utf-8')
|
|
|
|
self.formatter = logging.Formatter('%(asctime)s [%(levelname)s]:%(message)s')
|
|
|
|
self.rthandler.setFormatter(self.formatter)
|
|
|
|
self.logger.addHandler(self.rthandler)
|
|
|
|
|
2021-11-01 12:11:58 +00:00
|
|
|
if os.environ.get('LOAD_DEBUG') == '1':
|
2021-11-03 15:31:24 +00:00
|
|
|
self.logger.setLevel(logging.DEBUG)
|
2021-11-03 14:14:53 +00:00
|
|
|
else:
|
2021-11-03 15:31:24 +00:00
|
|
|
self.logger.setLevel(logging.WARNING)
|
2021-11-03 14:14:53 +00:00
|
|
|
|
2021-11-03 15:31:24 +00:00
|
|
|
self.logger.info('Starting')
|
|
|
|
self.logger.info('LOAD_DEBUG: %s', os.environ.get("LOAD_DEBUG"))
|
|
|
|
self.logger.info('on start. php_fieldnames: %s', format(self.php_fieldnames))
|
2021-11-01 12:11:58 +00:00
|
|
|
|
|
|
|
self.my_csv_writer = CSVWriter("logs/php_services_calls.csv", self.php_fieldnames)
|
2018-02-06 15:51:43 -05:00
|
|
|
|
|
|
|
@task
|
2018-02-13 12:35:08 +00:00
|
|
|
def login(self):
|
2020-11-02 12:05:07 +01:00
|
|
|
fake_ip = random.choice(self.fake_ip_addresses)
|
|
|
|
|
2018-02-13 12:35:08 +00:00
|
|
|
credentials = {
|
|
|
|
'name': 'user',
|
|
|
|
'password': 'password'
|
|
|
|
}
|
2020-11-02 12:05:07 +01:00
|
|
|
res = self.client.post('/api/user/login', json=credentials, headers={'x-forwarded-for': fake_ip})
|
2018-02-13 12:35:08 +00:00
|
|
|
print('login {}'.format(res.status_code))
|
|
|
|
|
2018-02-06 15:51:43 -05:00
|
|
|
|
|
|
|
@task
|
2018-02-13 12:35:08 +00:00
|
|
|
def load(self):
|
2021-11-03 15:31:24 +00:00
|
|
|
self.logger.info('new user, new load task\n')
|
2020-11-02 12:05:07 +01:00
|
|
|
fake_ip = random.choice(self.fake_ip_addresses)
|
|
|
|
|
|
|
|
self.client.get('/', headers={'x-forwarded-for': fake_ip})
|
|
|
|
user = self.client.get('/api/user/uniqueid', headers={'x-forwarded-for': fake_ip}).json()
|
2018-02-13 12:35:08 +00:00
|
|
|
uniqueid = user['uuid']
|
|
|
|
print('User {}'.format(uniqueid))
|
|
|
|
|
2020-11-02 13:27:45 +01:00
|
|
|
self.client.get('/api/catalogue/categories', headers={'x-forwarded-for': fake_ip})
|
2018-02-13 12:35:08 +00:00
|
|
|
# all products in catalogue
|
2020-11-02 13:27:45 +01:00
|
|
|
products = self.client.get('/api/catalogue/products', headers={'x-forwarded-for': fake_ip}).json()
|
2018-02-13 12:35:08 +00:00
|
|
|
for i in range(2):
|
|
|
|
item = None
|
|
|
|
while True:
|
|
|
|
item = choice(products)
|
|
|
|
if item['instock'] != 0:
|
|
|
|
break
|
|
|
|
|
2021-11-01 12:11:58 +00:00
|
|
|
headers={'x-forwarded-for': fake_ip}
|
2018-08-21 14:47:19 +01:00
|
|
|
# vote for item
|
|
|
|
if randint(1, 10) <= 3:
|
2021-11-01 12:11:58 +00:00
|
|
|
ratevalue = randint(1, 5)
|
|
|
|
put_rate_api_str = '{}{}/{}/{}'.format(self.php_services_api_prefix, self.php_service_rate, item['sku'], ratevalue )
|
2021-11-03 15:31:24 +00:00
|
|
|
self.logger.info('item: {} ratevalue: {} put_rate_api_str: {} by: {}\n'.format(item['sku'], ratevalue, put_rate_api_str, fake_ip))
|
2021-11-03 14:14:53 +00:00
|
|
|
try:
|
|
|
|
self.client.put(put_rate_api_str, headers)
|
|
|
|
self.my_csv_writer.writerow({'REQTYPE': 'PUT', 'SERVICE': '{}'.format(self.php_service_rate), 'INPUT': '{}/{}'.format(item['sku'], ratevalue ), 'HEADER': '{}'.format(headers), 'ERRFLAG': '{}'.format("")})
|
|
|
|
except BaseException as err:
|
2021-11-03 15:31:24 +00:00
|
|
|
self.logger.warnign("Last call generated an error")
|
|
|
|
self.logger.exception()
|
2021-11-03 14:14:53 +00:00
|
|
|
self.my_csv_writer.writerow({'REQTYPE': 'PUT', 'SERVICE': '{}'.format(self.php_service_rate), 'INPUT': '{}/{}'.format(item['sku'], ratevalue ), 'HEADER': '{}'.format(headers), 'ERRFLAG': '{}'.format(err)})
|
|
|
|
pass
|
2018-08-21 14:47:19 +01:00
|
|
|
|
2020-11-02 12:05:07 +01:00
|
|
|
self.client.get('/api/catalogue/product/{}'.format(item['sku']), headers={'x-forwarded-for': fake_ip})
|
2021-11-01 12:11:58 +00:00
|
|
|
|
|
|
|
get_rate_api_str = '{}{}/{}'.format(self.php_services_api_prefix, self.php_service_fetch, item['sku'])
|
2021-11-03 15:31:24 +00:00
|
|
|
self.logger.info('item: {} get_rate_api_str: {} by: {}\n'.format(item['sku'], get_rate_api_str, fake_ip))
|
2021-11-03 14:14:53 +00:00
|
|
|
try:
|
|
|
|
self.client.get(get_rate_api_str, headers={'x-forwarded-for': fake_ip})
|
|
|
|
self.my_csv_writer.writerow({'REQTYPE': 'GET', 'SERVICE': '{}'.format(self.php_service_fetch), 'INPUT': '{}'.format(item['sku']), 'HEADER': '{}'.format(headers), 'ERRFLAG': '{}'.format("") })
|
|
|
|
except BaseException as err:
|
2021-11-03 15:31:24 +00:00
|
|
|
self.logger.warnign("Last call generated an error")
|
|
|
|
self.logger.exception()
|
2021-11-03 14:14:53 +00:00
|
|
|
self.my_csv_writer.writerow({'REQTYPE': 'GET', 'SERVICE': '{}'.format(self.php_service_fetch), 'INPUT': '{}'.format(item['sku']), 'HEADER': '{}'.format(headers), 'ERRFLAG': '{}'.format(err) })
|
|
|
|
pass
|
2021-11-01 12:11:58 +00:00
|
|
|
|
2020-11-02 12:05:07 +01:00
|
|
|
self.client.get('/api/cart/add/{}/{}/1'.format(uniqueid, item['sku']), headers={'x-forwarded-for': fake_ip})
|
2018-02-13 12:35:08 +00:00
|
|
|
|
2020-11-02 12:05:07 +01:00
|
|
|
cart = self.client.get('/api/cart/cart/{}'.format(uniqueid), headers={'x-forwarded-for': fake_ip}).json()
|
2018-02-13 12:35:08 +00:00
|
|
|
item = choice(cart['items'])
|
2020-11-02 12:05:07 +01:00
|
|
|
self.client.get('/api/cart/update/{}/{}/2'.format(uniqueid, item['sku']), headers={'x-forwarded-for': fake_ip})
|
2018-02-13 12:35:08 +00:00
|
|
|
|
|
|
|
# country codes
|
2020-11-02 12:05:07 +01:00
|
|
|
code = choice(self.client.get('/api/shipping/codes', headers={'x-forwarded-for': fake_ip}).json())
|
|
|
|
city = choice(self.client.get('/api/shipping/cities/{}'.format(code['code']), headers={'x-forwarded-for': fake_ip}).json())
|
2018-02-13 12:35:08 +00:00
|
|
|
print('code {} city {}'.format(code, city))
|
2020-11-02 12:05:07 +01:00
|
|
|
shipping = self.client.get('/api/shipping/calc/{}'.format(city['uuid']), headers={'x-forwarded-for': fake_ip}).json()
|
2018-02-13 12:35:08 +00:00
|
|
|
shipping['location'] = '{} {}'.format(code['name'], city['name'])
|
|
|
|
print('Shipping {}'.format(shipping))
|
|
|
|
# POST
|
2020-11-02 12:05:07 +01:00
|
|
|
cart = self.client.post('/api/shipping/confirm/{}'.format(uniqueid), json=shipping, headers={'x-forwarded-for': fake_ip}).json()
|
2018-02-13 12:35:08 +00:00
|
|
|
print('Final cart {}'.format(cart))
|
|
|
|
|
2020-11-02 12:05:07 +01:00
|
|
|
order = self.client.post('/api/payment/pay/{}'.format(uniqueid), json=cart, headers={'x-forwarded-for': fake_ip}).json()
|
2018-02-13 12:35:08 +00:00
|
|
|
print('Order {}'.format(order))
|
|
|
|
|
2019-02-11 16:18:15 +00:00
|
|
|
@task
|
|
|
|
def error(self):
|
2021-03-17 15:30:51 +00:00
|
|
|
fake_ip = random.choice(self.fake_ip_addresses)
|
2019-03-04 12:52:55 +00:00
|
|
|
if os.environ.get('ERROR') == '1':
|
2019-02-11 16:18:15 +00:00
|
|
|
print('Error request')
|
|
|
|
cart = {'total': 0, 'tax': 0}
|
2020-11-02 12:05:07 +01:00
|
|
|
self.client.post('/api/payment/pay/partner-57', json=cart, headers={'x-forwarded-for': fake_ip})
|
2019-02-11 16:18:15 +00:00
|
|
|
|
2018-02-06 15:51:43 -05:00
|
|
|
|