From d0433b69aab66546d0e11502f3826d04e0b80a75 Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Sat, 10 Nov 2012 16:55:34 +0100 Subject: [PATCH] [Stopwatch] Get the "real size" used & minor tweaks --- .../Resources/views/Collector/time.html.twig | 20 +++++++++---------- .../DataCollector/TimeDataCollector.php | 4 ++-- .../Component/Stopwatch/StopwatchEvent.php | 10 ++++++---- .../Component/Stopwatch/StopwatchPeriod.php | 10 +++++----- .../Stopwatch/Tests/StopwatchEventTest.php | 8 ++++---- .../Stopwatch/Tests/StopwatchTest.php | 4 ++-- 6 files changed, 29 insertions(+), 27 deletions(-) diff --git a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/time.html.twig b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/time.html.twig index 8f16d54836..88c20a3ada 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/time.html.twig +++ b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/time.html.twig @@ -16,15 +16,15 @@ {% endif %} {% block toolbar %} - {% set total_time = collector.events|length ? '%.0f ms'|format(collector.totaltime) : 'n/a' %} + {% set duration = collector.events|length ? '%.0f ms'|format(collector.duration) : 'n/a' %} {% set icon %} Time - {{ total_time }} + {{ duration }} {% endset %} {% set text %}
Total time - {{ total_time }} + {{ duration }}
{% endset %} {% include '@WebProfiler/Profiler/toolbar_item.html.twig' with { 'link': profiler_url } %} @@ -54,7 +54,7 @@ - + @@ -70,7 +70,7 @@

{{ profile.parent ? "Request" : "Main Request" }} - - {{ collector.events.__section__.totaltime }} ms + - {{ collector.events.__section__.duration }} ms {% if profile.parent %} - parent {% endif %} @@ -86,7 +86,7 @@ {% set events = child.getcollector('time').events %}

Sub-request "{{ child.getcollector('request').requestattributes.get('_controller') }}" - - {{ events.__section__.totaltime }} ms + - {{ events.__section__.duration }} ms

{{ display_timeline('timeline_' ~ child.token, events, colors) }} @@ -198,7 +198,7 @@ // Filter events whose total time is below the threshold. drawableEvents = request.events.filter(function(event) { - return event.totaltime >= threshold; + return event.duration >= threshold; }); canvasHeight += gapPerEvent * drawableEvents.length; @@ -315,7 +315,7 @@ ctx.fillStyle = "#444"; ctx.font = "12px sans-serif"; text = event.name; - ms = " ~ " + (event.totaltime < 1 ? event.totaltime : parseInt(event.totaltime, 10)) + " ms / ~ " + event.memory + " MB"; + ms = " ~ " + (event.duration < 1 ? event.duration : parseInt(event.duration, 10)) + " ms / ~ " + event.memory + " MB"; if (x + event.starttime * ratio + ctx.measureText(text + ms).width > width) { ctx.textAlign = "end"; ctx.font = "10px sans-serif"; @@ -453,8 +453,8 @@ "origin": {{ "%F"|format(event.origin) }}, "starttime": {{ "%F"|format(event.starttime) }}, "endtime": {{ "%F"|format(event.endtime) }}, - "totaltime": {{ "%F"|format(event.totaltime) }}, - "memory": {{ "%.1f"|format(event.memory / 1024 / 1024) }}, + "duration": {{ "%F"|format(event.duration) }}, + "memory": {{ "%.1F"|format(event.memory / 1024 / 1024) }}, "periods": [ {%- for period in event.periods -%} {"start": {{ "%F"|format(period.starttime) }}, "end": {{ "%F"|format(period.endtime) }}}{{ loop.last ? '' : ', ' }} diff --git a/src/Symfony/Component/HttpKernel/DataCollector/TimeDataCollector.php b/src/Symfony/Component/HttpKernel/DataCollector/TimeDataCollector.php index 0d5ac81971..5fd2378499 100644 --- a/src/Symfony/Component/HttpKernel/DataCollector/TimeDataCollector.php +++ b/src/Symfony/Component/HttpKernel/DataCollector/TimeDataCollector.php @@ -70,11 +70,11 @@ class TimeDataCollector extends DataCollector * * @return float The elapsed time */ - public function getTotalTime() + public function getDuration() { $lastEvent = $this->data['events']['__section__']; - return $lastEvent->getOrigin() + $lastEvent->getTotalTime() - $this->getStartTime(); + return $lastEvent->getOrigin() + $lastEvent->getDuration() - $this->getStartTime(); } /** diff --git a/src/Symfony/Component/Stopwatch/StopwatchEvent.php b/src/Symfony/Component/Stopwatch/StopwatchEvent.php index a8146fe6f7..f54095ba71 100644 --- a/src/Symfony/Component/Stopwatch/StopwatchEvent.php +++ b/src/Symfony/Component/Stopwatch/StopwatchEvent.php @@ -75,6 +75,8 @@ class StopwatchEvent * Stops the last started event period. * * @return StopwatchEvent The event + * + * @throws \LogicException When stop() is called without a matching call to start() */ public function stop() { @@ -138,15 +140,15 @@ class StopwatchEvent } /** - * Gets the total time of all periods. + * Gets the duration of the events (including all periods). * - * @return integer The time (in milliseconds) + * @return integer The duration (in milliseconds) */ - public function getTotalTime() + public function getDuration() { $total = 0; foreach ($this->periods as $period) { - $total += $period->getTime(); + $total += $period->getDuration(); } return $this->formatTime($total); diff --git a/src/Symfony/Component/Stopwatch/StopwatchPeriod.php b/src/Symfony/Component/Stopwatch/StopwatchPeriod.php index 221ee792f7..57393f4742 100644 --- a/src/Symfony/Component/Stopwatch/StopwatchPeriod.php +++ b/src/Symfony/Component/Stopwatch/StopwatchPeriod.php @@ -30,9 +30,9 @@ class StopwatchPeriod */ public function __construct($start, $end) { - $this->start = $start; - $this->end = $end; - $this->memory = memory_get_usage(); + $this->start = (integer) $start; + $this->end = (integer) $end; + $this->memory = memory_get_usage(true); } /** @@ -58,9 +58,9 @@ class StopwatchPeriod /** * Gets the time spent in this period. * - * @return integer The time (in milliseconds) + * @return integer The period duration (in milliseconds) */ - public function getTime() + public function getDuration() { return $this->end - $this->start; } diff --git a/src/Symfony/Component/Stopwatch/Tests/StopwatchEventTest.php b/src/Symfony/Component/Stopwatch/Tests/StopwatchEventTest.php index f4e3cd7f86..1ba69d19fa 100644 --- a/src/Symfony/Component/Stopwatch/Tests/StopwatchEventTest.php +++ b/src/Symfony/Component/Stopwatch/Tests/StopwatchEventTest.php @@ -62,13 +62,13 @@ class StopwatchEventTest extends \PHPUnit_Framework_TestCase $this->assertCount(2, $event->getPeriods()); } - public function testTotalTime() + public function testDuration() { $event = new StopwatchEvent(microtime(true) * 1000); $event->start(); usleep(20000); $event->stop(); - $total = $event->getTotalTime(); + $total = $event->getDuration(); $this->assertTrue($total >= 11 && $total <= 29, $total.' should be 20 (between 11 and 29)'); $event = new StopwatchEvent(microtime(true) * 1000); @@ -78,7 +78,7 @@ class StopwatchEventTest extends \PHPUnit_Framework_TestCase $event->start(); usleep(10000); $event->stop(); - $total = $event->getTotalTime(); + $total = $event->getDuration(); $this->assertTrue($total >= 11 && $total <= 29, $total.' should be 20 (between 11 and 29)'); } @@ -100,7 +100,7 @@ class StopwatchEventTest extends \PHPUnit_Framework_TestCase $event->start(); usleep(10000); $event->ensureStopped(); - $total = $event->getTotalTime(); + $total = $event->getDuration(); $this->assertTrue($total >= 21 && $total <= 39, $total.' should be 30 (between 21 and 39)'); } diff --git a/src/Symfony/Component/Stopwatch/Tests/StopwatchTest.php b/src/Symfony/Component/Stopwatch/Tests/StopwatchTest.php index 8a77d8a0aa..d1dd2e616b 100644 --- a/src/Symfony/Component/Stopwatch/Tests/StopwatchTest.php +++ b/src/Symfony/Component/Stopwatch/Tests/StopwatchTest.php @@ -37,7 +37,7 @@ class StopwatchTest extends \PHPUnit_Framework_TestCase $event = $stopwatch->stop('foo'); $this->assertInstanceof('Symfony\Component\Stopwatch\StopwatchEvent', $event); - $total = $event->getTotalTime(); + $total = $event->getDuration(); $this->assertTrue($total > 10 && $total <= 29, $total.' should be 20 (between 10 and 29)'); } @@ -51,7 +51,7 @@ class StopwatchTest extends \PHPUnit_Framework_TestCase $stopwatch->stop('foo'); $this->assertInstanceof('Symfony\Component\Stopwatch\StopwatchEvent', $event); - $total = $event->getTotalTime(); + $total = $event->getDuration(); $this->assertTrue($total > 10 && $total <= 29, $total.' should be 20 (between 10 and 29)'); }

Total time{{ '%.0f'|format(collector.totaltime) }} ms{{ '%.0f'|format(collector.duration) }} ms
Initialization time