Merge "Update docs/hooks.txt for ShowSearchHitTitle"
[mediawiki.git] / tests / phpunit / includes / libs / TimingTest.php
blob4d71944004cdc97d483acd4094752ca8b1f3d55b
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.
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.
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
18 * @file
19 * @author Ori Livneh <ori@wikimedia.org>
22 class TimingTest extends PHPUnit_Framework_TestCase {
24 /**
25 * @covers Timing::clearMarks
26 * @covers Timing::getEntries
28 public function testClearMarks() {
29 $timing = new Timing;
30 $this->assertCount( 1, $timing->getEntries() );
32 $timing->mark( 'a' );
33 $timing->mark( 'b' );
34 $this->assertCount( 3, $timing->getEntries() );
36 $timing->clearMarks( 'a' );
37 $this->assertNull( $timing->getEntryByName( 'a' ) );
38 $this->assertNotNull( $timing->getEntryByName( 'b' ) );
40 $timing->clearMarks();
41 $this->assertCount( 1, $timing->getEntries() );
44 /**
45 * @covers Timing::mark
46 * @covers Timing::getEntryByName
48 public function testMark() {
49 $timing = new Timing;
50 $timing->mark( 'a' );
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'] );
58 usleep( 100 );
59 $timing->mark( 'a' );
60 $newEntry = $timing->getEntryByName( 'a' );
61 $this->assertGreaterThan( $entry['startTime'], $newEntry['startTime'] );
64 /**
65 * @covers Timing::measure
67 public function testMeasure() {
68 $timing = new Timing;
70 $timing->mark( 'a' );
71 usleep( 100 );
72 $timing->mark( 'b' );
74 $a = $timing->getEntryByName( 'a' );
75 $b = $timing->getEntryByName( 'b' );
77 $timing->measure( 'a_to_b', 'a', 'b' );
79 $entry = $timing->getEntryByName( 'a_to_b' );
80 $this->assertEquals( 'a_to_b', $entry['name'] );
81 $this->assertEquals( 'measure', $entry['entryType'] );
82 $this->assertEquals( $a['startTime'], $entry['startTime'] );
83 $this->assertEquals( $b['startTime'] - $a['startTime'], $entry['duration'] );
86 /**
87 * @covers Timing::getEntriesByType
89 public function testGetEntriesByType() {
90 $timing = new Timing;
92 $timing->mark( 'mark_a' );
93 usleep( 100 );
94 $timing->mark( 'mark_b' );
95 usleep( 100 );
96 $timing->mark( 'mark_c' );
98 $timing->measure( 'measure_a', 'mark_a', 'mark_b' );
99 $timing->measure( 'measure_b', 'mark_b', 'mark_c' );
101 $marks = array_map( function ( $entry ) {
102 return $entry['name'];
103 }, $timing->getEntriesByType( 'mark' ) );
105 $this->assertEquals( [ 'requestStart', 'mark_a', 'mark_b', 'mark_c' ], $marks );
107 $measures = array_map( function ( $entry ) {
108 return $entry['name'];
109 }, $timing->getEntriesByType( 'measure' ) );
111 $this->assertEquals( [ 'measure_a', 'measure_b' ], $measures );