测开之路七十八:性能测试蓝图之视图层

时间:2021-12-28 09:05:25

测开之路七十八:性能测试蓝图之视图层

 

from flask import request
from flask import jsonify
from flask import Blueprint
from flask import render_template
from performance.logic import Logic

performance = Blueprint('performance', __name__,
static_folder='static',
template_folder='templates',
url_prefix='/performance')


@performance.route('/')
def index():
""" 首页 """
return render_template("performance.html")


@performance.route('/api/v1/start', methods=['POST'])
def api_v1_start():
""" 执行压测 """
data = request.get_json()

# 判断host必传
if 'host' not in data or not data['host']:
return jsonify({
'status': 400,
'message': "invalid paramenter ['host']",
'data': data
})

# 判断代码必传
if 'code' not in data or not data['code']:
return jsonify({
'status': 400,
'message': "invalid paramenter ['code']",
'data': data
})

# 执行运行逻辑
try:
case_id, _, _ = Logic().start(data)
return jsonify({
'status': 0,
'message': 'success',
'data': case_id
})
except Exception as error:
return jsonify({
'status': 500,
'message': str(error),
'data': data,
})