phpbench: tests for LinkRecommendationFilter and Special:Homepage
This patch adds a phpbench (https://github.com/phpbench/phpbench) test for the link recommendation filter and Special:Homepage rendering. To set up, run "composer update" in the GrowthExperiments directory. Then from the GrowthExperiments directory, run: vendor/bin/phpbench run tests/Benchmark --revs=10 Bug: T291549 Change-Id: I8f1807f1d43aaecdaab49913e3b7c03f8c6a160f
这个提交包含在:
父节点
f51b40d210
当前提交
5e39f7cc82
|
@ -4,7 +4,8 @@
|
|||
"mediawiki/mediawiki-phan-config": "0.11.0",
|
||||
"mediawiki/minus-x": "1.1.1",
|
||||
"php-parallel-lint/php-console-highlighter": "0.5.0",
|
||||
"php-parallel-lint/php-parallel-lint": "1.3.1"
|
||||
"php-parallel-lint/php-parallel-lint": "1.3.1",
|
||||
"phpbench/phpbench": "^1.1"
|
||||
},
|
||||
"scripts": {
|
||||
"fix": [
|
||||
|
@ -16,6 +17,7 @@
|
|||
"@phpcs",
|
||||
"minus-x check ."
|
||||
],
|
||||
"phpbench": "phpbench run tests/Benchmark --revs=20",
|
||||
"phan": "phan -d . --long-progress-bar",
|
||||
"phpcs": "phpcs -s -p --cache"
|
||||
}
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
{
|
||||
"$schema": "./vendor/phpbench/phpbench/phpbench.schema.json",
|
||||
"runner.bootstrap": "./../../tests/phpunit/bootstrap.php"
|
||||
}
|
|
@ -0,0 +1,93 @@
|
|||
<?php
|
||||
|
||||
namespace GrowthExperiments\Tests\Benchmark;
|
||||
|
||||
use CommentStoreComment;
|
||||
use GrowthExperiments\GrowthExperimentsServices;
|
||||
use GrowthExperiments\NewcomerTasks\AddLink\LinkRecommendation;
|
||||
use GrowthExperiments\NewcomerTasks\AddLink\LinkRecommendationMetadata;
|
||||
use GrowthExperiments\NewcomerTasks\LinkRecommendationFilter;
|
||||
use GrowthExperiments\NewcomerTasks\Task\Task;
|
||||
use GrowthExperiments\NewcomerTasks\Task\TaskSet;
|
||||
use GrowthExperiments\NewcomerTasks\Task\TaskSetFilters;
|
||||
use GrowthExperiments\NewcomerTasks\TaskSuggester\SearchTaskSuggester;
|
||||
use GrowthExperiments\NewcomerTasks\TaskSuggester\StaticTaskSuggesterFactory;
|
||||
use GrowthExperiments\NewcomerTasks\TaskSuggester\TaskSuggester;
|
||||
use GrowthExperiments\NewcomerTasks\TaskType\LinkRecommendationTaskType;
|
||||
use GrowthExperiments\NewcomerTasks\TaskType\TaskType;
|
||||
use MediaWiki\MediaWikiServices;
|
||||
use MediaWiki\Revision\SlotRecord;
|
||||
use MediaWiki\User\UserIdentityValue;
|
||||
use StatusValue;
|
||||
use TextContent;
|
||||
use TitleValue;
|
||||
use User;
|
||||
|
||||
class GrowthExperimentsBench {
|
||||
|
||||
/** @var TaskSuggester */
|
||||
private $taskSuggester;
|
||||
/** @var LinkRecommendationFilter */
|
||||
protected $linkRecommendationFilter;
|
||||
/** @var TaskSet|StatusValue */
|
||||
protected $tasks;
|
||||
|
||||
public function setUpLinkRecommendation() {
|
||||
$tasks = [];
|
||||
$services = MediaWikiServices::getInstance();
|
||||
$linkRecommendationTaskType = new LinkRecommendationTaskType(
|
||||
'link-recommendation', TaskType::DIFFICULTY_EASY, []
|
||||
);
|
||||
$wikiPageFactory = $services->getWikiPageFactory();
|
||||
$growthServices = GrowthExperimentsServices::wrap( $services );
|
||||
$linkRecommendationStore = $growthServices->getLinkRecommendationStore();
|
||||
$user = User::newSystemUser( 'phpbench' );
|
||||
for ( $i = 0; $i < SearchTaskSuggester::DEFAULT_LIMIT; $i++ ) {
|
||||
$linkTarget = new TitleValue( NS_MAIN, "$i phpbench" );
|
||||
$page = $wikiPageFactory->newFromLinkTarget( $linkTarget );
|
||||
$pageUpdater = $page->newPageUpdater( $user );
|
||||
$pageUpdater->setContent( SlotRecord::MAIN, new TextContent( 'phpbench' ) );
|
||||
$revisionRecord = $pageUpdater->saveRevision( CommentStoreComment::newUnsavedComment( 'phpbench' ) );
|
||||
$tasks[] = new Task(
|
||||
$linkRecommendationTaskType,
|
||||
$linkTarget
|
||||
);
|
||||
$linkRecommendation = new LinkRecommendation(
|
||||
$linkTarget,
|
||||
$page->getId(),
|
||||
$revisionRecord ? $revisionRecord->getId() : $page->getRevisionRecord()->getId(),
|
||||
[],
|
||||
new LinkRecommendationMetadata( 1, 2, [], 0 )
|
||||
);
|
||||
$linkRecommendationStore->insert( $linkRecommendation );
|
||||
}
|
||||
$taskSuggesterFactory = new StaticTaskSuggesterFactory(
|
||||
$tasks,
|
||||
$services->getTitleFactory()
|
||||
);
|
||||
$this->taskSuggester = $taskSuggesterFactory->create();
|
||||
$this->linkRecommendationFilter = $growthServices->getLinkRecommendationFilter();
|
||||
// Pre-warm cache.
|
||||
$this->tasks = $this->taskSuggester->suggest(
|
||||
new UserIdentityValue( 1, 'Admin' ),
|
||||
new TaskSetFilters( [ 'link-recommendation' ] )
|
||||
);
|
||||
}
|
||||
|
||||
public function tearDownLinkRecommendation() {
|
||||
$services = MediaWikiServices::getInstance();
|
||||
$linkRecommendationStore = GrowthExperimentsServices::wrap( $services )->getLinkRecommendationStore();
|
||||
$wikiPageFactory = $services->getWikiPageFactory();
|
||||
$deletePageFactory = $services->getDeletePageFactory();
|
||||
$phpbenchUser = User::newSystemUser( 'phpbench' );
|
||||
foreach ( $this->tasks as $task ) {
|
||||
$linkRecommendationStore->deleteByLinkTarget( $task->getTitle() );
|
||||
$page = $wikiPageFactory->newFromLinkTarget( $task->getTitle() );
|
||||
$deletePage = $deletePageFactory->newDeletePage(
|
||||
$page,
|
||||
$phpbenchUser
|
||||
);
|
||||
$deletePage->deleteUnsafe( 'phpbench' );
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
namespace GrowthExperiments\Tests\Benchmark;
|
||||
|
||||
$path = dirname( __DIR__, 4 );
|
||||
|
||||
if ( getenv( 'MW_INSTALL_PATH' ) !== false ) {
|
||||
$path = getenv( 'MW_INSTALL_PATH' );
|
||||
}
|
||||
|
||||
require_once $path . '/includes/Setup.php';
|
||||
|
||||
require_once "GrowthExperimentsBench.php";
|
||||
|
||||
class LinkRecommendationFilterBench extends GrowthExperimentsBench {
|
||||
|
||||
// phpcs:disable MediaWiki.Commenting.FunctionAnnotations.UnrecognizedAnnotation
|
||||
|
||||
/**
|
||||
* @BeforeMethods ("setUpLinkRecommendation")
|
||||
* @AfterMethods ("tearDownLinkRecommendation")
|
||||
* @Assert("mode(variant.time.avg) < 100000 microseconds +/- 10%")
|
||||
*/
|
||||
public function benchFilter() {
|
||||
$this->linkRecommendationFilter->filter( $this->tasks );
|
||||
}
|
||||
|
||||
// phpcs:enable MediaWiki.Commenting.FunctionAnnotations.UnrecognizedAnnotation
|
||||
}
|
|
@ -0,0 +1,63 @@
|
|||
<?php
|
||||
|
||||
namespace GrowthExperiments\Tests\Benchmark;
|
||||
|
||||
use GlobalVarConfig;
|
||||
use GrowthExperiments\GrowthExperimentsServices;
|
||||
use GrowthExperiments\Specials\SpecialHomepage;
|
||||
use MediaWiki\MediaWikiServices;
|
||||
use SpecialPage;
|
||||
|
||||
$path = dirname( __DIR__, 4 );
|
||||
|
||||
if ( getenv( 'MW_INSTALL_PATH' ) !== false ) {
|
||||
$path = getenv( 'MW_INSTALL_PATH' );
|
||||
}
|
||||
|
||||
require_once $path . '/includes/Setup.php';
|
||||
|
||||
require_once "GrowthExperimentsBench.php";
|
||||
|
||||
class SpecialHomepageBench extends GrowthExperimentsBench {
|
||||
|
||||
/** @var SpecialHomepage */
|
||||
private $homepage;
|
||||
|
||||
public function setUp() {
|
||||
$services = MediaWikiServices::getInstance();
|
||||
$growthExperimentsServices = GrowthExperimentsServices::wrap( $services );
|
||||
$userOptionManager = $services->getUserOptionsManager();
|
||||
$this->homepage = new SpecialHomepage(
|
||||
$growthExperimentsServices->getHomepageModuleRegistry(),
|
||||
$services->getStatsdDataFactory(),
|
||||
$services->getPerDbNameStatsdDataFactory(),
|
||||
$growthExperimentsServices->getExperimentUserManager(),
|
||||
// This would normally be wiki-powered config, but
|
||||
// there is no need to test this
|
||||
GlobalVarConfig::newInstance(),
|
||||
$userOptionManager,
|
||||
$services->getTitleFactory()
|
||||
);
|
||||
$context = new \DerivativeContext( \RequestContext::getMain() );
|
||||
$testUser = $services->getUserFactory()->newFromId( 1 );
|
||||
$context->setUser( $testUser );
|
||||
$userOptionManager->setOption( $testUser, 'growthexperiments-homepage-enable', 1 );
|
||||
$userOptionManager->saveOptions( $testUser );
|
||||
// Needed for StartEmail.php and other code that may look for the context title.
|
||||
$context->setTitle( SpecialPage::getTitleFor( 'Homepage' ) );
|
||||
$this->homepage->setContext( $context );
|
||||
}
|
||||
|
||||
// phpcs:disable MediaWiki.Commenting.FunctionAnnotations.UnrecognizedAnnotation
|
||||
|
||||
/**
|
||||
* @BeforeMethods ("setUp", "setUpLinkRecommendation" )
|
||||
* @AfterMethods ("tearDownLinkRecommendation" )
|
||||
* @Assert("mode(variant.time.avg) < 700000 microseconds +/- 10%")
|
||||
*/
|
||||
public function benchExecute() {
|
||||
$this->homepage->execute();
|
||||
}
|
||||
|
||||
// phpcs:enable MediaWiki.Commenting.FunctionAnnotations.UnrecognizedAnnotation
|
||||
}
|
正在加载...
在新工单中引用