Skip to content

Commit

Permalink
rex_timer::measure: Umgang mit Exceptions (#3173)
Browse files Browse the repository at this point in the history
  • Loading branch information
gharlan authored and kodiakhq[bot] committed Jan 19, 2020
1 parent b98c960 commit 93e7d7f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
14 changes: 8 additions & 6 deletions redaxo/src/core/lib/util/timer.php
Expand Up @@ -57,15 +57,17 @@ public static function measure($label, callable $callable)
}

$timer = new self();
$result = $callable();
$timer->stop();

$duration = isset(self::$serverTimings[$label]) ? self::$serverTimings[$label] : 0;
$duration += $timer->getDelta(self::MILLISEC);
try {
return $callable();
} finally {
$timer->stop();

self::$serverTimings[$label] = $duration;
$duration = isset(self::$serverTimings[$label]) ? self::$serverTimings[$label] : 0;
$duration += $timer->getDelta(self::MILLISEC);

return $result;
self::$serverTimings[$label] = $duration;
}
}

/**
Expand Down
43 changes: 43 additions & 0 deletions redaxo/src/core/tests/util/timer_test.php
@@ -0,0 +1,43 @@
<?php

use PHPUnit\Framework\TestCase;

/**
* @internal
*/
class rex_timer_test extends TestCase
{
public function testMeasure(): void
{
$callable = static function () {
static $i = 1;
return 'result'.($i++);
};

$result = rex_timer::measure('test', $callable);
$timing = rex_timer::$serverTimings['test'];

$this->assertSame('result1', $result);
$this->assertIsFloat($timing);
$this->assertGreaterThan(0, $timing);

$result = rex_timer::measure('test', $callable);

$this->assertSame('result2', $result);
$this->assertGreaterThan($timing, rex_timer::$serverTimings['test']);

$exception = null;
try {
rex_timer::measure('test2', static function () {
throw new RuntimeException();
});
} catch (Throwable $exception) {
}

$timing = rex_timer::$serverTimings['test'];

$this->assertInstanceOf(RuntimeException::class, $exception);
$this->assertIsFloat($timing);
$this->assertGreaterThan(0, $timing);
}
}

0 comments on commit 93e7d7f

Please sign in to comment.