DbalPerformanceTestCase.php 1.24 KB
Newer Older
1 2 3
<?php

namespace Doctrine\Tests;
Sergei Morozov's avatar
Sergei Morozov committed
4

5
use function microtime;
6 7

/**
8
 * Base class for all DBAL performance tests.
Sergei Morozov's avatar
Sergei Morozov committed
9
 *
10 11 12
 * Tests implemented in this class must call startTiming at the beginning
 * and stopTiming at the end of all tests. Tests that do not start or stop
 * timing will fail.
13
 */
14
abstract class DbalPerformanceTestCase extends DbalFunctionalTestCase
15 16
{
    /**
17 18 19
     * time the test started
     *
     * @var float
20
     */
21
    private $startTime;
22 23

    /**
24 25 26
     * elapsed run time of the last test
     *
     * @var float
27
     */
28
    private $runTime;
29

30
    protected function assertPostConditions() : void
31 32
    {
        // If a perf test doesn't start or stop, it fails.
Sergei Morozov's avatar
Sergei Morozov committed
33 34
        self::assertNotNull($this->startTime, 'Test timing was started');
        self::assertNotNull($this->runTime, 'Test timing was stopped');
35 36 37 38 39
    }

    /**
     * begin timing
     */
40
    protected function startTiming() : void
41 42 43 44 45 46
    {
        $this->startTime = microtime(true);
    }

    /**
     * end timing
47
     */
48
    protected function stopTiming() : void
49
    {
50
        $this->runTime = microtime(true) - $this->startTime;
51 52 53
    }

    /**
54
     * @return float elapsed test execution time
55
     */
56
    public function getTime() : float
57
    {
58
        return $this->runTime;
59 60
    }
}