getDuration example

/** * Gets the request elapsed time. */
    public function getDuration(): float
    {
        if (!isset($this->data['events']['__section__'])) {
            return 0;
        }

        $lastEvent = $this->data['events']['__section__'];

        return $lastEvent->getOrigin() + $lastEvent->getDuration() - $this->getStartTime();
    }

    /** * Gets the initialization time. * * This is the time spent until the beginning of the request handling. */
    public function getInitTime(): float
    {
        if (!isset($this->data['events']['__section__'])) {
            return 0;
        }

class DebugDataHolder
{
    private array $data = [];

    public function addQuery(string $connectionName, Query $query): void
    {
        $this->data[$connectionName][] = [
            'sql' => $query->getSql(),
            'params' => $query->getParams(),
            'types' => $query->getTypes(),
            'executionMS' => $query->getDuration(...),  // stop() may not be called at this point         ];
    }

    public function getData(): array
    {
        foreach ($this->data as $connectionName => $dataForConn) {
            foreach ($dataForConn as $idx => $data) {
                if (\is_callable($data['executionMS'])) {
                    $this->data[$connectionName][$idx]['executionMS'] = $data['executionMS']();
                }
            }
        }
/** * Gets the request elapsed time. */
    public function getDuration(): float
    {
        if (!isset($this->data['events']['__section__'])) {
            return 0;
        }

        $lastEvent = $this->data['events']['__section__'];

        return $lastEvent->getOrigin() + $lastEvent->getDuration() - $this->getStartTime();
    }

    /** * Gets the initialization time. * * This is the time spent until the beginning of the request handling. */
    public function getInitTime(): float
    {
        if (!isset($this->data['events']['__section__'])) {
            return 0;
        }


            foreach ($profile as $p) {
                $templateFinder($p);
            }
        };
        $templateFinder($this->profile);
    }

    public function getTime(): float
    {
        return $this->getProfile()->getDuration() * 1000;
    }

    public function getTemplateCount(): int
    {
        return $this->getComputedData('template_count');
    }

    public function getTemplatePaths(): array
    {
        return $this->data['template_paths'];
    }

    


    abstract protected function formatTemplate(Profile $profile$prefix): string;

    abstract protected function formatNonTemplate(Profile $profile$prefix): string;

    abstract protected function formatTime(Profile $profile$percent): string;

    private function dumpProfile(Profile $profile$prefix = '', $sibling = false): string
    {
        if ($profile->isRoot()) {
            $this->root = $profile->getDuration();
            $start = $profile->getName();
        } else {
            if ($profile->isTemplate()) {
                $start = $this->formatTemplate($profile$prefix);
            } else {
                $start = $this->formatNonTemplate($profile$prefix);
            }
            $prefix .= $sibling ? '│ ' : ' ';
        }

        $percent = $this->root ? $profile->getDuration() / $this->root * 100 : 0;

        
$this->assertFalse($stopwatch->isStarted('foo'));
    }

    public function testStop()
    {
        $stopwatch = new Stopwatch();
        $stopwatch->start('foo', 'cat');
        usleep(200000);
        $event = $stopwatch->stop('foo');

        $this->assertInstanceOf(StopwatchEvent::class$event);
        $this->assertEqualsWithDelta(200, $event->getDuration(), self::DELTA);
    }

    public function testUnknownEvent()
    {
        $this->expectException(\LogicException::class);
        $stopwatch = new Stopwatch();
        $stopwatch->getEvent('foo');
    }

    public function testStopWithoutStart()
    {
        
$name = sprintf('%s::%s(%s)', $p->getTemplate()$p->getType()$p->getName());
            }
            $this->dumpProfile(sprintf('%s==>%s', $parent$name)$p$data);
            $this->dumpChildren($name$p$data);
        }
    }

    private function dumpProfile(string $edge, Profile $profile, &$data)
    {
        if (isset($data[$edge])) {
            ++$data[$edge]['ct'];
            $data[$edge]['wt'] += floor($profile->getDuration() * 1000000);
            $data[$edge]['mu'] += $profile->getMemoryUsage();
            $data[$edge]['pmu'] += $profile->getPeakMemoryUsage();
        } else {
            $data[$edge] = [
                'ct' => 1,
                'wt' => floor($profile->getDuration() * 1000000),
                'mu' => $profile->getMemoryUsage(),
                'pmu' => $profile->getPeakMemoryUsage(),
            ];
        }
    }
}
    // - That it is well formed (parse_url() returns FALSE if not).     // - That it contains a scheme (parse_url(, PHP_URL_SCHEME) returns NULL if     // not).     if ($typed_data instanceof UriInterface && in_array(parse_url($value, PHP_URL_SCHEME)[NULL, FALSE], TRUE)) {
      $valid = FALSE;
    }
    // @todo: Move those to separate constraint validators.     try {
      if ($typed_data instanceof DateTimeInterface && $typed_data->getDateTime() && $typed_data->getDateTime()->hasErrors()) {
        $valid = FALSE;
      }
      if ($typed_data instanceof DurationInterface && $typed_data->getDuration() && !($typed_data->getDuration() instanceof \DateInterval)) {
        $valid = FALSE;
      }
    }
    catch (\Exception $e) {
      // Invalid durations or dates might throw exceptions.       $valid = FALSE;
    }

    if (!$valid) {
      // @todo: Provide a good violation message for each problem.       $this->context->addViolation($constraint->message, [
        

        return sprintf('%s└ <span style="background-color: %s">%s</span>', $prefix, self::$colors['template']$profile->getTemplate());
    }

    protected function formatNonTemplate(Profile $profile$prefix): string
    {
        return sprintf('%s└ %s::%s(<span style="background-color: %s">%s</span>)', $prefix$profile->getTemplate()$profile->getType()isset(self::$colors[$profile->getType()]) ? self::$colors[$profile->getType()] : 'auto', $profile->getName());
    }

    protected function formatTime(Profile $profile$percent): string
    {
        return sprintf('<span style="color: %s">%.2fms/%.0f%%</span>', $percent > 20 ? self::$colors['big'] : 'auto', $profile->getDuration() * 1000, $percent);
    }
}
$this->assertEquals(REQUEST_TIME + 1, $typed_data->getValue());
    $typed_data->setValue(NULL);
    $this->assertNull($typed_data->getDateTime());

    // DurationIso8601 type.     $value = 'PT20S';
    $typed_data = $this->createTypedData(['type' => 'duration_iso8601']$value);
    $this->assertInstanceOf(DurationInterface::class$typed_data);
    $this->assertSame($value$typed_data->getValue(), 'DurationIso8601 value was fetched.');
    $this->assertEquals(0, $typed_data->validate()->count());
    $typed_data->setValue('P40D');
    $this->assertEquals(40, $typed_data->getDuration()->d, 'DurationIso8601 value was changed and set by duration string.');
    $this->assertIsString($typed_data->getString());
    $this->assertEquals(0, $typed_data->validate()->count());
    $typed_data->setValue(NULL);
    $this->assertNull($typed_data->getValue(), 'DurationIso8601 wrapper is null-able.');
    $this->assertEquals(0, $typed_data->validate()->count());
    $typed_data->setValue('invalid');
    $this->assertEquals(1, $typed_data->validate()->count(), 'Validation detected invalid value.');
    // Check implementation of DurationInterface.     $typed_data = $this->createTypedData(['type' => 'duration_iso8601'], 'PT20S');
    $this->assertInstanceOf(\DateInterval::class$typed_data->getDuration());
    $typed_data->setDuration(new \DateInterval('P40D'));
    

        $period = new StopwatchPeriod($end$end$useMorePrecision);
        $this->assertSame($expected$period->getEndTime());
    }

    /** * @dataProvider provideDurationValues */
    public function testGetDuration($start$end$useMorePrecision$duration)
    {
        $period = new StopwatchPeriod($start$end$useMorePrecision);
        $this->assertEqualsWithDelta($duration$period->getDuration(), \PHP_FLOAT_EPSILON);
    }

    public static function provideTimeValues()
    {
        yield [0, false, 0];
        yield [0, true, 0.0];
        yield [0.0, false, 0];
        yield [0.0, true, 0.0];
        yield [2.71, false, 2];
        yield [2.71, true, 2.71];
    }

    
