PHPUnit and Symfony - CPU Being Chewed at 100% For Simple Test Case

278 views Asked by At

I just joined a project, and I am attempting to get the PHPUnit tests in order. I think there might be something wrong, but I am not sure, so I am posting this question.

The following test is taking minutes to fail, and from what I have been reading, tests should be executed in 10 seconds or less. Also, It is chewing 100% CPU. What the test is testing, in production, executes in milliseconds, so I don't understand why it would take minutes... Side note, it is taking minutes just on the first line of the test case, I put some echo's in there, and it doesn't even get to the second line.

Here is the test case

public function it_should_get_the_weight_logs()
{
    $this->assertEmpty($this->tracker->getWeightLog());
    $this->assertEquals(0, $this->tracker->getWeightLog(0, null, '', '', '', true));

    $exercise = new ExerciseTracking;
    $exercise->setDate(new \DateTime);
    $exercise->setMeasure('oz');

    $this->entityManager->persist($exercise);
    $this->entityManager->flush();

    $this->assertEmpty($this->tracker->getWeightLog());
    $this->assertEquals(0, $this->tracker->getWeightLog(0, null, '', '', '', true));

    $exercise->setMeasure('kg');
    $this->entityManager->flush();

    $this->assertNotEmpty($this->tracker->getWeightLog());
    $this->assertEquals(1, $this->tracker->getWeightLog(0, null, '', '', '', true));

    $exercise->setMeasure('lbs');
    $this->entityManager->flush();

    $this->assertNotEmpty($this->tracker->getWeightLog());
    $this->assertEquals(1, $this->tracker->getWeightLog(0, null, '', '', '', true));
}

Here is the code for the first line

public function getWeightLog(int $limit = 0, ?int $offset = null, string $orderColumn = '', string $orderDirection = '', string $searchValue = '', bool $getCount = false)
{
    $qb = $this->createQueryBuilder('et');

    if ($getCount) {
        $qb->select('COUNT(et.id)');
    } else {
        $qb->select('et');
    }

    $qb->where('et.measure = \'kg\' OR et.measure = \'lbs\'')
        ->leftJoin('et.user', 'u')
        ->leftJoin('et.workout', 'w')
        ->leftJoin('et.exercise', 'ex');

    if ($limit) {
        $qb->setMaxResults($limit);
    }

    if (!is_null($offset)) {
        $qb->setFirstResult($offset);
    }

    if ($orderColumn) {
        $qb->orderBy($orderColumn, $orderDirection);
    }

    if ($searchValue !== '') {
        $parts = explode(' ', $searchValue);

        foreach ($parts as $part) {
            if (is_numeric($part)) {
                $qb->andWhere('et.repsQuantity = :part OR et.value = :part OR ex.id = :part OR ex.name = :part');
                $qb->setParameter('part', $part);
            } else {
                $qb->andWhere("et.date LIKE '%$part%' OR u.firstName LIKE '%$part%' 
                    OR u.lastName LIKE '%$part%' OR w.name LIKE '%$part%' OR ex.name LIKE '%$part%' 
                    OR ex.displayName LIKE '%$part%' OR et.measure LIKE '%$part%'");
            }
        }
    }

    if ($getCount) {
        return $qb->getQuery()->getSingleScalarResult();
    } else {
        return $qb->getQuery()->getResult();
    }
}

My question is, Should this test being chewing 100% CPU and eating more than 2GB of RAM?

1

There are 1 answers

0
Jeffrey L. Roberts On

It turns out, I was using a database populated with data from production, and there were queries fetching millions of rows, hence the cpu and memory consumption