2015-11-03 02:26:07 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
* @author Ori Livneh <ori@wikimedia.org>
|
|
|
|
|
*/
|
|
|
|
|
|
2024-02-16 22:07:23 +00:00
|
|
|
namespace Wikimedia\Tests;
|
|
|
|
|
|
|
|
|
|
use MediaWikiCoversValidator;
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
use Timing;
|
|
|
|
|
|
objectcache,resourceloader,rdbms,jobqueue: Widen @covers annotations
Follows-up I4c7d826c7ec654b, I1287f3979aba1bf1.
We lose useful coverage and spend valuable time keeping these accurate
through refactors (or worse, forget to do so). The theoretically "bad"
accidental coverage is almost never actually bad.
Having said that, I'm not removing them wholesale (yet). I've audited
each of these specific files to confirm it is a general test of the
specified subject class, and also kept it limited to those specified
classes. That's imho more than 100% of the benefit for less than 1%
of the cost (more because `@covers` is more valuable than the fragile
and corrosive individual private method tracking in tests that
inevitably get out of date with no local incentive to keep them up to
date).
Cases like structure tests keep `@coversNothing` etc and we still don't
count coverage of other classes. There may be a handful of large
legacy classes where some methods are effectively class-like in
complexity and that's why it's good for PHPUnit to offer the precision
instrument but that doesn't meant we have to use that by-default for
everything.
I think best practice is to write good narrow unit tests, that reflect
how the code should be used in practice. Not to write bad tests and
hide part of its coverage within the same class or even namespace.
Fortunately, that's generally what we do already it's just that we
also kept these annotations still in many cases.
This wastes time to keep methods in sync, time to realize (and fix)
when other people inevitably didn't keep them in sync, time to find
uncovered code only to realize it is already covered, time for a less
experienced engineer to feel obligate to and do write a low quality
test to cover the "missing" branch in an unrealistic way, time wasted
in on-boarding by using such "bad" tests as example for how to use
the code and then having to unlearn it months/years later, loss of
telemetry in knowing what code actually isn't propertly tested due to
being masked by a bad test, and lost oppertunities to find actually
ununused/unreachable code and to think about how to instead structure
the code such that maybe that code can be removed.
------
Especially cases like LBFactoryTest.php were getting out of hand,
and in GlobalIdGeneratorTest.php we even resorted to reminding people
with inline comments to keep tags in sync.
Change-Id: I69b5385868cc6b451e5f2ebec9539694968bf58c
2023-04-10 21:25:34 +00:00
|
|
|
/**
|
2024-02-16 18:04:47 +00:00
|
|
|
* @covers \Timing
|
objectcache,resourceloader,rdbms,jobqueue: Widen @covers annotations
Follows-up I4c7d826c7ec654b, I1287f3979aba1bf1.
We lose useful coverage and spend valuable time keeping these accurate
through refactors (or worse, forget to do so). The theoretically "bad"
accidental coverage is almost never actually bad.
Having said that, I'm not removing them wholesale (yet). I've audited
each of these specific files to confirm it is a general test of the
specified subject class, and also kept it limited to those specified
classes. That's imho more than 100% of the benefit for less than 1%
of the cost (more because `@covers` is more valuable than the fragile
and corrosive individual private method tracking in tests that
inevitably get out of date with no local incentive to keep them up to
date).
Cases like structure tests keep `@coversNothing` etc and we still don't
count coverage of other classes. There may be a handful of large
legacy classes where some methods are effectively class-like in
complexity and that's why it's good for PHPUnit to offer the precision
instrument but that doesn't meant we have to use that by-default for
everything.
I think best practice is to write good narrow unit tests, that reflect
how the code should be used in practice. Not to write bad tests and
hide part of its coverage within the same class or even namespace.
Fortunately, that's generally what we do already it's just that we
also kept these annotations still in many cases.
This wastes time to keep methods in sync, time to realize (and fix)
when other people inevitably didn't keep them in sync, time to find
uncovered code only to realize it is already covered, time for a less
experienced engineer to feel obligate to and do write a low quality
test to cover the "missing" branch in an unrealistic way, time wasted
in on-boarding by using such "bad" tests as example for how to use
the code and then having to unlearn it months/years later, loss of
telemetry in knowing what code actually isn't propertly tested due to
being masked by a bad test, and lost oppertunities to find actually
ununused/unreachable code and to think about how to instead structure
the code such that maybe that code can be removed.
------
Especially cases like LBFactoryTest.php were getting out of hand,
and in GlobalIdGeneratorTest.php we even resorted to reminding people
with inline comments to keep tags in sync.
Change-Id: I69b5385868cc6b451e5f2ebec9539694968bf58c
2023-04-10 21:25:34 +00:00
|
|
|
*/
|
2024-02-16 22:07:23 +00:00
|
|
|
class TimingTest extends TestCase {
|
2015-11-03 02:26:07 +00:00
|
|
|
|
2017-12-29 23:22:37 +00:00
|
|
|
use MediaWikiCoversValidator;
|
|
|
|
|
|
2015-11-03 02:26:07 +00:00
|
|
|
public function testClearMarks() {
|
|
|
|
|
$timing = new Timing;
|
|
|
|
|
$this->assertCount( 1, $timing->getEntries() );
|
|
|
|
|
|
|
|
|
|
$timing->mark( 'a' );
|
|
|
|
|
$timing->mark( 'b' );
|
|
|
|
|
$this->assertCount( 3, $timing->getEntries() );
|
|
|
|
|
|
|
|
|
|
$timing->clearMarks( 'a' );
|
|
|
|
|
$this->assertNull( $timing->getEntryByName( 'a' ) );
|
|
|
|
|
$this->assertNotNull( $timing->getEntryByName( 'b' ) );
|
|
|
|
|
|
|
|
|
|
$timing->clearMarks();
|
|
|
|
|
$this->assertCount( 1, $timing->getEntries() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testMark() {
|
|
|
|
|
$timing = new Timing;
|
|
|
|
|
$timing->mark( 'a' );
|
|
|
|
|
|
|
|
|
|
$entry = $timing->getEntryByName( 'a' );
|
|
|
|
|
$this->assertEquals( 'a', $entry['name'] );
|
|
|
|
|
$this->assertEquals( 'mark', $entry['entryType'] );
|
|
|
|
|
$this->assertArrayHasKey( 'startTime', $entry );
|
2019-09-17 14:31:49 +00:00
|
|
|
$this->assertSame( 0, $entry['duration'] );
|
2015-11-03 02:26:07 +00:00
|
|
|
|
2016-02-18 21:06:04 +00:00
|
|
|
usleep( 100 );
|
2015-11-03 02:26:07 +00:00
|
|
|
$timing->mark( 'a' );
|
|
|
|
|
$newEntry = $timing->getEntryByName( 'a' );
|
|
|
|
|
$this->assertGreaterThan( $entry['startTime'], $newEntry['startTime'] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testMeasure() {
|
|
|
|
|
$timing = new Timing;
|
|
|
|
|
|
|
|
|
|
$timing->mark( 'a' );
|
2016-02-18 21:06:04 +00:00
|
|
|
usleep( 100 );
|
2015-11-03 02:26:07 +00:00
|
|
|
$timing->mark( 'b' );
|
2016-02-18 21:06:04 +00:00
|
|
|
|
|
|
|
|
$a = $timing->getEntryByName( 'a' );
|
2015-11-03 02:26:07 +00:00
|
|
|
$b = $timing->getEntryByName( 'b' );
|
|
|
|
|
|
|
|
|
|
$timing->measure( 'a_to_b', 'a', 'b' );
|
|
|
|
|
|
|
|
|
|
$entry = $timing->getEntryByName( 'a_to_b' );
|
|
|
|
|
$this->assertEquals( 'a_to_b', $entry['name'] );
|
|
|
|
|
$this->assertEquals( 'measure', $entry['entryType'] );
|
|
|
|
|
$this->assertEquals( $a['startTime'], $entry['startTime'] );
|
|
|
|
|
$this->assertEquals( $b['startTime'] - $a['startTime'], $entry['duration'] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testGetEntriesByType() {
|
|
|
|
|
$timing = new Timing;
|
|
|
|
|
|
|
|
|
|
$timing->mark( 'mark_a' );
|
|
|
|
|
usleep( 100 );
|
|
|
|
|
$timing->mark( 'mark_b' );
|
|
|
|
|
usleep( 100 );
|
|
|
|
|
$timing->mark( 'mark_c' );
|
|
|
|
|
|
|
|
|
|
$timing->measure( 'measure_a', 'mark_a', 'mark_b' );
|
|
|
|
|
$timing->measure( 'measure_b', 'mark_b', 'mark_c' );
|
|
|
|
|
|
2022-08-05 10:29:54 +00:00
|
|
|
$marks = array_column( $timing->getEntriesByType( 'mark' ), 'name' );
|
2015-11-03 02:26:07 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertEquals( [ 'requestStart', 'mark_a', 'mark_b', 'mark_c' ], $marks );
|
2015-11-03 02:26:07 +00:00
|
|
|
|
2022-08-05 10:29:54 +00:00
|
|
|
$measures = array_column( $timing->getEntriesByType( 'measure' ), 'name' );
|
2015-11-03 02:26:07 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertEquals( [ 'measure_a', 'measure_b' ], $measures );
|
2015-11-03 02:26:07 +00:00
|
|
|
}
|
|
|
|
|
}
|