[BUGFIX] Fix parse time calculation 13/52813/4
authorSusanne Moog <susanne.moog@typo3.com>
Fri, 5 May 2017 15:20:16 +0000 (17:20 +0200)
committerChristian Kuhn <lolli@schwarzbu.ch>
Wed, 17 May 2017 15:11:11 +0000 (17:11 +0200)
Parse time calculation in RequestHandler was wrong,
additionally the same calculation is used in admin panel view.

The calculation was therefore added to the TimeTracker into a
common function and tested.

Change-Id: Ib935664e78a62deab7818426e2a853087c00b015
Resolves: #80742
Releases: master, 8.7
Reviewed-on: https://review.typo3.org/52813
Reviewed-by: Oliver Klee <typo3-coding@oliverklee.de>
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
typo3/sysext/core/Classes/TimeTracker/TimeTracker.php
typo3/sysext/core/Tests/Unit/TimeTracker/TimeTrackerTest.php [new file with mode: 0644]
typo3/sysext/frontend/Classes/Http/RequestHandler.php
typo3/sysext/frontend/Classes/View/AdminPanelView.php

index 3dcddf5..2af4cbf 100644 (file)
@@ -295,6 +295,28 @@ class TimeTracker implements SingletonInterface
         return $this->getMilliseconds($microtime) - $this->starttime;
     }
 
+    /**
+     * Get total parse time in milliseconds(without backend user initialization)
+     *
+     * @return int
+     */
+    public function getParseTime(): int
+    {
+        // Compensates for the time consumed with Back end user initialization.
+        $processStart = isset($GLOBALS['TYPO3_MISC']['microtime_start']) ? $GLOBALS['TYPO3_MISC']['microtime_start'] : null;
+        $processEnd = isset($GLOBALS['TYPO3_MISC']['microtime_end']) ? $GLOBALS['TYPO3_MISC']['microtime_end'] : null;
+        $totalParseTime = $this->getMilliseconds($processEnd) - $this->getMilliseconds($processStart);
+
+        $beUserInitializationStart = isset($GLOBALS['TYPO3_MISC']['microtime_BE_USER_start']) ? $GLOBALS['TYPO3_MISC']['microtime_BE_USER_start'] : null;
+        $beUserInitializationEnd = isset($GLOBALS['TYPO3_MISC']['microtime_BE_USER_end']) ? $GLOBALS['TYPO3_MISC']['microtime_BE_USER_end'] : null;
+        $beUserInitialization = $this->getMilliseconds($beUserInitializationEnd) - $this->getMilliseconds($beUserInitializationStart);
+        if ($beUserInitialization > 0) {
+            $totalParseTime -= $beUserInitialization;
+        }
+
+        return $totalParseTime;
+    }
+
     /*******************************************
      *
      * Printing the parsing time information (for Admin Panel)
diff --git a/typo3/sysext/core/Tests/Unit/TimeTracker/TimeTrackerTest.php b/typo3/sysext/core/Tests/Unit/TimeTracker/TimeTrackerTest.php
new file mode 100644 (file)
index 0000000..423003e
--- /dev/null
@@ -0,0 +1,82 @@
+<?php
+declare(strict_types=1);
+namespace TYPO3\CMS\Core\Tests\Unit\TimeTracker;
+
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+use TYPO3\CMS\Core\TimeTracker\TimeTracker;
+use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
+
+/**
+ * Class TimeTrackerTest
+ */
+class TimeTrackerTest extends UnitTestCase
+{
+    /**
+     * @test
+     */
+    public function getParseTimeReturnsZeroIfNoValuesAreSet()
+    {
+        unset(
+            $GLOBALS['TYPO3_MISC']['microtime_end'],
+            $GLOBALS['TYPO3_MISC']['microtime_start'],
+            $GLOBALS['TYPO3_MISC']['microtime_BE_USER_start'],
+            $GLOBALS['TYPO3_MISC']['microtime_BE_USER_end']
+        );
+        $parseTime = (new TimeTracker())->getParseTime();
+        self::assertSame(0, $parseTime);
+    }
+
+    /**
+     * @test
+     */
+    public function getParseTimeReturnsTotalParseTimeInMillisecondsWithoutBeUserInitialization()
+    {
+        $baseValue = time();
+        $GLOBALS['TYPO3_MISC']['microtime_start'] = $baseValue;
+        $GLOBALS['TYPO3_MISC']['microtime_end'] = $baseValue + 10;
+        $GLOBALS['TYPO3_MISC']['microtime_BE_USER_start'] = $baseValue + 1;
+        $GLOBALS['TYPO3_MISC']['microtime_BE_USER_end'] = $baseValue + 3;
+        $parseTime = (new TimeTracker())->getParseTime();
+        self::assertSame(8000, $parseTime);
+    }
+
+    /**
+     * @test
+     */
+    public function getParseTimeReturnsParseTimeIfOnlyOneBeUserTimeWasSet()
+    {
+        $baseValue = time();
+        $GLOBALS['TYPO3_MISC']['microtime_start'] = $baseValue;
+        $GLOBALS['TYPO3_MISC']['microtime_end'] = $baseValue + 10;
+        $GLOBALS['TYPO3_MISC']['microtime_BE_USER_start'] = $baseValue + 1;
+        $GLOBALS['TYPO3_MISC']['microtime_BE_USER_end'] = 0;
+        $parseTime = (new TimeTracker())->getParseTime();
+        self::assertSame(10000, $parseTime);
+    }
+
+    /**
+     * @test
+     */
+    public function getParseTimeReturnsParseTimeIfNoBeUserTimeWasSet()
+    {
+        $baseValue = time();
+        $GLOBALS['TYPO3_MISC']['microtime_start'] = $baseValue;
+        $GLOBALS['TYPO3_MISC']['microtime_end'] = $baseValue + 10;
+        $GLOBALS['TYPO3_MISC']['microtime_BE_USER_start'] = 0;
+        $GLOBALS['TYPO3_MISC']['microtime_BE_USER_end'] = 0;
+        $parseTime = (new TimeTracker())->getParseTime();
+        self::assertSame(10000, $parseTime);
+    }
+}
index ecc129e..0895d8a 100644 (file)
@@ -244,7 +244,7 @@ class RequestHandler implements RequestHandlerInterface
                 $debugParseTime = !empty($GLOBALS['TYPO3_CONF_VARS']['FE']['debug']);
             }
             if ($debugParseTime) {
-                $this->controller->content .= LF . '<!-- Parsetime: ' . $this->getParseTime() . 'ms -->';
+                $this->controller->content .= LF . '<!-- Parsetime: ' . $this->timeTracker->getParseTime() . 'ms -->';
             }
         }
         $this->controller->redirectToExternalUrl();
