Skip to content

Commit

Permalink
feat: mock perf mode no log (#216)
Browse files Browse the repository at this point in the history
Co-authored-by: rikasai233 <rikasai233@gmail.com>
  • Loading branch information
lihuacai168 and trumpchifan committed Jun 12, 2024
1 parent 56e6570 commit 62733eb
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ EMAIL_HOST_PASSWORD=your_email_password # 发件人邮箱密码
# 国外机器可以打开
# DEBIAN_REPO=deb.debian.org # Debian 软件源地址
# PIP_INDEX_URL=https://pypi.org/simple # PyPI 软件源地址


# 模型是否为性能模式,默认是否,会输出日志,1表示是性能模式,不输出日志
IS_PERF=0
4 changes: 4 additions & 0 deletions FasterRunner/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,3 +376,7 @@

# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = "django.db.models.AutoField"


# mock配置, 如果是性能测试环境,就设置为1,会关闭写日志, db
IS_PERF = os.environ.get("IS_PERF", "0")
30 changes: 19 additions & 11 deletions mock/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from rest_framework.request import Request
from rest_framework.response import Response
from rest_framework.views import APIView
from django.conf import settings

from FasterRunner.customer_swagger import CustomSwaggerAutoSchema
from .models import MockAPI, MockAPILog, MockProject
Expand Down Expand Up @@ -45,6 +46,7 @@ class Meta:
model = MockAPI
fields = ["project__project_name", "api_name", "creator", "request_path"]


class MockAPIViewset(viewsets.ModelViewSet):
swagger_tag = '项目下的Mock API CRUD'
swagger_schema = CustomSwaggerAutoSchema
Expand Down Expand Up @@ -84,6 +86,7 @@ def update(self, request, *args, **kwargs):

return Response(serializer.data)


class RequestObject:
def __init__(self, request):
self.method: str = request.method
Expand Down Expand Up @@ -150,7 +153,8 @@ def load_and_execute(module_name, code, method_name, request) -> Response:

def process(path, project_id, request: Request):
try:
logger.info(f"request path: {request.get_full_path()}")
if settings.IS_PERF == '0':
logger.info(f"request path: {request.get_full_path()}")

request_obj: dict = {
"method": request.method.upper(),
Expand All @@ -160,20 +164,17 @@ def process(path, project_id, request: Request):
"headers": request.headers._store,
"query_params": request.query_params,
}
logger.info(f"request_obj: {json.dumps(request_obj, indent=4)}")
if settings.IS_PERF == '0':
logger.info(f"request_obj: {json.dumps(request_obj, indent=4)}")

mock_api = MockAPI.objects.get(
project__project_id=project_id,
request_path=path,
request_method=request.method.upper(),
is_active=True,
)
request_id: str = uuid.uuid4().hex
log_obj = MockAPILog.objects.create(
request_obj=request_obj,
request_id=request_id,
api_id=mock_api.api_id,
project_id=mock_api.project,
)

response = load_and_execute(
"mock_api_module", mock_api.response_text, "execute", request
)
Expand All @@ -183,9 +184,16 @@ def process(path, project_id, request: Request):
"body": response.data,
"headers": response.headers._store,
}
logger.info(f"response_obj: {json.dumps(response_obj, indent=4)}", exc_info=True)
log_obj.response_obj = response_obj
log_obj.save()
if settings.IS_PERF == '0':
logger.info(f"response_obj: {json.dumps(response_obj, indent=4)}", exc_info=True)
log_obj = MockAPILog.objects.create(
request_obj=request_obj,
request_id=request_id,
api_id=mock_api.api_id,
project_id=mock_api.project,
)
log_obj.response_obj = response_obj
log_obj.save()
if response is not None:
return response
return Response({"error": "Execution failure"})
Expand Down

0 comments on commit 62733eb

Please sign in to comment.