Debian Codegraph @

tests/phpunit/unit/includes/libs/TimingTest.php

raw symbols SCIP
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 <[email protected]>
20 */
21
22 namespace Wikimedia\Tests;
23
24 use MediaWikiCoversValidator;
25 use PHPUnit\Framework\TestCase;
26 use Timing;
27
28 /**
29 * @covers \Timing
30 */
31 class TimingTest extends TestCase {
32
33 use MediaWikiCoversValidator;
34
35 public function testClearMarks() {
36 $timing = new Timing;
37 $this->assertCount( 1, $timing->getEntries() );
38
39 $timing->mark( 'a' );
40 $timing->mark( 'b' );
41 $this->assertCount( 3, $timing->getEntries() );
42
43 $timing->clearMarks( 'a' );
44 $this->assertNull( $timing->getEntryByName( 'a' ) );
45 $this->assertNotNull( $timing->getEntryByName( 'b' ) );
46
47 $timing->clearMarks();
48 $this->assertCount( 1, $timing->getEntries() );
49 }
50
51 public function testMark() {
52 $timing = new Timing;
53 $timing->mark( 'a' );
54
55 $entry = $timing->getEntryByName( 'a' );
56 $this->assertEquals( 'a', $entry['name'] );
57 $this->assertEquals( 'mark', $entry['entryType'] );
58 $this->assertArrayHasKey( 'startTime', $entry );
59 $this->assertSame( 0, $entry['duration'] );
60
61 usleep( 100 );
62 $timing->mark( 'a' );
63 $newEntry = $timing->getEntryByName( 'a' );
64 $this->assertGreaterThan( $entry['startTime'], $newEntry['startTime'] );
65 }
66
67 public function testMeasure() {
68 $timing = new Timing;
69
70 $timing->mark( 'a' );
71 usleep( 100 );
72 $timing->mark( 'b' );
73
74 $a = $timing->getEntryByName( 'a' );
75 $b = $timing->getEntryByName( 'b' );
76
77 $timing->measure( 'a_to_b', 'a', 'b' );
78
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'] );
84 }
85
86 public function testGetEntriesByType() {
87 $timing = new Timing;
88
89 $timing->mark( 'mark_a' );
90 usleep( 100 );
91 $timing->mark( 'mark_b' );
92 usleep( 100 );
93 $timing->mark( 'mark_c' );
94
95 $timing->measure( 'measure_a', 'mark_a', 'mark_b' );
96 $timing->measure( 'measure_b', 'mark_b', 'mark_c' );
97
98 $marks = array_column( $timing->getEntriesByType( 'mark' ), 'name' );
99
100 $this->assertEquals( [ 'requestStart', 'mark_a', 'mark_b', 'mark_c' ], $marks );
101
102 $measures = array_column( $timing->getEntriesByType( 'measure' ), 'name' );
103
104 $this->assertEquals( [ 'measure_a', 'measure_b' ], $measures );
105 }
106 }