Skip to content

Commit

Permalink
add e2e performance test (#795)
Browse files Browse the repository at this point in the history
* add e2e performance test

* remove error import

* change request num

* change request num

* change by review

* fix review

* remove unsed value

* fix review
  • Loading branch information
noO0oOo0ob authored Oct 12, 2023
1 parent e4afff7 commit c10a638
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 1 deletion.
9 changes: 9 additions & 0 deletions e2e_tests/assets/checker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from lyrebird import event
import time

TITLE = 'test_encoder_decoder'
CHECKER_TIME = 0.5

@event('flow')
def test_checker(flow):
time.sleep(CHECKER_TIME)
10 changes: 9 additions & 1 deletion e2e_tests/assets/serve.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
from flask import Flask, request
import hashlib
import sys
import time

app = Flask(__name__)
CORE_TIME = 0.6


@app.route("/e2e_serve", methods=["POST"])
def e2e_test():

req_body = request.get_data()
if request.files and "file" in request.files:
req_body = request.files['file'].read()
req_body = request.files["file"].read()
return hashlib.md5(request.url.encode() + req_body).hexdigest()
return hashlib.md5(request.url.encode() + req_body).hexdigest()

Expand All @@ -20,6 +22,12 @@ def status():
return "OK"


@app.route("/long_time_service", methods=["GET"])
def long_time_service():
time.sleep(CORE_TIME)
return "OK"


if __name__ == "__main__":
port = 5000
if sys.argv:
Expand Down
2 changes: 2 additions & 0 deletions e2e_tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def _init_port(self):
self.port = 5000
self.api_status = f'http://127.0.0.1:{self.port}/status'
self.api_post = f'http://127.0.0.1:{self.port}/e2e_serve'
self.api_long_time_service = f'http://127.0.0.1:{self.port}/long_time_service'

def start(self):
self.mock_server_process = subprocess.Popen(
Expand Down Expand Up @@ -78,6 +79,7 @@ def _init_port(self):
self.api_status = f'http://127.0.0.1:{self.port}/api/status'
self.uri_mock = f'http://127.0.0.1:{self.port}/mock/'
self.uri_extra_mock = f'http://127.0.0.1:{self.extra_mock_port}/'
self.api_flows = f'http://127.0.0.1:{self.port}/api/flow'

def _find_free_port(self):
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
Expand Down
83 changes: 83 additions & 0 deletions e2e_tests/test_performance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import os
import requests
from concurrent.futures import ThreadPoolExecutor, as_completed
import time



current_path = os.path.abspath(os.path.dirname(__file__))
checker_path = f'{current_path}/assets/checker.py'
modifier_path = f'{current_path}/assets/flow_editor.py'
REQUEST_NUM = 60
AVU_DURATION = 2
ALL_DURATION = 15


def fetch_url(req_url):
response = requests.get(req_url)
return response.text


def send_request(url):
with ThreadPoolExecutor() as executor:
futures = [executor.submit(fetch_url, url) for i in range(REQUEST_NUM)]
for future in as_completed(futures):
try:
res = future.result()
except Exception as e:
print(f"Request Error:{e}")


def test_performance(lyrebird, mock_server):
start_time = time.time()
send_request(lyrebird.uri_mock + mock_server.api_long_time_service)
end_time = time.time()
duration = end_time - start_time
r = requests.get(url=lyrebird.api_flows).json()
sum_time = sum(flow['duration'] for flow in r)
assert len(r) == REQUEST_NUM
assert sum_time/len(r) < AVU_DURATION
assert duration < ALL_DURATION
lyrebird.stop()


def test_performance_with_checker(lyrebird_with_args, mock_server):
lyrebird_with_args.start(checker_path=[checker_path])
start_time = time.time()
send_request(lyrebird_with_args.uri_mock + mock_server.api_long_time_service)
end_time = time.time()
duration = end_time - start_time
r = requests.get(url=lyrebird_with_args.api_flows).json()
sum_time = sum(flow['duration'] for flow in r)
assert len(r) == REQUEST_NUM
assert sum_time/len(r) < AVU_DURATION
assert duration < ALL_DURATION
lyrebird_with_args.stop()


def test_performance_with_modifier(lyrebird_with_args, mock_server):
lyrebird_with_args.start(checker_path=[modifier_path])
start_time = time.time()
send_request(lyrebird_with_args.uri_mock + mock_server.api_long_time_service)
end_time = time.time()
duration = end_time - start_time
r = requests.get(url=lyrebird_with_args.api_flows).json()
sum_time = sum(flow['duration'] for flow in r)
assert len(r) == REQUEST_NUM
assert sum_time/len(r) < AVU_DURATION
assert duration < ALL_DURATION
lyrebird_with_args.stop()


def test_performance_with_checker_and_modifier(lyrebird_with_args, mock_server):
lyrebird_with_args.start(checker_path=[checker_path, modifier_path])
start_time = time.time()
send_request(lyrebird_with_args.uri_mock + mock_server.api_long_time_service)
end_time = time.time()
duration = end_time - start_time
r = requests.get(url=lyrebird_with_args.api_flows).json()
sum_time = sum(flow['duration'] for flow in r)
assert len(r) == REQUEST_NUM
assert sum_time/len(r) < AVU_DURATION
assert duration < ALL_DURATION
lyrebird_with_args.stop()

0 comments on commit c10a638

Please sign in to comment.