diff --git a/core/Profiler.php b/core/Profiler.php
index e9297464eeb081efced0860f7ba52dfff74cad58..433fe5a38018253df7eba21707f0ddd418894691 100644
--- a/core/Profiler.php
+++ b/core/Profiler.php
@@ -186,9 +186,11 @@ class Profiler
      * Initializes Profiling via XHProf.
      * See: https://github.com/piwik/piwik/blob/master/tests/README.xhprof.md
      */
-    public static function setupProfilerXHProf($mainRun = false)
+    public static function setupProfilerXHProf($mainRun = false, $setupDuringTracking = false)
     {
-        if(SettingsServer::isTrackerApiRequest()) {
+        if (!$setupDuringTracking
+            && SettingsServer::isTrackerApiRequest()
+        ) {
             // do not profile Tracker
             return;
         }
@@ -237,9 +239,6 @@ class Profiler
             }
             $runs = self::getProfilingRunIds();
             $runs[] = $runId;
-//            $weights = array_fill(0, count($runs), 1);
-//            $aggregate = xhprof_aggregate_runs($xhprofRuns, $runs, $weights, $profilerNamespace);
-//            $runId = $xhprofRuns->save_run($aggregate, $profilerNamespace);
 
             if($mainRun) {
                 $runIds = implode(',', $runs);
diff --git a/plugins/CoreConsole/Commands/TestsRun.php b/plugins/CoreConsole/Commands/TestsRun.php
index 87e8a4a1f11873214d4554f384b65b264ec79696..f2f2530f284561ebf3d3a37fab101efcbe1f2d24 100644
--- a/plugins/CoreConsole/Commands/TestsRun.php
+++ b/plugins/CoreConsole/Commands/TestsRun.php
@@ -16,6 +16,7 @@ use Symfony\Component\Console\Input\InputOption;
 use Symfony\Component\Console\Output\OutputInterface;
 
 /**
+ * Executes PHP tests.
  */
 class TestsRun extends ConsoleCommand
 {
@@ -25,6 +26,7 @@ class TestsRun extends ConsoleCommand
         $this->setDescription('Run Piwik PHPUnit tests one group after the other');
         $this->addArgument('group', InputArgument::OPTIONAL, 'Run only a specific test group. Separate multiple groups by comma, for instance core,integration', '');
         $this->addOption('options', 'o', InputOption::VALUE_OPTIONAL, 'All options will be forwarded to phpunit', '');
+        $this->addOption('xhprof', null, InputOption::VALUE_NONE, 'Profile using xhprof.');
     }
 
     protected function execute(InputInterface $input, OutputInterface $output)
@@ -67,10 +69,17 @@ class TestsRun extends ConsoleCommand
             $command = sprintf('php -d zend_extension=%s %s', $xdebugFile, $phpunitPath);
         }
 
-        if(empty($groups)) {
+        if (empty($groups)) {
             $groups = $this->getTestsGroups();
         }
-        foreach($groups as $group) {
+
+        if ($input->getOption('xhprof')) {
+            Profiler::setupProfilerXHProf($isMainRun = true);
+
+            putenv('PIWIK_USE_XHPROF=1');
+        }
+
+        foreach ($groups as $group) {
             $params = '--group ' . $group . ' ' . str_replace('%group%', $group, $options);
             $cmd = sprintf('cd %s/tests/PHPUnit && %s %s', PIWIK_DOCUMENT_ROOT, $command, $params);
             $output->writeln('Executing command: <info>' . $cmd . '</info>');
@@ -83,5 +92,4 @@ class TestsRun extends ConsoleCommand
     {
         return array('Core', 'Plugins', 'Integration', 'UI');
     }
-
-}
+}
\ No newline at end of file
diff --git a/tests/PHPUnit/Fixture.php b/tests/PHPUnit/Fixture.php
index e7387927c450cf6e3d512d9f748294d5175b6df3..4daa3ff52140a8d4dd38b6763b349ae52e316a95 100644
--- a/tests/PHPUnit/Fixture.php
+++ b/tests/PHPUnit/Fixture.php
@@ -243,6 +243,10 @@ class Fixture extends PHPUnit_Framework_Assert
         if ($this->testEnvironment === null) {
             $this->testEnvironment = new Piwik_TestingEnvironment();
             $this->testEnvironment->delete();
+
+            if (getenv('PIWIK_USE_XHPROF') == 1) {
+                $this->testEnvironment->useXhprof = true;
+            }
         }
         return $this->testEnvironment;
     }
diff --git a/tests/PHPUnit/TestingEnvironment.php b/tests/PHPUnit/TestingEnvironment.php
index dae74b4a3c9c69607f035c282c6326187d3387ca..c61ca8825b7b756dd37fbdd03b9e2f096026bb50 100644
--- a/tests/PHPUnit/TestingEnvironment.php
+++ b/tests/PHPUnit/TestingEnvironment.php
@@ -132,6 +132,10 @@ class Piwik_TestingEnvironment
             }
         }
 
+        if ($testingEnvironment->useXhprof) {
+            \Piwik\Profiler::setupProfilerXHProf($mainRun = false, $setupDuringTracking = true);
+        }
+
         Config::setSingletonInstance(new Config(
             $testingEnvironment->configFileGlobal, $testingEnvironment->configFileLocal, $testingEnvironment->configFileCommon
         ));