Jyxo\TimerTest::testDuration PHP Method

testDuration() public method

Tests duration measuring.
public testDuration ( )
    public function testDuration()
    {
        $mt = microtime(true);
        $name = Timer::start();
        usleep(100);
        $delta = Timer::stop($name);
        $outerDelta = microtime(true) - $mt;
        // Measured time is greater than 0
        $this->assertGreaterThan(0, $delta);
        // Measured time is less than the time between the two function calls
        $this->assertLessThan($outerDelta, $delta);
        // Non-existent timer
        $this->assertSame(0.0, Timer::stop('foo'));
        // Start 4 timers
        $names = ['foo', 'bar', 'tmp', 'ohai'];
        $times = array_fill_keys($names, 0);
        foreach ($names as $name) {
            Timer::start($name);
        }
        // End them in reverse order
        foreach (array_reverse($names) as $name) {
            usleep(100);
            $times[$name] = Timer::stop($name);
        }
        // The measured time is supposed to be in descending order
        foreach ($names as $i => $name) {
            $this->assertGreaterThan(0, $times[$name]);
            if ($i > 0) {
                $this->assertLessThan($times[$names[$i - 1]], $times[$name]);
            }
        }
    }