Skip to content
Snippets Groups Projects
Commit 8cfa5c1e authored by Kenzo-Hugo Hillion's avatar Kenzo-Hugo Hillion :recycle:
Browse files

Add profiling tool for performance issues

parent d8c73868
No related branches found
No related tags found
2 merge requests!59Prod,!21Try to fix the issue for gene length graph
...@@ -58,6 +58,7 @@ packaging = "*" ...@@ -58,6 +58,7 @@ packaging = "*"
python-slugify = "*" python-slugify = "*"
master = {git = "https://github.com/khillion/bioapi.git"} master = {git = "https://github.com/khillion/bioapi.git"}
marshmallow = "*" marshmallow = "*"
django-pandas = "*"
[requires] [requires]
python_version = "3.7" python_version = "3.7"
{ {
"_meta": { "_meta": {
"hash": { "hash": {
"sha256": "5998b6b97448fd635cc1b05787de28ac5ac3344ca6b8055831fca13790fc3f33" "sha256": "4be3394e3c4abe5fc7b75328ef912eaba09e15365322b7493e256f2def2ff013"
}, },
"pipfile-spec": 6, "pipfile-spec": 6,
"requires": { "requires": {
...@@ -86,6 +86,14 @@ ...@@ -86,6 +86,14 @@
"index": "pypi", "index": "pypi",
"version": "==2.2.0" "version": "==2.2.0"
}, },
"django-pandas": {
"hashes": [
"sha256:738cc03ffb411eef3eb02334d1f5a5d40697099a92ac59eb39629c08a9c2d6fb",
"sha256:788f4652012a67d2c5849191b01af58255f7af815ab612bebca019854235a9bc"
],
"index": "pypi",
"version": "==0.6.1"
},
"djangorestframework": { "djangorestframework": {
"hashes": [ "hashes": [
"sha256:5488aed8f8df5ec1d70f04b2114abc52ae6729748a176c453313834a9ee179c8", "sha256:5488aed8f8df5ec1d70f04b2114abc52ae6729748a176c453313834a9ee179c8",
...@@ -812,11 +820,11 @@ ...@@ -812,11 +820,11 @@
}, },
"pluggy": { "pluggy": {
"hashes": [ "hashes": [
"sha256:0db4b7601aae1d35b4a033282da476845aa19185c1e6964b25cf324b5e4ec3e6", "sha256:15b2acde666561e1298d71b523007ed7364de07029219b604cf808bfa1c765b0",
"sha256:fa5fa1622fa6dd5c030e9cad086fa19ef6a0cf6d7a2d12318e10cb49d6d68f34" "sha256:966c145cd83c96502c3c3868f50408687b38434af77734af1e9ca461a4081d2d"
], ],
"index": "pypi", "index": "pypi",
"version": "==0.13.0" "version": "==0.13.1"
}, },
"prometheus-client": { "prometheus-client": {
"hashes": [ "hashes": [
...@@ -866,10 +874,10 @@ ...@@ -866,10 +874,10 @@
}, },
"pygments": { "pygments": {
"hashes": [ "hashes": [
"sha256:71e430bc85c88a430f000ac1d9b331d2407f681d6f6aec95e8bcfbc3df5b0127", "sha256:83ec6c6133ca6b529b7ff5aa826328fd14b5bb02a58c37f4f06384e96a0f94ab",
"sha256:881c4c157e45f30af185c1ffe8d549d48ac9127433f2c380c24b84572ad66297" "sha256:b7949de3d396836085fea596998b135a22610bbcc4f2abfe9e448e44cbc58388"
], ],
"version": "==2.4.2" "version": "==2.5.1"
}, },
"pylint": { "pylint": {
"hashes": [ "hashes": [
...@@ -888,17 +896,17 @@ ...@@ -888,17 +896,17 @@
}, },
"pyrsistent": { "pyrsistent": {
"hashes": [ "hashes": [
"sha256:eb6545dbeb1aa69ab1fb4809bfbf5a8705e44d92ef8fc7c2361682a47c46c778" "sha256:f3b280d030afb652f79d67c5586157c5c1355c9a58dfc7940566e28d28f3df1b"
], ],
"version": "==0.15.5" "version": "==0.15.6"
}, },
"pytest": { "pytest": {
"hashes": [ "hashes": [
"sha256:8e256fe71eb74e14a4d20a5987bb5e1488f0511ee800680aaedc62b9358714e8", "sha256:63344a2e3bce2e4d522fd62b4fdebb647c019f1f9e4ca075debbd13219db4418",
"sha256:ff0090819f669aaa0284d0f4aad1a6d9d67a6efdc6dd4eb4ac56b704f890a0d6" "sha256:f67403f33b2b1d25a6756184077394167fe5e2f9d8bdaab30707d19ccec35427"
], ],
"index": "pypi", "index": "pypi",
"version": "==5.2.4" "version": "==5.3.1"
}, },
"pytest-cov": { "pytest-cov": {
"hashes": [ "hashes": [
......
import pandas as pd import pandas as pd
from django_pandas.io import read_frame
from drf_yasg import openapi from drf_yasg import openapi
from drf_yasg.utils import swagger_auto_schema from drf_yasg.utils import swagger_auto_schema
from marshmallow.exceptions import ValidationError from marshmallow.exceptions import ValidationError
...@@ -7,10 +8,10 @@ from rest_framework.decorators import action ...@@ -7,10 +8,10 @@ from rest_framework.decorators import action
from rest_framework.response import Response from rest_framework.response import Response
from rest_framework.status import HTTP_204_NO_CONTENT, HTTP_422_UNPROCESSABLE_ENTITY from rest_framework.status import HTTP_204_NO_CONTENT, HTTP_422_UNPROCESSABLE_ENTITY
from metagenedb.common.utils.df_operations import get_mask
from metagenedb.apps.catalog.models import Gene from metagenedb.apps.catalog.models import Gene
from metagenedb.api.catalog.qparams_validators.gene import GeneLengthQueryParams from metagenedb.api.catalog.qparams_validators.gene import GeneLengthQueryParams
from metagenedb.apps.catalog.serializers import GeneSerializer from metagenedb.apps.catalog.serializers import GeneSerializer
from metagenedb.common.utils.df_operations import get_mask
from .bulk_viewset import BulkViewSet from .bulk_viewset import BulkViewSet
...@@ -52,11 +53,12 @@ class GeneViewSet(BulkViewSet): ...@@ -52,11 +53,12 @@ class GeneViewSet(BulkViewSet):
serializer_class = GeneSerializer serializer_class = GeneSerializer
lookup_field = 'gene_id' lookup_field = 'gene_id'
GENE_LENGTH_COL = 'length' GENE_LENGTH_COL = 'length'
DEFAULT_WINDOW_SIZE = 10000
def get_permissions(self): def get_permissions(self):
return super(self.__class__, self).get_permissions() return super(self.__class__, self).get_permissions()
def _count_windows(self, df, window_size=10000, window_col=GENE_LENGTH_COL): def _count_windows(self, df, window_size=DEFAULT_WINDOW_SIZE, window_col=GENE_LENGTH_COL):
""" """
Count how many line of the df belong to each windows defined by the window_size for the window_col Count how many line of the df belong to each windows defined by the window_size for the window_col
:param df: :param df:
...@@ -84,7 +86,7 @@ class GeneViewSet(BulkViewSet): ...@@ -84,7 +86,7 @@ class GeneViewSet(BulkViewSet):
operation_id='Gene length distribution', operation_id='Gene length distribution',
) )
@action(methods=['get'], detail=False) @action(methods=['get'], detail=False)
def gene_length(self, request, window_size=10000): def gene_length(self, request):
try: try:
query_params = GeneLengthQueryParams().load(request.query_params) query_params = GeneLengthQueryParams().load(request.query_params)
except ValidationError as validation_error: except ValidationError as validation_error:
...@@ -93,9 +95,9 @@ class GeneViewSet(BulkViewSet): ...@@ -93,9 +95,9 @@ class GeneViewSet(BulkViewSet):
'allowed_query_params': ', '.join(GeneLengthQueryParams().declared_fields.keys()) 'allowed_query_params': ', '.join(GeneLengthQueryParams().declared_fields.keys())
}) })
return Response(error_message, status=HTTP_422_UNPROCESSABLE_ENTITY) return Response(error_message, status=HTTP_422_UNPROCESSABLE_ENTITY)
if 'window_size' in query_params:
window_size = query_params.get('window_size') window_size = query_params.get('window_size', self.DEFAULT_WINDOW_SIZE)
df = pd.DataFrame(list(self.queryset.values(self.GENE_LENGTH_COL))) df = read_frame(self.queryset.values(self.GENE_LENGTH_COL))
if df.empty: if df.empty:
return Response( return Response(
{'results': {}}, {'results': {}},
......
import cProfile
import functools
def profile(file_path):
def decorator_profile(func):
@functools.wraps(func)
def wrapper_profile(*args, **kwargs):
cp = cProfile.Profile()
cp.enable()
value = func(*args, **kwargs)
cp.disable()
cp.dump_stats(file_path)
return value
return wrapper_profile
return decorator_profile
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment