Skip to content

Commit

Permalink
Add benchmark utility
Browse files Browse the repository at this point in the history
  • Loading branch information
laszlovl committed May 1, 2015
1 parent ffb9645 commit 96f3934
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 0 deletions.
132 changes: 132 additions & 0 deletions benchmark/commands/BenchmarkController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php

namespace app\commands;

use app\models\Animal;
use app\models\StaticAnimal;
use yii\db\ActiveRecord;
use Yii;

class BenchmarkController extends \yii\console\Controller
{
const REPEAT = 10;

public function actionIndex()
{
$this->createDatabase();

$this->warm();

return $this->benchmarks();
}

private function benchmarks()
{
$this->benchmark('benchmarkGetProperty');

$this->benchmark('benchmarkGetRelation');

$this->benchmark('benchmarkSetProperty');

$this->benchmark('benchmarkValidate');
}

private function benchmark($function)
{
$regular = Animal::class;
$static = StaticAnimal::class;

$callable = [$this, $function];

$regularDuration = $this->runBenchmark($callable, [$regular]);
$staticDuration = $this->runBenchmark($callable, [$static]);

$improvement = self::improvement($regularDuration, $staticDuration);

$this->stdout("Benchmarking $function...\n"
. "Regular ActiveRecord: $regularDuration\n"
. "Static ActiveRecord: $staticDuration\n"
. "Improvement: $improvement\n\n");
}

private function runBenchmark(callable $callable, array $args)
{
$durations = [];

for ($i = 0; $i < self::REPEAT; $i++) {
$start = microtime(true);

call_user_func_array($callable, $args);

$durations[] = microtime(true) - $start;
}

$duration = array_sum($durations) / count($durations);

return $duration;
}

private function benchmarkGetProperty($class)
{
foreach ($class::find()->all() as $model) {
$temp = $model->name;
}
}

private function benchmarkGetRelation($class)
{
foreach ($class::find()->with('self')->all() as $model) {
$temp = $model->self;
}
}

private function benchmarkSetProperty($class)
{
foreach ($class::find()->all() as $model) {
$model->name = 'testing';
}
}

private function benchmarkValidate($class)
{
foreach ($class::find()->all() as $model) {
$model->validate();
}
}

private static function improvement($old, $new)
{
return -(floor(($new - $old) / $old * 100)) . '%';
}

private function warm()
{
Animal::find()->one()->name;
StaticAnimal::find()->one()->name;
}

private function createDatabase()
{
$db = Yii::$app->db;

if ($db->getTableSchema('animal') === null) {
$db->createCommand()->createTable('animal', [
'id' => 'pk',
'name' => 'string'
])->execute();

// sqlite: "too many terms in compound SELECT"
for ($i = 0; $i < 2; $i++) {
$rows = [];
for ($j = 0; $j < 500; $j++) {
$rows[] = ['test'];
}

$db->createCommand()->batchInsert('animal', ['name'], $rows)->execute();
}

// yii's internal schema caching; this has nothing to do with our changes
$db->getTableSchema('animal', true);
}
}
}
13 changes: 13 additions & 0 deletions benchmark/config/console.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
return [
'id' => 'staticactiverecord-benchmark',
'basePath' => dirname(__DIR__),
'controllerNamespace' => 'app\commands',
'components' => [
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'sqlite:data/benchmark.db',
'charset' => 'utf8',
]
]
];
8 changes: 8 additions & 0 deletions benchmark/models/Animal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace app\models;

class Animal extends \yii\db\ActiveRecord
{
use AnimalTrait;
}
22 changes: 22 additions & 0 deletions benchmark/models/AnimalTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace app\models;

trait AnimalTrait {
public static function tableName()
{
return 'animal';
}

public function rules()
{
return [
['name', 'string']
];
}

public function getSelf()
{
return $this->hasOne(static::class, ['id' => 'id']);
}
}
8 changes: 8 additions & 0 deletions benchmark/models/StaticAnimal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace app\models;

class StaticAnimal extends \lvl\staticactiverecord\db\ActiveRecord
{
use AnimalTrait;
}
14 changes: 14 additions & 0 deletions benchmark/yii
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env php
<?php
// fcgi doesn't have STDIN and STDOUT defined by default
defined('STDIN') or define('STDIN', fopen('php://stdin', 'r'));
defined('STDOUT') or define('STDOUT', fopen('php://stdout', 'w'));

require(__DIR__ . '/../vendor/autoload.php');
require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');

$config = require(__DIR__ . '/config/console.php');

$application = new yii\console\Application($config);
$exitCode = $application->run();
exit($exitCode);

0 comments on commit 96f3934

Please sign in to comment.