From f367b0ab2acd4a6b141c5eeaf59d3dcaff527aa0 Mon Sep 17 00:00:00 2001 From: Pierre-antoine Comby Date: Mon, 25 May 2020 01:12:31 +0200 Subject: [PATCH] add timed decorator for perf tracking --- management/commands/_import_utils.py | 16 ++++++++++++++++ management/commands/import_account.py | 5 ++++- management/commands/import_activities.py | 3 ++- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/management/commands/_import_utils.py b/management/commands/_import_utils.py index 731048c..eb9c8b5 100644 --- a/management/commands/_import_utils.py +++ b/management/commands/_import_utils.py @@ -1,6 +1,8 @@ #!/usr/bin/env python3 import json +import time +from collections import defaultdict from django.core.management.base import BaseCommand from collections import defaultdict @@ -10,6 +12,20 @@ from django.db import transaction 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): """ Generic command for import of NK15 database diff --git a/management/commands/import_account.py b/management/commands/import_account.py index c7db885..53c01f8 100644 --- a/management/commands/import_account.py +++ b/management/commands/import_account.py @@ -15,7 +15,7 @@ from note.models import Note, NoteUser, NoteClub from note.models import Alias from member.models import Club, Profile -from ._import_utils import ImportCommand, BulkCreateManager +from ._import_utils import ImportCommand, BulkCreateManager, timed M_DURATION = 396 M_START = datetime.date(2019, 8, 31) @@ -44,6 +44,8 @@ class Command(ImportCommand): def add_arguments(self, parser): parser.add_argument('-a', '--alias', action='store_true', help="import alias") + + @timed @transaction.atomic def import_account(self, cur, chunk_size): """ @@ -157,6 +159,7 @@ class Command(ImportCommand): bulk_mgr.done() self.print_success("comptes table imported") + @timed def import_alias(self, cur, chunk_size): """ Import Alias from nk15 diff --git a/management/commands/import_activities.py b/management/commands/import_activities.py index 04f5268..74fdb94 100644 --- a/management/commands/import_activities.py +++ b/management/commands/import_activities.py @@ -22,7 +22,7 @@ class Command(ImportCommand): def add_arguments(self, parser): pass - + @timed @transaction.atomic def import_activities(self, cur, chunk_size): cur.execute("SELECT * FROM activites ORDER by id") @@ -58,6 +58,7 @@ class Command(ImportCommand): bulk_mgr.done() return MAP_IDACTIVITY, MAP_NAMEACTIVITY + @timed @transaction.atomic def import_activity_entries(cur): bulk_mgr = BulkCreateManager()