Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Wikibase client "maintenance" script for benchmarking Lua serialiazation of entities
<?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