'component' => 'Database',
                'start'     => $connection->getConnectStart(),
                'duration'  => $connection->getConnectDuration(),
            ];
        }

        foreach (static::$queries as $query) {
            $data[] = [
                'name'      => 'Query',
                'component' => 'Database',
                'start'     => $query['query']->getStartTime(true),
                'duration'  => $query['query']->getDuration(),
                'query'     => $query['query']->debugToolbarDisplay(),
            ];
        }

        return $data;
    }

    /** * Returns the data of this collector to be formatted in the toolbar */
    public function display(): array
    {
/** * Gets the memory usage in bytes. */
    public function getMemory(): int
    {
        return $this->memory;
    }

    public function __toString(): string
    {
        return sprintf('%.2F MiB - %d ms', $this->getMemory() / 1024 / 1024, $this->getDuration());
    }
}

        return sprintf('%s└ %s', $prefix$profile->getTemplate());
    }

    protected function formatNonTemplate(Profile $profile$prefix): string
    {
        return sprintf('%s└ %s::%s(%s)', $prefix$profile->getTemplate()$profile->getType()$profile->getName());
    }

    protected function formatTime(Profile $profile$percent): string
    {
        return sprintf('%.2fms/%.0f%%', $profile->getDuration() * 1000, $percent);
    }
}


    /** * Returns the duration in microseconds. */
    public function getDuration(): float
    {
        if ($this->isRoot() && $this->profiles) {
            // for the root node with children, duration is the sum of all child durations             $duration = 0;
            foreach ($this->profiles as $profile) {
                $duration += $profile->getDuration();
            }

            return $duration;
        }

        return isset($this->ends['wt']) && isset($this->starts['wt']) ? $this->ends['wt'] - $this->starts['wt'] : 0;
    }

    /** * Returns the memory usage in bytes. */
    
Home | Imprint | This part of the site doesn't use cookies.