Lib从一个viewset逐行评测所有方法
drf-viewset-profiler的Python项目详细描述
drf视图集分析器
Decorator逐行分析视图集(及其序列化程序)中的所有方法。对于请求期间调用的所有方法,输出 将生成显示点击数、时间(以秒为单位)、行和已执行方法的每行内容
安装
pip install drf-viewset-profiler
使用
装饰要分析的视图集
fromdrf_viewset_profilerimportline_profiler_viewset@line_profiler_viewsetclassSomeViewSet(ViewSet):queryset=Model.objects.all()
在中设置中间件配置设置.py在
^{pr2}$在此视图集中发出请求以分析和测量浪费的时间(以秒为单位)
配置
DRF_VIEWSET_PROFILER={"DEFAULT_OUTPUT_GENERATION_TYPE":"drf_viewset_profiler.output.FileOutput","DEFAULT_OUTPUT_LOCATION":"","ENABLE_SERIALIZER_PROFILER":True}
默认输出生成类型
- drf视图集_profiler.output.FileOutput:在txt文件中生成带有已分析视图集名称的输出
- drf视图集_探查器.output.StdOutput:在控制台中生成输出(默认)
可以通过扩展baseoutput类进行自定义
默认输出位置
- 使用要分析的视图的名称生成输出文件的位置(默认值为空)
启用\u SERIALIZER_PROFILER
- 分析序列化程序中与视图集关联的方法(默认值为True)
输出示例
Total time: 1.7e-05 s
File: /.pyenv/versions/3.7.4/envs/drf-viewset-profiler/lib/python3.7/site-packages/django/views/generic/base.py
Function: _allowed_methods at line 113
Line # Hits Time Per Hit % Time Line Contents
==============================================================
113 def _allowed_methods(self):
114 1 17.0 17.0 100.0 return [m.upper() for m in self.http_method_names if hasattr(self, m)]
Total time: 0.000158 s
File: /.pyenv/versions/3.7.4/envs/drf-viewset-profiler/lib/python3.7/site-packages/rest_framework/generics.py
Function: get_serializer at line 103
Line # Hits Time Per Hit % Time Line Contents
==============================================================
103 def get_serializer(self, *args, **kwargs):
104 """
105 Return the serializer instance that should be used for validating and
106 deserializing input, and for serializing output.
107 """
108 1 15.0 15.0 9.5 serializer_class = self.get_serializer_class()
109 1 12.0 12.0 7.6 kwargs['context'] = self.get_serializer_context()
110 1 131.0 131.0 82.9 return serializer_class(*args, **kwargs)
Total time: 4e-06 s
File: /.pyenv/versions/3.7.4/envs/drf-viewset-profiler/lib/python3.7/site-packages/rest_framework/generics.py
Function: get_serializer_class at line 112
Line # Hits Time Per Hit % Time Line Contents
==============================================================
112 def get_serializer_class(self):
113 """
114 Return the class to use for the serializer.
115 Defaults to using `self.serializer_class`.
116
117 You may want to override this if you need to provide different
118 serializations depending on the incoming request.
119
120 (Eg. admins get full serialization, others get basic serialization)
121 """
122 1 3.0 3.0 75.0 assert self.serializer_class is not None, (
123 "'%s' should either include a `serializer_class` attribute, "
124 "or override the `get_serializer_class()` method."
125 % self.__class__.__name__
126 )
127
128 1 1.0 1.0 25.0 return self.serializer_class
...
Total time: 1.5491 s
File: /drf-viewset-profiler/test-drf-project/testapp/views.py
Function: create at line 52
Line # Hits Time Per Hit % Time Line Contents
==============================================================
52 def create(self, request):
53 1 4.0 4.0 0.0 import time
54 1 1505235.0 1505235.0 97.2 time.sleep(1.5)
55 1 43866.0 43866.0 2.8 return super().create(request)
贡献
- 克隆此存储库
- 安装诗歌(pip安装诗歌)
- 运行
poetry install
- 运行
pre-commit install
- 创建分支,进行更改并打开请求
- 项目
标签: