[![PyPI latest](https://img.shields.io/pypi/v/drf-viewset-profiler.svg)](https://pypi.python.org/pypi/drf-viewset-profiler)
[![Build Status](https://travis-ci.org/fvlima/drf-viewset-profiler.svg?branch=master)](https://travis-ci.org/fvlima/drf-viewset-profiler)
# drf-viewset-profiler
Decorator to profile all methods from a viewset (and its serializer) line by line. For all methods that were called during a request, an output
will be generated showing the number of hits, time (in seconds), lines and the content of each line of a method that was executed
## Installation
`pip install drf-viewset-profiler`
## Usage
Decorate the viewset that will be profiled
```python
from drf_viewset_profiler import line_profiler_viewset
@line_profiler_viewset
class SomeViewSet(ViewSet):
queryset = Model.objects.all()
```
Set the middleware config in settings.py
```python
MIDDLEWARE = [
...
"drf_viewset_profiler.middleware.LineProfilerViewSetMiddleware"
]
```
Make requests in this viewset to profile and measure the time in seconds wasted
## Configuration
```python
DRF_VIEWSET_PROFILER = {
"DEFAULT_OUTPUT_GENERATION_TYPE": "drf_viewset_profiler.output.FileOutput",
"DEFAULT_OUTPUT_LOCATION": "",
"ENABLE_SERIALIZER_PROFILER": True
}
```
#### DEFAULT_OUTPUT_GENERATION_TYPE
- drf_viewset_profiler.output.FileOutput: generates the output in a txt file with the name of the profiled viewset
- drf_viewset_profiler.output.StdOutput: generates the output in the console (default)
It's possible to customize by extending the BaseOuput class
#### DEFAULT_OUTPUT_LOCATION
- the location to generate the output file with the name of the view that will profiled (default is empty)
#### ENABLE_SERIALIZER_PROFILER
- profile the methods from the serializer vinculated with the viewset (default is True)
#### Output example
```
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)
```
## Contribute
- Clone this repository
- Install poetry (pip install poetry)
- run `poetry install`
- run `pre-commit install`
- Create your branch, make changes and open the pull request
Raw data
{
"_id": null,
"home_page": "https://github.com/fvlima/drf-viewset-profiler",
"name": "drf-viewset-profiler",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.8,<4.0",
"maintainer_email": "",
"keywords": "django rest framework,drf,drf viewset,viewset line profiler,profiler",
"author": "Frederico V Lima",
"author_email": "frederico.vieira@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/ee/d0/9706be1c6009eb0e94e64462dd76bc8c3a486e8deb92dd107133e996d5e6/drf_viewset_profiler-0.4.0.tar.gz",
"platform": null,
"description": "[![PyPI latest](https://img.shields.io/pypi/v/drf-viewset-profiler.svg)](https://pypi.python.org/pypi/drf-viewset-profiler)\n[![Build Status](https://travis-ci.org/fvlima/drf-viewset-profiler.svg?branch=master)](https://travis-ci.org/fvlima/drf-viewset-profiler)\n\n# drf-viewset-profiler\n\nDecorator to profile all methods from a viewset (and its serializer) line by line. For all methods that were called during a request, an output\nwill be generated showing the number of hits, time (in seconds), lines and the content of each line of a method that was executed\n\n## Installation\n\n`pip install drf-viewset-profiler`\n\n## Usage\n\nDecorate the viewset that will be profiled\n\n```python\nfrom drf_viewset_profiler import line_profiler_viewset\n\n@line_profiler_viewset\nclass SomeViewSet(ViewSet):\n queryset = Model.objects.all()\n```\n\nSet the middleware config in settings.py\n\n```python\nMIDDLEWARE = [\n ...\n \"drf_viewset_profiler.middleware.LineProfilerViewSetMiddleware\"\n]\n```\n\nMake requests in this viewset to profile and measure the time in seconds wasted\n\n## Configuration\n\n```python\nDRF_VIEWSET_PROFILER = {\n \"DEFAULT_OUTPUT_GENERATION_TYPE\": \"drf_viewset_profiler.output.FileOutput\",\n \"DEFAULT_OUTPUT_LOCATION\": \"\",\n \"ENABLE_SERIALIZER_PROFILER\": True\n}\n```\n\n#### DEFAULT_OUTPUT_GENERATION_TYPE\n- drf_viewset_profiler.output.FileOutput: generates the output in a txt file with the name of the profiled viewset\n- drf_viewset_profiler.output.StdOutput: generates the output in the console (default)\n\nIt's possible to customize by extending the BaseOuput class\n\n#### DEFAULT_OUTPUT_LOCATION\n- the location to generate the output file with the name of the view that will profiled (default is empty)\n\n#### ENABLE_SERIALIZER_PROFILER\n- profile the methods from the serializer vinculated with the viewset (default is True)\n\n#### Output example\n\n```\nTotal time: 1.7e-05 s\nFile: /.pyenv/versions/3.7.4/envs/drf-viewset-profiler/lib/python3.7/site-packages/django/views/generic/base.py\nFunction: _allowed_methods at line 113\n\nLine # Hits Time Per Hit % Time Line Contents\n==============================================================\n113 def _allowed_methods(self):\n114 1 17.0 17.0 100.0 return [m.upper() for m in self.http_method_names if hasattr(self, m)]\n\nTotal time: 0.000158 s\nFile: /.pyenv/versions/3.7.4/envs/drf-viewset-profiler/lib/python3.7/site-packages/rest_framework/generics.py\nFunction: get_serializer at line 103\n\nLine # Hits Time Per Hit % Time Line Contents\n==============================================================\n103 def get_serializer(self, *args, **kwargs):\n104 \"\"\"\n105 Return the serializer instance that should be used for validating and\n106 deserializing input, and for serializing output.\n107 \"\"\"\n108 1 15.0 15.0 9.5 serializer_class = self.get_serializer_class()\n109 1 12.0 12.0 7.6 kwargs['context'] = self.get_serializer_context()\n110 1 131.0 131.0 82.9 return serializer_class(*args, **kwargs)\n\nTotal time: 4e-06 s\nFile: /.pyenv/versions/3.7.4/envs/drf-viewset-profiler/lib/python3.7/site-packages/rest_framework/generics.py\nFunction: get_serializer_class at line 112\n\nLine # Hits Time Per Hit % Time Line Contents\n==============================================================\n112 def get_serializer_class(self):\n113 \"\"\"\n114 Return the class to use for the serializer.\n115 Defaults to using `self.serializer_class`.\n116 \n117 You may want to override this if you need to provide different\n118 serializations depending on the incoming request.\n119 \n120 (Eg. admins get full serialization, others get basic serialization)\n121 \"\"\"\n122 1 3.0 3.0 75.0 assert self.serializer_class is not None, (\n123 \"'%s' should either include a `serializer_class` attribute, \"\n124 \"or override the `get_serializer_class()` method.\"\n125 % self.__class__.__name__\n126 )\n127 \n128 1 1.0 1.0 25.0 return self.serializer_class \n\n...\n\nTotal time: 1.5491 s\nFile: /drf-viewset-profiler/test-drf-project/testapp/views.py\nFunction: create at line 52\n\nLine # Hits Time Per Hit % Time Line Contents\n==============================================================\n 52 def create(self, request):\n 53 1 4.0 4.0 0.0 import time\n 54 1 1505235.0 1505235.0 97.2 time.sleep(1.5)\n 55 1 43866.0 43866.0 2.8 return super().create(request) \n``` \n\n## Contribute\n\n- Clone this repository\n- Install poetry (pip install poetry)\n- run `poetry install`\n- run `pre-commit install`\n- Create your branch, make changes and open the pull request\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Lib to profile all methods from a viewset line by line",
"version": "0.4.0",
"project_urls": {
"Homepage": "https://github.com/fvlima/drf-viewset-profiler",
"Repository": "https://github.com/fvlima/drf-viewset-profiler"
},
"split_keywords": [
"django rest framework",
"drf",
"drf viewset",
"viewset line profiler",
"profiler"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "098b24617ccb7ef256511826a283d87e21d207163e0a57e06600b4c41191ff7f",
"md5": "3110be866c9b0cc435482c1fbefff4fd",
"sha256": "656aba377cc7c18ce46935dd5ca3576f89ffc73a730983a0ecd679e034e23582"
},
"downloads": -1,
"filename": "drf_viewset_profiler-0.4.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "3110be866c9b0cc435482c1fbefff4fd",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8,<4.0",
"size": 9088,
"upload_time": "2024-01-21T16:37:21",
"upload_time_iso_8601": "2024-01-21T16:37:21.925235Z",
"url": "https://files.pythonhosted.org/packages/09/8b/24617ccb7ef256511826a283d87e21d207163e0a57e06600b4c41191ff7f/drf_viewset_profiler-0.4.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "eed09706be1c6009eb0e94e64462dd76bc8c3a486e8deb92dd107133e996d5e6",
"md5": "55b0e65b5caf7a98bd10cf7d2f50da2d",
"sha256": "6454e708305da7e2594e141851abdc795aff42a09ffe6d87b1a12115b85f18bc"
},
"downloads": -1,
"filename": "drf_viewset_profiler-0.4.0.tar.gz",
"has_sig": false,
"md5_digest": "55b0e65b5caf7a98bd10cf7d2f50da2d",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8,<4.0",
"size": 5490,
"upload_time": "2024-01-21T16:37:24",
"upload_time_iso_8601": "2024-01-21T16:37:24.263056Z",
"url": "https://files.pythonhosted.org/packages/ee/d0/9706be1c6009eb0e94e64462dd76bc8c3a486e8deb92dd107133e996d5e6/drf_viewset_profiler-0.4.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-01-21 16:37:24",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "fvlima",
"github_project": "drf-viewset-profiler",
"travis_ci": true,
"coveralls": false,
"github_actions": false,
"tox": true,
"lcname": "drf-viewset-profiler"
}