Skip to content

Commit 8946e2f

Browse files
committed
Improve util.profiling.Timer class
. Add ability to provide intermediate results with `elapsedTime()` without prior cal top `stop()` . Add fluent interface to the start() and stop() methods
1 parent d650119 commit 8946e2f

File tree

3 files changed

+56
-32
lines changed

3 files changed

+56
-32
lines changed

ChangeLog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ XP Framework Core ChangeLog
110110

111111
### Features
112112

113+
* Changed util.profiling.Timer to be able to provide intermediate results
114+
with `elapsedTime()` without prior call to `stop()`, and added fluent
115+
interface to util.profiling.Timer's start() and stop() methods - (@thekid)
113116
* Implemented taking exceptions from tearDown() into account for test failure /
114117
success in the unittest package, see xp-framework/core#32 - (@thekid)
115118
* Implemented pushing back bytes to buffered stream (see xp-framework/core#16)

src/main/php/util/profiling/Timer.class.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,27 @@
1515
* @purpose Provide a simple profiling timer
1616
*/
1717
class Timer extends \lang\Object {
18-
public
19-
$start = 0.0,
20-
$stop = 0.0;
18+
protected $start= null;
19+
protected $stop= null;
2120

2221
/**
2322
* Start the timer
2423
*
24+
* @return self
2525
*/
2626
public function start() {
2727
$this->start= microtime(true);
28+
return $this;
2829
}
2930

3031
/**
3132
* Stop the timer
3233
*
34+
* @return self
3335
*/
3436
public function stop() {
3537
$this->stop= microtime(true);
38+
return $this;
3639
}
3740

3841
/**
@@ -61,6 +64,12 @@ public static function measure($block) {
6164
* @return float seconds elapsed
6265
*/
6366
public function elapsedTime() {
64-
return $this->stop - $this->start;
67+
if (null === $this->start) {
68+
return 0.0;
69+
} else if (null === $this->stop) {
70+
return microtime(true) - $this->start;
71+
} else {
72+
return $this->stop - $this->start;
73+
}
6574
}
6675
}

src/test/php/net/xp_framework/unittest/util/TimerTest.class.php

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,56 +4,68 @@
44
use util\profiling\Timer;
55
use util\Comparator;
66

7-
87
/**
98
* Tests Timer class
109
*
1110
* @see xp://util.profiling.Timer
1211
*/
1312
class TimerTest extends TestCase {
14-
15-
/**
16-
* Tests elapsed time is zero if not started yet
17-
*
18-
*/
13+
14+
#[@test]
15+
public function can_create() {
16+
new Timer();
17+
}
18+
1919
#[@test]
20-
public function initiallyZero() {
20+
public function elapsed_time_is_zero_before_timer_is_started() {
2121
$this->assertEquals(0.0, (new Timer())->elapsedTime());
2222
}
2323

24-
/**
25-
* Tests elapsed time after 100 milliseconds
26-
*
27-
*/
2824
#[@test]
29-
public function elapsedTimeGreaterThanZeroUsingStartAndStop() {
30-
$fixture= new Timer();
31-
$fixture->start();
32-
usleep(100 * 1000);
33-
$fixture->stop();
25+
public function start_returns_timer() {
26+
$t= new Timer();
27+
$this->assertEquals($t, $t->start());
28+
}
29+
30+
#[@test]
31+
public function stop_returns_timer() {
32+
$t= new Timer();
33+
$t->start();
34+
$this->assertEquals($t, $t->stop());
35+
}
36+
37+
#[@test]
38+
public function calling_stop_before_start_does_not_raise_exception() {
39+
(new Timer())->stop();
40+
}
41+
42+
#[@test]
43+
public function elapsed_time_after_50_milliseconds() {
44+
$fixture= (new Timer())->start();
45+
usleep(50 * 1000);
46+
$elapsed= $fixture->stop()->elapsedTime();
47+
$this->assertTrue($elapsed > 0.0, 'Elapsed time '.$elapsed.' should be greater than zero');
48+
}
49+
50+
#[@test]
51+
public function elapsed_time_without_stop_after_50_milliseconds() {
52+
$fixture= (new Timer())->start();
53+
usleep(50 * 1000);
3454
$elapsed= $fixture->elapsedTime();
35-
$this->assertTrue($elapsed > 0.0, 'Elapsed time should be greater than zero');
55+
$this->assertTrue($elapsed > 0.0, 'Elapsed time '.$elapsed.' should be greater than zero');
3656
}
3757

38-
/**
39-
* Tests elapsed time after 100 milliseconds
40-
*
41-
*/
4258
#[@test]
43-
public function elapsedTimeGreaterThanZeroUsingMeasure() {
59+
public function elapsed_time_measured_after_50_milliseconds() {
4460
$fixture= Timer::measure(function() {
4561
usleep(100 * 1000);
4662
});
4763
$elapsed= $fixture->elapsedTime();
48-
$this->assertTrue($elapsed > 0.0, 'Elapsed time should be greater than zero');
64+
$this->assertTrue($elapsed > 0.0, 'Elapsed time '.$elapsed.' should be greater than zero');
4965
}
5066

51-
/**
52-
* Tests measure()
53-
*
54-
*/
5567
#[@test, @expect('lang.IllegalArgumentException')]
56-
public function notCallable() {
68+
public function not_callable_argument_passed_to_measure() {
5769
Timer::measure('@not-callable@');
5870
}
5971
}

0 commit comments

Comments
 (0)