commit 93496bdd1dcaa23eb413139737d2e2dfdc348821 Author: zhaoyafan Date: Tue Mar 21 01:18:45 2023 +0800 Commit. diff --git a/Data/accounts.csv b/Data/accounts.csv new file mode 100644 index 0000000..abce1f0 --- /dev/null +++ b/Data/accounts.csv @@ -0,0 +1 @@ +user,user diff --git a/Library/LocustBasic.py b/Library/LocustBasic.py new file mode 100644 index 0000000..1564c57 --- /dev/null +++ b/Library/LocustBasic.py @@ -0,0 +1,25 @@ +from locust import task, FastHttpUser, constant, between +from locust.main import main +import csv +import sys +import os + + +class CsvDataReader: + __point = 0 + + def __init__(self, filename: str, encoding=None): + filename = '%s%s%s/%s' % (os.path.dirname(os.path.dirname(__file__)), os.sep, 'Data', filename) + try: + open(filename).read(4096) + except UnicodeDecodeError: + encoding = 'utf-8' + self.csv_data = [row for row in csv.reader(open(filename, encoding=encoding))] + self.csv_data_length = len(self.csv_data) + if self.csv_data_length == 0: + raise Exception('No data in file %s.' % filename) + + def read(self): + data = self.csv_data[self.__point % self.csv_data_length] + self.__point += 1 + return data diff --git a/locustfile.py b/locustfile.py new file mode 100644 index 0000000..5e3a771 --- /dev/null +++ b/locustfile.py @@ -0,0 +1,104 @@ +from Library.LocustBasic import * + +Account = CsvDataReader('accounts.csv') + + +class Test(FastHttpUser): + token = '' + wait_time = between(0.5, 0.8) + + def on_start(self): + account = Account.read() + with self.client.request( + method='POST', + url='/api/user/login', + data={ + 'username': account[0], + 'password': account[1] + }, + catch_response=True + ) as response: + pass + if response.status_code == 200 and response.json()['code'] == 0: + self.token = response.json()['data']['token'] + else: + response.failure(Exception('Login failed.')) + self.stop() + + @task(30) + def api_task(self): + with self.client.request( + method='GET', + url='/api/task?page_size=20&page=1&id=&protocol=&name=&tag=&host_id=&status=', + headers={ + 'Auth-Token': self.token + }, + catch_response=True + ) as response: + if response.status_code == 200 and response.json()['code'] == 0: + pass + else: + response.failure(Exception('%s: %.64s' % (response.status_code, response.text))) + + @task(30) + def api_logs(self): + with self.client.request( + method='GET', + url='/api/task/log?page_size=20&page=1&task_id=&protocol=&status=', + headers={ + 'Auth-Token': self.token + }, + catch_response=True + ) as response: + if response.status_code == 200 and response.json()['code'] == 0: + pass + else: + response.failure(Exception('%s: %.64s' % (response.status_code, response.text))) + + @task(25) + def api_host(self): + with self.client.request( + method='GET', + url='/api/host?page_size=20&page=1&id=&name=&alias=', + headers={ + 'Auth-Token': self.token + }, + catch_response=True + ) as response: + if response.status_code == 200 and response.json()['code'] == 0: + pass + else: + response.failure(Exception('%s: %.64s' % (response.status_code, response.text))) + + @task(15) + def api_user(self): + with self.client.request( + method='GET', + url='/api/user', + headers={ + 'Auth-Token': self.token + }, + catch_response=True + ) as response: + if response.status_code == 200 and response.json()['code'] == 0: + pass + else: + response.failure(Exception('%s: %.64s' % (response.status_code, response.text))) + + +if __name__ == '__main__': + # Locust file, default self. + sys.argv.append('--locustfile=%s' % __file__) + # Host to load test. + sys.argv.append('--host=http://fanscloud.net:5920') + # Host to bind the web interface to. Defaults to '*' (all interfaces) + sys.argv.append('--web-host=127.0.0.1') + # Peak number of concurrent Locust users. + # Primarily used together with --headless or --autostart. + sys.argv.append('--users=250') + # Rate to spawn users at (users per second). + # Primarily used together with --headless or --autostart + sys.argv.append('--spawn-rate=10') + # Primarily used together with --headless or --autostart + sys.argv.append('--run-time=3m') + sys.exit(main()) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..f834ce5 Binary files /dev/null and b/requirements.txt differ