Created
November 15, 2019 13:20
-
-
Save mariushoch/541d55e9e4fc2dabdfe539ec3ade47d0 to your computer and use it in GitHub Desktop.
Wikibase client "maintenance" script for benchmarking Lua serialiazation of entities
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Wikibase; | |
use Deserializers\DispatchingDeserializer; | |
use Language; | |
use Maintenance; | |
use Wikibase\Client\WikibaseClient; | |
use Wikibase\Client\DataAccess\Scribunto\EntityAccessor; | |
use Wikibase\Client\Usage\HashUsageAccumulator; | |
use Wikibase\DataModel\Entity\BasicEntityIdParser; | |
use Wikibase\DataModel\Services\Lookup\InMemoryEntityLookup; | |
use Wikibase\LanguageFallbackChain; | |
$basePath = getenv( 'MW_INSTALL_PATH' ) !== false | |
? getenv( 'MW_INSTALL_PATH' ) | |
: __DIR__ . '/../../../..'; | |
require_once $basePath . '/maintenance/Maintenance.php'; | |
/** | |
* @license GPL-2.0-or-later | |
* @author Marius Hoch | |
*/ | |
class BenchmarkLuaEntitySerialization extends Maintenance { | |
public function __construct() { | |
parent::__construct(); | |
} | |
/** | |
* @see Maintenance::execute | |
* | |
* @return bool | |
*/ | |
public function execute() { | |
$entityAccessor = $this->newEntityAccessor(); | |
// Run once before benchmarking | |
$entityAccessor->getEntity( 'Q42' ); | |
$t0 = microtime( true ); | |
$entityAccessor->getEntity( 'Q42' ); | |
$this->output( 'Took ' . ( microtime( true ) - $t0 ) . "s\n" ); | |
return true; | |
} | |
private function newEntityAccessor(): EntityAccessor { | |
$wikibaseClient = WikibaseClient::getDefaultInstance(); | |
$settings = $wikibaseClient->getSettings(); | |
return new EntityAccessor( | |
new BasicEntityIdParser(), | |
$this->getEntityLookup(), | |
new HashUsageAccumulator(), | |
$wikibaseClient->getCompactEntitySerializer(), | |
$wikibaseClient->getCompactBaseDataModelSerializerFactory()->newStatementListSerializer(), | |
$wikibaseClient->getPropertyDataTypeLookup(), | |
new LanguageFallbackChain( [] ), | |
Language::factory( 'en' ), | |
$wikibaseClient->getTermsLanguages(), | |
$settings->getSetting( 'fineGrainedLuaTracking' ) | |
); | |
} | |
private function getAllTypesEntityDeserializer(): DispatchingDeserializer { | |
$wikibaseClient = WikibaseClient::getDefaultInstance(); | |
$deserializerFactoryCallbacks = $wikibaseClient->getEntityDeserializerFactoryCallbacks(); | |
$baseDeserializerFactory = $wikibaseClient->getBaseDataModelDeserializerFactory(); | |
$deserializers = []; | |
foreach ( $deserializerFactoryCallbacks as $callback ) { | |
$deserializers[] = call_user_func( $callback, $baseDeserializerFactory ); | |
} | |
return new DispatchingDeserializer( $deserializers ); | |
} | |
private function getEntityLookup(): InMemoryEntityLookup { | |
$serialization = json_decode( | |
file_get_contents( 'https://www.wikidata.org/wiki/Special:EntityData/Q42.json' ), | |
true | |
)['entities']['Q42']; | |
$wikibaseClient = WikibaseClient::getDefaultInstance(); | |
$testItem = $this->getAllTypesEntityDeserializer()->deserialize( $serialization ); | |
return new InMemoryEntityLookup( $testItem ); | |
} | |
} | |
$maintClass = BenchmarkLuaEntitySerialization::class; | |
require_once RUN_MAINTENANCE_IF_MAIN; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment