Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
12345 anikendra 1
<?php
2
/**
3
 * DebugTimer Test Case
4
 *
5
 * PHP 5
6
 *
7
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
8
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
9
 *
10
 * Licensed under The MIT License
11
 * Redistributions of files must retain the above copyright notice.
12
 *
13
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
14
 * @link          http://cakephp.org CakePHP(tm) Project
15
 * @since         debug_kit 2.0
16
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
17
 */
18
 
19
App::uses('DebugTimer', 'DebugKit.Lib');
20
 
21
/**
22
 * Class DebugTimerTest
23
 *
24
 * @since         debug_kit 2.0
25
 */
26
class DebugTimerTest extends CakeTestCase {
27
 
28
/**
29
 * tearDown method
30
 *
31
 * @return void
32
 */
33
	public function tearDown() {
34
		DebugTimer::clear();
35
	}
36
 
37
/**
38
 * Start Timer test
39
 *
40
 * @return void
41
 */
42
	public function testTimers() {
43
		$this->assertTrue(DebugTimer::start('test1', 'this is my first test'));
44
		usleep(5000);
45
		$this->assertTrue(DebugTimer::stop('test1'));
46
		$elapsed = DebugTimer::elapsedTime('test1');
47
		$this->assertTrue($elapsed > 0.0050);
48
 
49
		$this->assertTrue(DebugTimer::start('test2', 'this is my second test'));
50
		sleep(1);
51
		$this->assertTrue(DebugTimer::stop('test2'));
52
		$elapsed = DebugTimer::elapsedTime('test2');
53
		$expected = stripos(PHP_OS, 'win') === false ? 0.999: 0.95; // Windows timer's precision is bad
54
		$this->assertTrue($elapsed >= $expected);
55
 
56
		DebugTimer::start('test3');
57
		$this->assertIdentical(DebugTimer::elapsedTime('test3'), 0);
58
		$this->assertFalse(DebugTimer::stop('wrong'));
59
	}
60
 
61
/**
62
 * test timers with no names.
63
 *
64
 * @return void
65
 */
66
	public function testAnonymousTimers() {
67
		$this->assertTrue(DebugTimer::start());
68
		usleep(2000);
69
		$this->assertTrue(DebugTimer::stop());
70
		$timers = DebugTimer::getAll();
71
 
72
		$this->assertEquals(2, count($timers));
73
		end($timers);
74
		$key = key($timers);
75
		$lineNo = __LINE__ - 8;
76
 
77
		$file = Debugger::trimPath(__FILE__);
78
		$expected = $file . ' line ' . $lineNo;
79
		$this->assertEquals($expected, $key);
80
 
81
		$timer = $timers[$expected];
82
		$this->assertTrue($timer['time'] > 0.0020);
83
		$this->assertEquals($expected, $timers[$expected]['message']);
84
	}
85
 
86
/**
87
 * Assert that nested anonymous timers don't get mixed up.
88
 *
89
 * @return void
90
 */
91
	public function testNestedAnonymousTimers() {
92
		$this->assertTrue(DebugTimer::start());
93
		usleep(100);
94
		$this->assertTrue(DebugTimer::start());
95
		usleep(100);
96
		$this->assertTrue(DebugTimer::stop());
97
		$this->assertTrue(DebugTimer::stop());
98
 
99
		$timers = DebugTimer::getAll();
100
		$this->assertEquals(3, count($timers), 'incorrect number of timers %s');
101
		$firstTimerLine = __LINE__ - 9;
102
		$secondTimerLine = __LINE__ - 8;
103
		$file = Debugger::trimPath(__FILE__);
104
 
105
		$this->assertTrue(isset($timers[$file . ' line ' . $firstTimerLine]), 'first timer is not set %s');
106
		$this->assertTrue(isset($timers[$file . ' line ' . $secondTimerLine]), 'second timer is not set %s');
107
 
108
		$firstTimer = $timers[$file . ' line ' . $firstTimerLine];
109
		$secondTimer = $timers[$file . ' line ' . $secondTimerLine];
110
		$this->assertTrue($firstTimer['time'] > $secondTimer['time']);
111
	}
112
 
113
/**
114
 * test that calling start with the same name does not overwrite previous timers
115
 * and instead adds new ones.
116
 *
117
 * @return void
118
 */
119
	public function testRepeatTimers() {
120
		DebugTimer::start('my timer', 'This is the first call');
121
		usleep(100);
122
		DebugTimer::start('my timer', 'This is the second call');
123
		usleep(100);
124
 
125
		DebugTimer::stop('my timer');
126
		DebugTimer::stop('my timer');
127
 
128
		$timers = DebugTimer::getAll();
129
		$this->assertEquals(3, count($timers), 'wrong timer count %s');
130
 
131
		$this->assertTrue(isset($timers['my timer']));
132
		$this->assertTrue(isset($timers['my timer #2']));
133
 
134
		$this->assertTrue($timers['my timer']['time'] > $timers['my timer #2']['time'], 'timer 2 is longer? %s');
135
		$this->assertEquals('This is the first call', $timers['my timer']['message']);
136
		$this->assertEquals('This is the second call #2', $timers['my timer #2']['message']);
137
	}
138
 
139
/**
140
 * testRequestTime
141
 *
142
 * @return void
143
 */
144
	public function testRequestTime() {
145
		$result1 = DebugTimer::requestTime();
146
		usleep(50);
147
		$result2 = DebugTimer::requestTime();
148
		$this->assertTrue($result1 < $result2);
149
	}
150
 
151
/**
152
 * test getting all the set timers.
153
 *
154
 * @return void
155
 */
156
	public function testGetTimers() {
157
		DebugTimer::start('test1', 'this is my first test');
158
		DebugTimer::stop('test1');
159
		usleep(50);
160
		DebugTimer::start('test2');
161
		DebugTimer::stop('test2');
162
		$timers = DebugTimer::getAll();
163
 
164
		$this->assertEquals(3, count($timers));
165
		$this->assertTrue(is_float($timers['test1']['time']));
166
		$this->assertTrue(isset($timers['test1']['message']));
167
		$this->assertTrue(isset($timers['test2']['message']));
168
	}
169
}