aa60101bcdc1592979b8a91c0795bef54ab8b36b
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / TimingTest.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @author Ori Livneh <ori@wikimedia.org>
20 */
21
22 class TimingTest extends PHPUnit_Framework_TestCase {
23
24 /**
25 * @covers Timing::clearMarks
26 * @covers Timing::getEntries
27 */
28 public function testClearMarks() {
29 $timing = new Timing;
30 $this->assertCount( 1, $timing->getEntries() );
31
32 $timing->mark( 'a' );
33 $timing->mark( 'b' );
34 $this->assertCount( 3, $timing->getEntries() );
35
36 $timing->clearMarks( 'a' );
37 $this->assertNull( $timing->getEntryByName( 'a' ) );
38 $this->assertNotNull( $timing->getEntryByName( 'b' ) );
39
40 $timing->clearMarks();
41 $this->assertCount( 1, $timing->getEntries() );
42 }
43
44 /**
45 * @covers Timing::mark
46 * @covers Timing::getEntryByName
47 */
48 public function testMark() {
49 $timing = new Timing;
50 $timing->mark( 'a' );
51
52 $entry = $timing->getEntryByName( 'a' );
53 $this->assertEquals( 'a', $entry['name'] );
54 $this->assertEquals( 'mark', $entry['entryType'] );
55 $this->assertArrayHasKey( 'startTime', $entry );
56 $this->assertEquals( 0, $entry['duration'] );
57
58 $timing->mark( 'a' );
59 $newEntry = $timing->getEntryByName( 'a' );
60 $this->assertGreaterThan( $entry['startTime'], $newEntry['startTime'] );
61 }
62
63 /**
64 * @covers Timing::measure
65 */
66 public function testMeasure() {
67 $timing = new Timing;
68
69 $timing->mark( 'a' );
70 $a = $timing->getEntryByName( 'a' );
71
72 $timing->mark( 'b' );
73 $b = $timing->getEntryByName( 'b' );
74
75 $timing->measure( 'a_to_b', 'a', 'b' );
76
77 $entry = $timing->getEntryByName( 'a_to_b' );
78 $this->assertEquals( 'a_to_b', $entry['name'] );
79 $this->assertEquals( 'measure', $entry['entryType'] );
80 $this->assertEquals( $a['startTime'], $entry['startTime'] );
81 $this->assertEquals( $b['startTime'] - $a['startTime'], $entry['duration'] );
82 }
83
84 /**
85 * @covers Timing::getEntriesByType
86 */
87 public function testGetEntriesByType() {
88 $timing = new Timing;
89
90 $timing->mark( 'mark_a' );
91 usleep( 100 );
92 $timing->mark( 'mark_b' );
93 usleep( 100 );
94 $timing->mark( 'mark_c' );
95
96 $timing->measure( 'measure_a', 'mark_a', 'mark_b' );
97 $timing->measure( 'measure_b', 'mark_b', 'mark_c' );
98
99 $marks = array_map( function ( $entry ) {
100 return $entry['name'];
101 }, $timing->getEntriesByType( 'mark' ) );
102
103 $this->assertEquals( [ 'requestStart', 'mark_a', 'mark_b', 'mark_c' ], $marks );
104
105 $measures = array_map( function ( $entry ) {
106 return $entry['name'];
107 }, $timing->getEntriesByType( 'measure' ) );
108
109 $this->assertEquals( [ 'measure_a', 'measure_b' ], $measures );
110 }
111 }