jeudi 9 août 2018

Django test taking 20min+

I have a mysterious test that's taking much longer to run than it should. I have tried to debug it for a while now but I've run out of creativity. The test does create a lot of database records, but still, it's only a few hundreds, I could understand it taking one minute, but definitely not 20. Here's the relevant code that chokes for some reason:

from rest_framework.test import APITestCase

class MyTest(APITestCase, ...):

    ...

    def setUp(self):
        self.setup_questions()

    def setup_questions(self):
        question_levels = [
                QuestionLevelFactory.create(title="Major", ...),
                QuestionLevelFactory.create(title="Minor", ...),
                QuestionLevelFactory.create(title="Recommended", ...)
            ]

            questionnaire1 = QuestionnaireFactory.create(...)
            questionnaire2 = QuestionnaireFactory.create(...)

            possible_answer_set = PossibleAnswerSetFactory.create(...)

            for question_level in question_levels:
                if question_level.title == "Recommended":
                    questionnaire = questionnaire2
                    counter = 25
                else:
                    questionnaire = questionnaire1
                    counter = 100
                for i in range(counter):
                    if i <= 97:
                        question = QuestionFactory.create(
                            level_object=question_level, possible_answer_set=possible_answer_set,
                            questionnaire=questionnaire
                        )
                    else:
                        question = QuestionFactory.create(
                            level_object=question_level, possible_answer_set=possible_answer_set,
                            questionnaire=questionnaire, code="{}-{}".format(question_level.title, i)
                        )

                    if i < 95:
                        AnswerFactory.create(
                            question=question, assessment=self.assessment, value="yes"
                        )
                    elif i <= 97:
                        AnswerFactory.create(
                            question=question, assessment=self.assessment, value="not_applicable"
                        )
                    else:
                        AnswerFactory.create(
                            question=question, assessment=self.assessment, value="no",
                            justification="{} {} justification".format(question_level.title, i)
                        )

Note that, when the question_levels are listed in this order, the test only starts chocking when processing the "Minor" level. The first one ("Major") one is processed much faster. When I switch Minor and Major, things are slow from the beginning.

This is a legacy project that I started working on rather recently, so I don't know exactly what could be happening behind the curtains, but honestly I can't imagine anything that would cause such a strange behavior.

I'm running my Django app in a Docker container and connection to a Postgres server in another. Using SQLite doesn't really help, still takes very long.

Relevant library versions:

Django==1.9.13
django-factory-boy==0.1.6
factory-boy==2.7.0
fake-factory==0.7.2

Any suggestions/ideas are highly appreciated.

Aucun commentaire:

Enregistrer un commentaire