You are here

class Tester in Simple XML sitemap 4.x

Same name and namespace in other branches
  1. 8.3 tests/scripts/performance_test.php \Tester

Hierarchy

Expanded class hierarchy of Tester

File

tests/scripts/performance_test.php, line 46

View source
class Tester {

  /**
   * @var int
   */
  private $timerKey = 0;

  /**
   * @var \Psr\Log\LoggerInterface
   */
  private $logger;
  public function __construct(LoggerInterface $logger) {
    $this->logger = $logger;
  }
  use RandomGeneratorTrait;
  public function createNodeType() {
    if (NodeType::load('simple_sitemap_performance_test')) {
      return;
    }

    // Create the content type.
    $node_type = NodeType::create([
      'type' => 'simple_sitemap_performance_test',
      'name' => 'simple_sitemap_performance_test',
    ]);
    $node_type
      ->save();
    node_add_body_field($node_type);

    /** @var \Drupal\simple_sitemap\Manager\Generator $generator */
    $generator = \Drupal::service('simple_sitemap.generator');
    $generator
      ->entityManager()
      ->setBundleSettings('node', 'simple_sitemap_performance_test', [
      'index' => TRUE,
    ]);
  }
  public function createNode() {

    // Create a node.
    $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',
    ]);

    // Start drush batch process.
    batch_set($batch
      ->toArray());

    // See https://www.drupal.org/node/638712
    $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);

    // Remove the batch as Drush doesn't appear to properly clean up on success.
    $batch =& batch_get();
    $batch = NULL;
  }

  /**
   * @param bool $count_queries
   * @param $context
   * @throws \Drupal\Component\Plugin\Exception\PluginException
   */
  public static function doBatchGenerateSitemap($count_queries = FALSE, &$context) {
    if ($count_queries) {
      $query_logger = Database::startLog('simple_sitemap');
    }

    // Passes a special object in to $context that outputs every time
    // $context['message'] is set.
    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}";
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RandomGeneratorTrait::$randomGenerator protected property The random generator.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers. 1
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
RandomGeneratorTrait::randomStringValidate public function Callback for random string validation.
Tester::$logger private property
Tester::$timerKey private property
Tester::createNode public function
Tester::createNodeType public function
Tester::doBatchGenerateSitemap public static function
Tester::runGenerate public function
Tester::__construct public function