Show migration elapsed time when verbosity>1

This facilitates performance debugging related to migrations.
This commit is contained in:
Claude Paroz 2014-11-05 11:01:48 +01:00
parent 057305e588
commit 285bd02c92
1 changed files with 12 additions and 4 deletions

View File

@ -4,6 +4,7 @@ from __future__ import unicode_literals
from collections import OrderedDict from collections import OrderedDict
from importlib import import_module from importlib import import_module
import itertools import itertools
import time
import traceback import traceback
import warnings import warnings
@ -216,22 +217,29 @@ class Command(BaseCommand):
def migration_progress_callback(self, action, migration, fake=False): def migration_progress_callback(self, action, migration, fake=False):
if self.verbosity >= 1: if self.verbosity >= 1:
compute_time = self.verbosity > 1
if action == "apply_start": if action == "apply_start":
if compute_time:
self.start = time.time()
self.stdout.write(" Applying %s..." % migration, ending="") self.stdout.write(" Applying %s..." % migration, ending="")
self.stdout.flush() self.stdout.flush()
elif action == "apply_success": elif action == "apply_success":
elapsed = " (%.3fs)" % (time.time() - self.start) if compute_time else ""
if fake: if fake:
self.stdout.write(self.style.MIGRATE_SUCCESS(" FAKED")) self.stdout.write(self.style.MIGRATE_SUCCESS(" FAKED" + elapsed))
else: else:
self.stdout.write(self.style.MIGRATE_SUCCESS(" OK")) self.stdout.write(self.style.MIGRATE_SUCCESS(" OK" + elapsed))
elif action == "unapply_start": elif action == "unapply_start":
if compute_time:
self.start = time.time()
self.stdout.write(" Unapplying %s..." % migration, ending="") self.stdout.write(" Unapplying %s..." % migration, ending="")
self.stdout.flush() self.stdout.flush()
elif action == "unapply_success": elif action == "unapply_success":
elapsed = " (%.3fs)" % (time.time() - self.start) if compute_time else ""
if fake: if fake:
self.stdout.write(self.style.MIGRATE_SUCCESS(" FAKED")) self.stdout.write(self.style.MIGRATE_SUCCESS(" FAKED" + elapsed))
else: else:
self.stdout.write(self.style.MIGRATE_SUCCESS(" OK")) self.stdout.write(self.style.MIGRATE_SUCCESS(" OK" + elapsed))
def sync_apps(self, connection, app_labels): def sync_apps(self, connection, app_labels):
"Runs the old syncdb-style operation on a list of app_labels." "Runs the old syncdb-style operation on a list of app_labels."