@@ -348,22 +348,4 @@ class RequestHandler implements RequestHandlerInterface
         // This is a dirty workaround and bypasses the protected access modifier of the controller member.
         $GLOBALS['TSFE'] = &$this->controller;
     }
-
-    /**
-     * Calculates the parsetime of the page and returns it.
-     *
-     * @return int the parse time of the page
-     */
-    protected function getParseTime()
-    {
-        // Compensates for the time consumed with Back end user initialization.
-        $processStart = isset($GLOBALS['TYPO3_MISC']['microtime_start']) ? $GLOBALS['TYPO3_MISC']['microtime_start'] : null;
-        $processEnd = isset($GLOBALS['TYPO3_MISC']['microtime_end']) ? $GLOBALS['TYPO3_MISC']['microtime_end'] : null;
-        $beUserInitializationStart = isset($GLOBALS['TYPO3_MISC']['microtime_BE_USER_start']) ? $GLOBALS['TYPO3_MISC']['microtime_BE_USER_start'] : null;
-        $beUserInitializationEnd = isset($GLOBALS['TYPO3_MISC']['microtime_BE_USER_end']) ? $GLOBALS['TYPO3_MISC']['microtime_BE_USER_end'] : null;
-        return $this->timeTracker->getMilliseconds($processStart)
-                - $this->timeTracker->getMilliseconds($processEnd)
-                - ($this->timeTracker->getMilliseconds($beUserInitializationStart)
-                - $this->timeTracker->getMilliseconds($beUserInitializationEnd));
-    }
 }
index 7e5b058..27df96f 100644 (file)
@@ -765,7 +765,8 @@ class AdminPanelView
                 $tableArr[] = [$this->extGetLL('info_feuserName'), htmlspecialchars($tsfe->fe_user->user['username'])];
                 $tableArr[] = [$this->extGetLL('info_feuserId'), htmlspecialchars($tsfe->fe_user->user['uid'])];
             }
-            $tableArr[] = [$this->extGetLL('info_totalParsetime'), $tsfe->scriptParseTime . ' ms', true];
+
+            $tableArr[] = [$this->extGetLL('info_totalParsetime'), $this->getTimeTracker()->getParseTime() . ' ms', true];
             $table = '';
             foreach ($tableArr as $key => $arr) {
                 $label = (isset($arr[2]) ? '<strong>' . $arr[0] . '</strong>' : $arr[0]);