add timed decorator for perf tracking

This commit is contained in:
Pierre-antoine Comby 2020-05-25 01:12:31 +02:00
parent 996ac3c337
commit f367b0ab2a
3 changed files with 22 additions and 2 deletions

View File

@ -1,6 +1,8 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import json import json
import time
from collections import defaultdict
from django.core.management.base import BaseCommand from django.core.management.base import BaseCommand
from collections import defaultdict from collections import defaultdict
@ -10,6 +12,20 @@ from django.db import transaction
from polymorphic.models import PolymorphicModel from polymorphic.models import PolymorphicModel
def timed(method):
""""
A simple decorator to measure time elapsed in class function (hence the args[0])
"""
def _timed(*args, **kw):
ts = time.time()
result = method(*args, **kw)
te = time.time()
args[0].print_success(f"{method.__name__} executed ({te-ts:.2f}s)")
return result
return _timed
class ImportCommand(BaseCommand): class ImportCommand(BaseCommand):
""" """
Generic command for import of NK15 database Generic command for import of NK15 database

View File

@ -15,7 +15,7 @@ from note.models import Note, NoteUser, NoteClub
from note.models import Alias from note.models import Alias
from member.models import Club, Profile from member.models import Club, Profile
from ._import_utils import ImportCommand, BulkCreateManager from ._import_utils import ImportCommand, BulkCreateManager, timed
M_DURATION = 396 M_DURATION = 396
M_START = datetime.date(2019, 8, 31) M_START = datetime.date(2019, 8, 31)
@ -44,6 +44,8 @@ class Command(ImportCommand):
def add_arguments(self, parser): def add_arguments(self, parser):
parser.add_argument('-a', '--alias', action='store_true', help="import alias") parser.add_argument('-a', '--alias', action='store_true', help="import alias")
@timed
@transaction.atomic @transaction.atomic
def import_account(self, cur, chunk_size): def import_account(self, cur, chunk_size):
""" """
@ -157,6 +159,7 @@ class Command(ImportCommand):
bulk_mgr.done() bulk_mgr.done()
self.print_success("comptes table imported") self.print_success("comptes table imported")
@timed
def import_alias(self, cur, chunk_size): def import_alias(self, cur, chunk_size):
""" """
Import Alias from nk15 Import Alias from nk15

View File

@ -22,7 +22,7 @@ class Command(ImportCommand):
def add_arguments(self, parser): def add_arguments(self, parser):
pass pass
@timed
@transaction.atomic @transaction.atomic
def import_activities(self, cur, chunk_size): def import_activities(self, cur, chunk_size):
cur.execute("SELECT * FROM activites ORDER by id") cur.execute("SELECT * FROM activites ORDER by id")
@ -58,6 +58,7 @@ class Command(ImportCommand):
bulk_mgr.done() bulk_mgr.done()
return MAP_IDACTIVITY, MAP_NAMEACTIVITY return MAP_IDACTIVITY, MAP_NAMEACTIVITY
@timed
@transaction.atomic @transaction.atomic
def import_activity_entries(cur): def import_activity_entries(cur):
bulk_mgr = BulkCreateManager() bulk_mgr = BulkCreateManager()