From 93496bdd1dcaa23eb413139737d2e2dfdc348821 Mon Sep 17 00:00:00 2001 From: zhaoyafan Date: Tue, 21 Mar 2023 01:18:45 +0800 Subject: [PATCH] Commit. --- Data/accounts.csv | 1 + Library/LocustBasic.py | 25 ++++++++++ locustfile.py | 104 +++++++++++++++++++++++++++++++++++++++++ requirements.txt | Bin 0 -> 1116 bytes 4 files changed, 130 insertions(+) create mode 100644 Data/accounts.csv create mode 100644 Library/LocustBasic.py create mode 100644 locustfile.py create mode 100644 requirements.txt 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 0000000000000000000000000000000000000000..f834ce5dd52f79ec7eb0b05979f68a8543d63baf GIT binary patch literal 1116 zcmZ8g%TB^j6r8n*p8|pMP!=x0#D$58i3^veyb21Xlva_CS7*-YZ5z|vrnzU%%$!Gm ze`d&WK!F{$xZ~4D7d@T{u5pD0R*V;nt*dy%XvC*Wq~13bZe1h9(8tK$&apvEmJ_)O zbdXXn$DVi6yqJQN2bLjq&r)=O4|8=}#RgUEz+AD!#-nU`9`cM0anERKjuK3XOYmHK z2vy3V^F5F)ra#wGRXYD;9n1_RS5uU%Wn3a7d!+V?4%D&W zJ%aYrJLJ7f*-d@TO-DGf=(x)h@6VR+f6LgS(kSRIv#bPfR66rU^Hzp-+Eumb?oBx* zwRW}{b8fBFd?sR5|Al!ai+ZpBtz>^2?_tv~