Skip to content

Instantly share code, notes, and snippets.

@SamMousa
Last active September 12, 2015 12:47
Show Gist options
  • Save SamMousa/30b2ad1d7931faf4398e to your computer and use it in GitHub Desktop.
Save SamMousa/30b2ad1d7931faf4398e to your computer and use it in GitHub Desktop.
<?php
use \yiiunit\data\ar\Customer;
// This enables calling the benchmark from a symlink.
chdir(dirname(($_SERVER['SCRIPT_FILENAME'][0] == '/' ? '': getcwd() . '/') . $_SERVER['SCRIPT_FILENAME']));
include dirname(($_SERVER['SCRIPT_FILENAME'][0] == '/' ? '': getcwd() . '/') . $_SERVER['SCRIPT_FILENAME']) . '/tests/bootstrap.php';
$test = new \yiiunit\framework\db\ActiveRecordTest();
$config = $test->getParam('databases')['mysql'];
unset($config['fixture']);
$config['class'] = \yii\db\Connection::class;
$db = \Yii::createObject($config);
Customer::$db = $db;
$tests = [
'Setting attributes (magic)' => [
'repeat' => 500,
'inner' => 500,
'param' => function() { return new Customer(); },
'test' => function($inner, Customer $model) {
for ($i = 0; $i < $inner; $i++) {
$model->email = 'test@test.com';
}
},
'dummy' => function($model) {}
],
'Setting attributes (setAttribute)' => [
'repeat' => 500,
'inner' => 500,
'param' => function() { return new Customer(); },
'test' => function($inner, Customer $model){
for ($i = 0; $i < $inner; $i++) {
$model->setAttribute('email', 'test@test.com');
}
},
'dummy' => function($model) {}
],
'Getting attributes (magic)' => [
'repeat' => 500,
'inner' => 500,
'param' => function() { return new Customer(); },
'test' => function($inner, Customer $model) {
for ($i = 0; $i < $inner; $i++) {
// Tests get and set.
$model->email;
}
},
'dummy' => function($model) {}
],
'Getting attributes (getAttribute)' => [
'repeat' => 500,
'inner' => 500,
'param' => function() { return new Customer(); },
'test' => function($inner, Customer $model) {
for ($i = 0; $i < $inner; $i++) {
// Tests get and set.
$model->getAttribute('email');
}
},
'dummy' => function($model) {}
],
'Deleting all customer records.' => [
'repeat' => 1,
'inner' => 0,
'param' => function() { Customer::deleteAll(); },
'test' => function() {},
'dummy' => function($model) {}
],
'Creating & saving one by one. (magic)' => [
'repeat' => 100,
'inner' => 50,
'param' => function() { return null; },
'test' => function($inner) {
for ($i = 0; $i < $inner; $i++) {
$customer = new Customer();
$customer->email = 'test@test.com';
$customer->name = 'bob';
$customer->address = 'Some place';
$customer->status = 15;
$customer->save(false);
}
},
'dummy' => function($model) {}
],
'Creating & saving one by one. (setAttribute)' => [
'repeat' => 100,
'inner' => 50,
'param' => function() { return null; },
'test' => function($inner) {
for ($i = 0; $i < $inner; $i++) {
$customer = new Customer();
$customer->setAttribute('email', 'test@test.com');
$customer->setAttribute('name', 'bob');
$customer->setAttribute('address', 'Some place');
$customer->setAttribute('status', 15);
$customer->save(false);
}
},
'dummy' => function($model) {}
],
'Reading 1000 records.' => [
'repeat' => 20,
'inner' => 5,
'param' => function() { return null; },
'test' => function($inner) {
for ($i = 0; $i < $inner; $i++) {
Customer::find()->limit(1000)->all();
}
},
'dummy' => function($model) {}
],
"Reading all atributes (magic)" => [
'repeat' => 500,
'inner' => 500,
'param' => function() { return Customer::find()->one(); },
'test' => function($inner, $customer) {
for ($i = 0; $i < $inner; $i++) {
$customer->email;
$customer->name;
$customer->address;
$customer->status;
$customer->profile_id;
}
},
'dummy' => function($model) {}
],
"Reading all atributes (getAttribute)" => [
'repeat' => 500,
'inner' => 500,
'param' => function() { return Customer::find()->one(); },
'test' => function($inner, $customer) {
for ($i = 0; $i < $inner; $i++) {
$customer->getAttribute('email');
$customer->getAttribute('name');
$customer->getAttribute('address');
$customer->getAttribute('status');
$customer->getAttribute('profile_id');
}
},
'dummy' => function($model) {}
]
];
$times =[];
foreach ($tests as $title => $test) {
echo "\nExecuting $title...\n";
for($i = 0; $i < $test['repeat']; $i++) {
$param = $test['param']();
$start = microtime(true);
$test['test']($test['inner'], $param);
$end = microtime(true);
// Compensate for closure call time.
$test['dummy']($param);
$dummy = microtime(true) - $end;
$times[$title][] = $end - $start - $dummy;
echo '.';
}
}
$widths = [50, 8, 8, 8, 8, 8, 8, 8];
function printRow(array $columns, array $widths, $pad = ' ') {
$pads = array_fill(0, count($columns), $pad);
$widths = array_slice($widths, 0, count($columns));
echo '| ' . implode(' | ', array_map('str_pad', $columns, $widths, $pads)) . " |\n";
}
\yii\helpers\Console::output();
passthru('git branch | grep \*');
printRow([
"Test",
"Total",
"Min",
"Max",
"Avg",
"# Runs",
"# Loops"
], $widths);
printRow(array_fill(0, 7, ''), $widths, '-');
foreach($times as $key => $runs) {
printRow([
$key,
number_format(array_sum($runs), 4),
number_format(min($runs), 4),
number_format(max($runs), 4),
number_format(array_sum($runs) / count($runs), 4),
$tests[$key]['repeat'],
$tests[$key]['inner']
], $widths);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment