Skip to content

Instantly share code, notes, and snippets.

@timmillwood
Last active April 14, 2016 08:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timmillwood/ed75cbf4b066db8777bd7aa333d2db5a to your computer and use it in GitHub Desktop.
Save timmillwood/ed75cbf4b066db8777bd7aa333d2db5a to your computer and use it in GitHub Desktop.
Hacky revision performance test
Array
(
[node] => Array
(
[revs] => Array
(
[create] => 23.061083078384
[load] => 0.065523862838745
)
[norevs] => Array
(
[create] => 23.849312067032
[load] => 0.070587158203125
)
)
[block] => Array
(
[revs] => Array
(
[create] => 19.585115909576
[load] => 0.07262110710144
)
[norevs] => Array
(
[create] => 19.11677479744
[load] => 0.11151790618896
)
)
)
Time: 1.45 minutes, Memory: 6.00Mb
<?php
/**
* Created by PhpStorm.
* User: timmillwood
* Date: 14/04/16
* Time: 07:30
*/
namespace Drupal\KernelTests;
use Drupal\block_content\Entity\BlockContentType;
use Drupal\node\Entity\NodeType;
class RevisionPerformanceTest extends KernelTestBase {
protected $strictConfigSchema = FALSE;
public static $modules = ['system', 'user', 'node', 'block_content'];
protected $title;
public function testRevisionPerformance() {
$this->installSchema('node', ['node_access']);
$this->installEntitySchema('user');
$this->installEntitySchema('node');
$this->installEntitySchema('block_content');
NodeType::create(['type' => 'test', 'label' => 'test'])->save();
BlockContentType::create(['id' => 'test', 'label' => 'test'])->save();
$this->title = $this->randomMachineName();
$output['node']['revs'] = $this->generate('node', true);
$output['node']['norevs'] = $this->generate('node', false);
$output['block']['revs'] = $this->generate('block_content', true);
$output['block']['norevs'] = $this->generate('block_content', false);
fwrite(STDOUT, print_r($output, true));
}
protected function generate($entity_type, $revs) {
$storage = \Drupal::entityTypeManager()->getStorage($entity_type);
$create_pre = microtime(true);
for ($i = 0; $i < 50; $i++) {
$this->createEntity($storage, $revs, $i);
}
$create_post = microtime(true);
$create_time = $create_post - $create_pre;
$load_pre = microtime(true);
for ($i = 0; $i < 50; $i++) {
$this->loadEntity($storage, $revs, $i);
}
$load_post = microtime(true);
$load_time = $load_post - $load_pre;
return ['create' => $create_time, 'load' => $load_time];
}
protected function createEntity($storage, $revs, $i) {
$field = $storage->getEntityTypeId() == 'node' ? 'title' : 'info';
$title = $this->title . (string) $revs . $i;
$entity = $storage->create(['type' => 'test', $field => $title]);
$entity->save();
for ($i = 0; $i < 50; $i++) {
$entity = $this->updateEntity($entity, $field, $revs, $i);
}
}
protected function updateEntity($entity, $field, $revs, $i) {
$title = $entity->get($field)->value . $i;
$entity->setNewRevision($revs);
$entity->set($field, $title);
$entity->save();
return $entity;
}
protected function loadEntity($storage, $revs, $i) {
$field = $storage->getEntityTypeId() == 'node' ? 'title' : 'info';
$title = $this->title . (string) $revs . $i . '012345678910111213141516171819202122232425262728293031323334353637383940414243444546474849';
$entities = $storage->loadByProperties([$field => $title]);
return reset($entities);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment