Skip to content

Instantly share code, notes, and snippets.

@mdmunir
Created November 24, 2016 15:49
Show Gist options
  • Save mdmunir/8a4b312511c6878d5ae16cb93c4bbedd to your computer and use it in GitHub Desktop.
Save mdmunir/8a4b312511c6878d5ae16cb93c4bbedd to your computer and use it in GitHub Desktop.
<?php
namespace App\Console\Commands;
use Yii;
use Illuminate\Console\Command;
use App\models\larva\User as LarvaUser;
use app\models\yii\User as YiiUser;
class TestORM extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'test:orm';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
// init
$this->init();
// create record
list($lt, $yt) = $this->testCreate();
$this->line("Test create record:");
$this->line("Laravel: $lt");
$this->line("Yii: $yt");
$this->line('--------------');
// create record
list($lt, $yt) = $this->testRead();
$this->line("Test read record:");
$this->line("Laravel: $lt");
$this->line("Yii: $yt");
}
protected function init()
{
$db = new \yii\db\Connection([
'dsn' => 'sqlite:' . database_path('database.sqlite'),
]);
$command = $db->createCommand();
if ($db->getTableSchema('user')) {
$command->dropTable('user')->execute();
}
$command->createTable('user', [
'id' => 'pk',
'name' => 'string(32)',
'email' => 'string(64)'
])->execute();
// Yii
$command = \Yii::$app->db->createCommand();
if (Yii::$app->db->getTableSchema('user')) {
$command->dropTable('user')->execute();
}
$command->createTable('user', [
'id' => 'pk',
'name' => 'string(32)',
'email' => 'string(64)'
])->execute();
}
protected function testCreate()
{
$lt = $yt = 0.0;
for ($i = 0; $i < 100; $i++) {
$name = 'name' . $i;
$email = "email{$i}@gmail.com";
$t = microtime(true);
$user = new LarvaUser();
$user->name = $name;
$user->email = $email;
$user->save();
$lt += (microtime(true) - $t);
// yii
$t = microtime(true);
$user = new YiiUser();
$user->name = $name;
$user->email = $email;
$user->save(false);
$yt += (microtime(true) - $t);
}
return[$lt, $yt];
}
protected function testRead()
{
$lt = $yt = 0.0;
for ($i = 0; $i < 500; $i++) {
for ($id = 1; $id <= 10; $id++) {
$t = microtime(true);
$user = LarvaUser::find($id);
$user->name;
$lt += (microtime(true) - $t);
// yii
$t = microtime(true);
$user = YiiUser::findOne($id);
$user->name;
$yt += (microtime(true) - $t);
}
}
return[$lt, $yt];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment