performance_test.php in Simple XML sitemap 4.x
File
tests/scripts/performance_test.php
View source
<?php
use Drupal\Component\Utility\Timer;
use Drupal\Core\Batch\BatchBuilder;
use Drupal\Core\Database\Database;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\RandomGeneratorTrait;
use Drupal\simple_sitemap\Queue\BatchTrait;
use Drush\Drush;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Filesystem;
if (PHP_SAPI !== 'cli') {
exit;
}
if (!function_exists('drush_backend_batch_process')) {
throw new \RuntimeException("This script is designed to be run with drush scr");
}
include_once 'core/tests/Drupal/Tests/RandomGeneratorTrait.php';
$module_handler = \Drupal::moduleHandler();
$logger = Drush::service('logger');
if (!\Drupal::moduleHandler()
->moduleExists('simple_sitemap')) {
$logger
->error("In order to use this script, simple_sitemap must be installed.");
exit;
}
class Tester {
private $timerKey = 0;
private $logger;
public function __construct(LoggerInterface $logger) {
$this->logger = $logger;
}
use RandomGeneratorTrait;
public function createNodeType() {
if (NodeType::load('simple_sitemap_performance_test')) {
return;
}
$node_type = NodeType::create([
'type' => 'simple_sitemap_performance_test',
'name' => 'simple_sitemap_performance_test',
]);
$node_type
->save();
node_add_body_field($node_type);
$generator = \Drupal::service('simple_sitemap.generator');
$generator
->entityManager()
->setBundleSettings('node', 'simple_sitemap_performance_test', [
'index' => TRUE,
]);
}
public function createNode() {
$node = Node::create([
'type' => 'simple_sitemap_performance_test',
'title' => $this
->getRandomGenerator()
->sentences(5),
'body' => $this
->getRandomGenerator()
->sentences(20),
]);
$node
->save();
}
public function runGenerate($count_queries = FALSE) {
$batch = new BatchBuilder();
$relative_path_to_script = (new Filesystem())
->makePathRelative(__DIR__, \Drupal::root()) . basename(__FILE__);
$batch
->setFile($relative_path_to_script);
$batch
->addOperation(__CLASS__ . '::' . 'doBatchGenerateSitemap', [
$count_queries,
]);
$batch
->setFinishCallback([
BatchTrait::class,
'finishGeneration',
]);
batch_set($batch
->toArray());
$batch =& batch_get();
$batch['progressive'] = FALSE;
$timer = 'simple_sitemap:perf_test:' . $this->timerKey++;
Timer::start($timer);
drush_backend_batch_process();
$time = round(Timer::stop($timer)['time'] / 1000, 2) . ' seconds';
$this->logger
->info('Generation completed in: ' . $time);
$batch =& batch_get();
$batch = NULL;
}
public static function doBatchGenerateSitemap($count_queries = FALSE, &$context) {
if ($count_queries) {
$query_logger = Database::startLog('simple_sitemap');
}
BatchTrait::doBatchGenerateSitemap($context);
if ($count_queries) {
$context['message'] = "Query count: " . count($query_logger
->get('simple_sitemap'));
}
else {
$peak_mem = format_size(memory_get_peak_usage(TRUE));
$mem = format_size(memory_get_usage(TRUE));
$context['message'] = "Memory: {$peak_mem}, non-peak mem: {$mem}";
}
}
}
$batch =& batch_get();
if (!empty($batch['running'])) {
return;
}
$tester = new Tester($logger);
if (!Drush::output()
->isVerbose()) {
Drush::output()
->setVerbosity(OutputInterface::VERBOSITY_VERBOSE);
}
if (isset($extra[0]) && strtolower($extra[0]) === 'generate') {
$tester
->createNodeType();
$node_count = isset($extra[1]) ? (int) $extra[1] : 500;
for ($i = 0; $i < $node_count; $i++) {
$tester
->createNode();
}
$logger
->info("Created {$node_count} nodes for sitemap testing");
}
$logger
->info("Running memory usage tests:");
drupal_flush_all_caches();
$tester
->runGenerate();
$tester
->runGenerate();
$logger
->info("Running query count tests:");
drupal_flush_all_caches();
$tester
->runGenerate(TRUE);
$tester
->runGenerate(TRUE);