Merge "rdbms: avoid LoadBalancer::getConnection waste when using $groups"
[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 use MediaWikiCoversValidator;
25
26 /**
27 * @covers Timing::clearMarks
28 * @covers Timing::getEntries
29 */
30 public function testClearMarks() {
31 $timing = new Timing;
32 $this->assertCount( 1, $timing->getEntries() );
33
34 $timing->mark( 'a' );
35 $timing->mark( 'b' );
36 $this->assertCount( 3, $timing->getEntries() );
37
38 $timing->clearMarks( 'a' );
39 $this->assertNull( $timing->getEntryByName( 'a' ) );
40 $this->assertNotNull( $timing->getEntryByName( 'b' ) );
41
42 $timing->clearMarks();
43 $this->assertCount( 1, $timing->getEntries() );
44 }
45
46 /**
47 * @covers Timing::mark
48 * @covers Timing::getEntryByName
49 */
50 public function testMark() {
51 $timing = new Timing;
52 $timing->mark( 'a' );
53
54 $entry = $timing->getEntryByName( 'a' );
55 $this->assertEquals( 'a', $entry['name'] );
56 $this->assertEquals( 'mark', $entry['entryType'] );
57 $this->assertArrayHasKey( 'startTime', $entry );
58 $this->assertEquals( 0, $entry['duration'] );
59
60 usleep( 100 );
61 $timing->mark( 'a' );
62 $newEntry = $timing->getEntryByName( 'a' );
63 $this->assertGreaterThan( $entry['startTime'], $newEntry['startTime'] );
64 }
65
66 /**
67 * @covers Timing::measure
68 */
69 public function testMeasure() {
70 $timing = new Timing;
71
72 $timing->mark( 'a' );
73 usleep( 100 );
74 $timing->mark( 'b' );
75
76 $a = $timing->getEntryByName( 'a' );
77 $b = $timing->getEntryByName( 'b' );
78
79 $timing->measure( 'a_to_b', 'a', 'b' );
80
81 $entry = $timing->getEntryByName( 'a_to_b' );
82 $this->assertEquals( 'a_to_b', $entry['name'] );
83 $this->assertEquals( 'measure', $entry['entryType'] );
84 $this->assertEquals( $a['startTime'], $entry['startTime'] );
85 $this->assertEquals( $b['startTime'] - $a['startTime'], $entry['duration'] );
86 }
87
88 /**
89 * @covers Timing::getEntriesByType
90 */
91 public function testGetEntriesByType() {
92 $timing = new Timing;
93
94 $timing->mark( 'mark_a' );
95 usleep( 100 );
96 $timing->mark( 'mark_b' );
97 usleep( 100 );
98 $timing->mark( 'mark_c' );
99
100 $timing->measure( 'measure_a', 'mark_a', 'mark_b' );
101 $timing->measure( 'measure_b', 'mark_b', 'mark_c' );
102
103 $marks = array_map( function ( $entry ) {
104 return $entry['name'];
105 }, $timing->getEntriesByType( 'mark' ) );
106
107 $this->assertEquals( [ 'requestStart', 'mark_a', 'mark_b', 'mark_c' ], $marks );
108
109 $measures = array_map( function ( $entry ) {
110 return $entry['name'];
111 }, $timing->getEntriesByType( 'measure' ) );
112
113 $this->assertEquals( [ 'measure_a', 'measure_b' ], $measures );
114 }
115 }