Merge "Make FilterProfiler independent of DeferredUpdate"

This commit is contained in:
jenkins-bot 2021-02-28 13:18:51 +00:00 committed by Gerrit Code Review
commit ef4a5c4115
3 changed files with 34 additions and 35 deletions

View file

@ -3,7 +3,6 @@
namespace MediaWiki\Extension\AbuseFilter;
use BagOStuff;
use DeferredUpdates;
use IBufferingStatsdDataFactory;
use MediaWiki\Config\ServiceOptions;
use Psr\Log\LoggerInterface;
@ -242,35 +241,32 @@ class FilterProfiler {
* @phan-param array<string,array{time:float,conds:int,result:bool}> $data
*/
public function recordPerFilterProfiling( Title $title, array $data ) : void {
// Defer profiling updates to avoid massive (~1 second) edit time increases
DeferredUpdates::addCallableUpdate( function () use ( $title, $data ) {
$slowFilterThreshold = $this->options->get( 'AbuseFilterSlowFilterRuntimeLimit' );
$slowFilterThreshold = $this->options->get( 'AbuseFilterSlowFilterRuntimeLimit' );
foreach ( $data as $filterName => $params ) {
list( $filterID, $global ) = GlobalNameUtils::splitGlobalName( $filterName );
// @todo Maybe add a parameter to recordProfilingResult to record global filters
// data separately (in the foreign wiki)
if ( !$global ) {
$this->recordProfilingResult(
$filterID,
$params['time'],
$params['conds'],
$params['result']
);
}
if ( $params['time'] > $slowFilterThreshold ) {
$this->recordSlowFilter(
$title,
$filterName,
$params['time'],
$params['conds'],
$params['result'],
$global
);
}
foreach ( $data as $filterName => $params ) {
list( $filterID, $global ) = GlobalNameUtils::splitGlobalName( $filterName );
// @todo Maybe add a parameter to recordProfilingResult to record global filters
// data separately (in the foreign wiki)
if ( !$global ) {
$this->recordProfilingResult(
$filterID,
$params['time'],
$params['conds'],
$params['result']
);
}
} );
if ( $params['time'] > $slowFilterThreshold ) {
$this->recordSlowFilter(
$title,
$filterName,
$params['time'],
$params['conds'],
$params['result'],
$global
);
}
}
}
/**

View file

@ -231,12 +231,13 @@ class FilterRunner {
$runnerData = $this->checkAllFiltersInternal();
}
$this->profileExecution( $runnerData );
// hack until DI for DeferredUpdates is possible (T265749)
if ( defined( 'MW_PHPUNIT_TEST' ) ) {
$this->profileExecution( $runnerData );
$this->updateEmergencyCache( $runnerData->getMatchesMap() );
} else {
DeferredUpdates::addCallableUpdate( function () use ( $runnerData ) {
$this->profileExecution( $runnerData );
$this->updateEmergencyCache( $runnerData->getMatchesMap() );
} );
}

View file

@ -1,16 +1,22 @@
<?php
namespace MediaWiki\Extension\AbuseFilter\Tests\Unit;
use HashBagOStuff;
use IBufferingStatsdDataFactory;
use MediaWiki\Config\ServiceOptions;
use MediaWiki\Extension\AbuseFilter\FilterProfiler;
use MediaWikiUnitTestCase;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use TestLogger;
use Title;
/**
* @coversDefaultClass \MediaWiki\Extension\AbuseFilter\FilterProfiler
* @covers ::__construct
* @fixme convert to pure unit test when DI for DeferredUpdates is possible (T265749)
*/
class AbuseFilterFilterProfilerTest extends MediaWikiIntegrationTestCase {
class FilterProfilerTest extends MediaWikiUnitTestCase {
private const NULL_FILTER_PROFILE = [
'count' => 0,
@ -70,7 +76,6 @@ class AbuseFilterFilterProfilerTest extends MediaWikiIntegrationTestCase {
],
]
);
DeferredUpdates::doUpdates();
$this->assertSame(
[
'count' => 1,
@ -109,7 +114,6 @@ class AbuseFilterFilterProfilerTest extends MediaWikiIntegrationTestCase {
],
]
);
DeferredUpdates::doUpdates();
$this->assertSame(
[
'count' => 2,
@ -181,7 +185,6 @@ class AbuseFilterFilterProfilerTest extends MediaWikiIntegrationTestCase {
],
]
);
DeferredUpdates::doUpdates();
$profiler->resetFilterProfile( 1 );
$this->assertSame( self::NULL_FILTER_PROFILE, $profiler->getFilterProfile( 1 ) );
$this->assertNotSame( self::NULL_FILTER_PROFILE, $profiler->getFilterProfile( 2 ) );
@ -294,7 +297,6 @@ class AbuseFilterFilterProfilerTest extends MediaWikiIntegrationTestCase {
],
]
);
DeferredUpdates::doUpdates();
$profiler->recordStats( 'default', 100, 256.5, true );
$profiler->recordStats( 'default', 200, 512.5, false );