-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
197 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
] | ||
] | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |