Merge "Add timing instrumentation"

This commit is contained in:
jenkins-bot 2017-02-10 21:17:55 +00:00 committed by Gerrit Code Review
commit 3a802c6efb

View file

@ -51,12 +51,15 @@ class GenerateFancyCaptchas extends Maintenance {
); );
$this->addOption( "delete", "Delete the old captches" ); $this->addOption( "delete", "Delete the old captches" );
$this->mDescription = "Generate new captchas and move them into storage"; $this->mDescription = "Generate new captchas and move them into storage";
$this->requireExtension( "FancyCaptcha" ); $this->requireExtension( "FancyCaptcha" );
} }
public function execute() { public function execute() {
global $wgCaptchaSecret, $wgCaptchaDirectoryLevels; global $wgCaptchaSecret, $wgCaptchaDirectoryLevels;
$totalTime = -microtime( true );
$instance = ConfirmEditHooks::getInstance(); $instance = ConfirmEditHooks::getInstance();
if ( !( $instance instanceof FancyCaptcha ) ) { if ( !( $instance instanceof FancyCaptcha ) ) {
$this->error( "\$wgCaptchaClass is not FancyCaptcha.\n", 1 ); $this->error( "\$wgCaptchaClass is not FancyCaptcha.\n", 1 );
@ -84,7 +87,6 @@ class GenerateFancyCaptchas extends Maintenance {
$e = null; // exception $e = null; // exception
try { try {
$captchaScript = 'captcha.py'; $captchaScript = 'captcha.py';
if ( $this->hasOption( 'oldcaptcha' ) ) { if ( $this->hasOption( 'oldcaptcha' ) ) {
@ -104,25 +106,29 @@ class GenerateFancyCaptchas extends Maintenance {
} }
} }
$this->output( "Generating $countGen new captchas...\n" ); $this->output( "Generating $countGen new captchas.." );
$retVal = 1; $retVal = 1;
$captchaTime = -microtime( true );
wfShellExec( $cmd, $retVal, [], [ 'time' => 0 ] ); wfShellExec( $cmd, $retVal, [], [ 'time' => 0 ] );
if ( $retVal != 0 ) { if ( $retVal != 0 ) {
wfRecursiveRemoveDir( $tmpDir ); wfRecursiveRemoveDir( $tmpDir );
$this->error( "Could not run generation script.\n", 1 ); $this->error( "Could not run generation script.\n", 1 );
} }
$flags = FilesystemIterator::SKIP_DOTS; $captchaTime += microtime( true );
$iter = new RecursiveIteratorIterator( $this->output( " Done.\n" );
new RecursiveDirectoryIterator( $tmpDir, $flags ),
RecursiveIteratorIterator::CHILD_FIRST // include dirs
);
$this->output( "Done.\n" ); $this->output(
sprintf(
"\nGenerated %d captchas in %.1f seconds\n",
$countGen,
$captchaTime
)
);
$originalFiles = []; $originalFiles = [];
if ( $deleteOldCaptchas ) { if ( $deleteOldCaptchas ) {
$this->output( "Getting a list of old captchas...\n" ); $this->output( "Getting a list of old captchas..." );
foreach ( foreach (
$backend->getFileList( $backend->getFileList(
[ 'dir' => $backend->getRootStoragePath() . '/captcha-render' ] [ 'dir' => $backend->getRootStoragePath() . '/captcha-render' ]
@ -130,10 +136,21 @@ class GenerateFancyCaptchas extends Maintenance {
) { ) {
$originalFiles[] = $file; $originalFiles[] = $file;
} }
$this->output( "Done.\n" ); $this->output( " Done.\n" );
} }
$this->output( "Copying the new captchas to storage...\n" ); $this->output( "Copying the new captchas to storage..." );
$storeTime = -microtime( true );
$iter = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(
$tmpDir,
FilesystemIterator::SKIP_DOTS
),
RecursiveIteratorIterator::CHILD_FIRST // include dirs
);
$captchasGenerated = iterator_count( $iter );
foreach ( $iter as $fileInfo ) { foreach ( $iter as $fileInfo ) {
if ( !$fileInfo->isFile() ) { if ( !$fileInfo->isFile() ) {
continue; continue;
@ -149,24 +166,50 @@ class GenerateFancyCaptchas extends Maintenance {
$this->error( "Could not save file '{$fileInfo->getPathname()}'.\n" ); $this->error( "Could not save file '{$fileInfo->getPathname()}'.\n" );
} }
} }
$this->output( "Done.\n" ); $storeTime += microtime( true );
$this->output( " Done.\n" );
$this->output(
sprintf(
"\nCopied %d captchas to storage in %.1f seconds\n",
$captchasGenerated,
$storeTime
)
);
if ( $deleteOldCaptchas ) { if ( $deleteOldCaptchas ) {
$numOriginalFiles = count( $originalFiles ); $numOriginalFiles = count( $originalFiles );
$this->output( "Deleting {$numOriginalFiles} old captchas...\n" ); $this->output( "Deleting {$numOriginalFiles} old captchas...\n" );
$deleteTime = -microtime( true );
foreach ( $originalFiles as $file ) { foreach ( $originalFiles as $file ) {
$backend->quickDelete( [ 'src' => $file ] ); $backend->quickDelete( [ 'src' => $file ] );
} }
$deleteTime += microtime( true );
$this->output( "Done.\n" ); $this->output( "Done.\n" );
$this->output(
sprintf(
"\nDeleted %d old captchas in %.1f seconds\n",
count( $originalFiles ),
$deleteTime
)
);
} }
} catch ( Exception $e ) { } catch ( Exception $e ) {
wfRecursiveRemoveDir( $tmpDir ); wfRecursiveRemoveDir( $tmpDir );
throw $e; throw $e;
} }
$this->output( "Removing temporary files...\n" ); $this->output( "Removing temporary files..." );
wfRecursiveRemoveDir( $tmpDir ); wfRecursiveRemoveDir( $tmpDir );
$this->output( "Done.\n" ); $this->output( " Done.\n" );
$totalTime += microtime( true );
$this->output(
sprintf(
"\nWhole captchas generation process took %.1f seconds\n",
$totalTime
)
);
} }
} }