PHPUnit和Symfony-简单测试用例以100%占用CPU

时间:2019-11-21 19:16:44

标签: symfony phpunit symfony-3.4 simple-phpunit

我刚刚加入一个项目,并且试图按顺序进行PHPUnit测试。我认为可能有问题,但是我不确定,所以我发布了这个问题。

以下测试需要几分钟的时间才能失败,根据我的阅读,测试应该在10秒或更短的时间内执行。另外,它正在咀嚼100%的CPU。测试在生产中正在测试的内容在毫秒内执行,所以我不明白为什么要花几分钟的时间...旁注,仅在测试用例的第一行就花了几分钟,我在其中放了一些回声,甚至没有到达第二行。

这是测试用例

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));
}

这是第一行的代码

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();
    }
}

我的问题是,该测试是否应该消耗100%的CPU并消耗2GB以上的RAM?

1 个答案:

答案 0 :(得分:0)

事实证明,我正在使用一个填充有生产数据的数据库,并且有一些查询提取了数百万行,因此导致CPU和内存消耗