Update to 1.1

Read CHANGELOG.md#1.1.0
This commit is contained in:
bzick
2013-07-22 18:03:43 +04:00
parent a68a30bec5
commit c4610a7778
17 changed files with 760 additions and 277 deletions

View File

@ -1,5 +1,35 @@
<?php
require_once __DIR__.'/scripts/bootstrap.php';
$opt = getopt("h", array(
/** @var string $message */
"cleanup",
/** @var boolean $stress */
"stress:",
/** @var boolean $auto_reload */
"auto_reload",
/** @vat boolean $help */
/** @vat boolean $h */
"help"
));
$opt += array(
"stress" => 0
);
extract($opt);
if(isset($h) || isset($help)) {
echo "
Start: ".basename(__FILE__)." [--stress COUNT] [--auto_reload] [--cleanup]
Usage: ".basename(__FILE__)." [--help | -h]
";
exit;
}
Benchmark::$stress = intval($stress);
Benchmark::$auto_reload = isset($auto_reload);
exec("rm -rf ".__DIR__."/compile/*");
echo "Smarty3 vs Twig vs Fenom\n\n";
@ -17,7 +47,6 @@ Benchmark::runs("fenom", 'echo/smarty.tpl', __DIR__.'/templates/echo/data.js
//if(extension_loaded("phalcon")) {
// Benchmark::runs("volt", 'echo/twig.tpl', __DIR__.'/templates/echo/data.json');
//}
echo "\nTesting 'foreach' of big array...\n";
Benchmark::runs("smarty3", 'foreach/smarty.tpl', __DIR__.'/templates/foreach/data.json');
@ -36,11 +65,10 @@ Benchmark::runs("fenom", 'inheritance/smarty/b100.tpl', __DIR__.'/templates/for
// Benchmark::runs("volt", 'inheritance/twig/b100.tpl', __DIR__.'/templates/foreach/data.json');
//}
echo "\nDone. Cleanup.\n";
//passthru("rm -rf ".__DIR__."/compile/*");
passthru("rm -f ".__DIR__."/templates/inheritance/smarty/*");
passthru("rm -f ".__DIR__."/templates/inheritance/twig/*");
echo "\nSmarty3 vs Fenom (more details)\n\n";
echo "Coming soon\n";
echo "\nDone\n";
if(isset($cleanup)) {
echo "Cleanup.\n";
passthru("rm -rf ".__DIR__."/compile/*");
passthru("rm -f ".__DIR__."/templates/inheritance/smarty/*");
passthru("rm -f ".__DIR__."/templates/inheritance/twig/*");
}

View File

@ -0,0 +1,3 @@
<?php
require_once __DIR__.'/../../vendor/autoload.php';

View File

@ -3,11 +3,14 @@
require(__DIR__.'/../../vendor/autoload.php');
class Benchmark {
public static $t = "%8s: %-22s %10.4f sec, %10.1f MiB\n";
const OUTPUT = "%8s: %-22s %10.4f sec, %10.1f MiB\n";
public static function smarty3($tpl, $data, $double, $message) {
public static $stress = 0;
public static $auto_reload = false;
public static function smarty3($tpl, $data, $double, $stress = false, $auto_reload = false) {
$smarty = new Smarty();
$smarty->compile_check = false;
$smarty->compile_check = $auto_reload;
$smarty->setTemplateDir(__DIR__.'/../templates');
$smarty->setCompileDir(__DIR__."/../compile/");
@ -18,20 +21,28 @@ class Benchmark {
}
$start = microtime(true);
$smarty->assign($data);
$smarty->fetch($tpl);
if($stress) {
for($i=0; $i<$stress; $i++) {
$smarty->assign($data);
$smarty->fetch($tpl);
}
} else {
$smarty->assign($data);
$smarty->fetch($tpl);
}
return microtime(true) - $start;
printf(self::$t, __FUNCTION__, $message, round(microtime(true)-$start, 4), round(memory_get_peak_usage()/1024/1024, 2));
// printf(self::$t, __FUNCTION__, $message, round(microtime(true)-$start, 4), round(memory_get_peak_usage()/1024/1024, 2));
}
public static function twig($tpl, $data, $double, $message) {
public static function twig($tpl, $data, $double, $stress = false, $auto_reload = false) {
Twig_Autoloader::register();
$loader = new Twig_Loader_Filesystem(__DIR__.'/../templates');
$twig = new Twig_Environment($loader, array(
'cache' => __DIR__."/../compile/",
'autoescape' => false,
'auto_reload' => false,
'auto_reload' => $auto_reload,
));
if($double) {
@ -39,45 +50,57 @@ class Benchmark {
}
$start = microtime(true);
$twig->loadTemplate($tpl)->render($data);
printf(self::$t, __FUNCTION__, $message, round(microtime(true)-$start, 4), round(memory_get_peak_usage()/1024/1024, 2));
if($stress) {
for($i=0; $i<$stress; $i++) {
$twig->loadTemplate($tpl)->render($data);
}
} else {
$twig->loadTemplate($tpl)->render($data);
}
return microtime(true) - $start;
}
public static function fenom($tpl, $data, $double, $message) {
public static function fenom($tpl, $data, $double, $stress = false, $auto_reload = false) {
$fenom = Fenom::factory(__DIR__.'/../templates', __DIR__."/../compile");
if($auto_reload) {
$fenom->setOptions(Fenom::AUTO_RELOAD);
}
if($double) {
$fenom->fetch($tpl, $data);
}
$_SERVER["t"] = $start = microtime(true);
$fenom->fetch($tpl, $data);
printf(self::$t, __FUNCTION__, $message, round(microtime(true)-$start, 4), round(memory_get_peak_usage()/1024/1024, 2));
}
public static function volt($tpl, $data, $double, $message) {
$view = new \Phalcon\Mvc\View();
//$view->setViewsDir(__DIR__.'/../templates');
$volt = new \Phalcon\Mvc\View\Engine\Volt($view);
$volt->setOptions(array(
"compiledPath" => __DIR__.'/../compile',
"compiledExtension" => __DIR__."/../.compile"
));
if($double) {
$volt->render($tpl, $data);
}
$start = microtime(true);
var_dump($tpl);
$volt->render(__DIR__.'/../templates/'.$tpl, $data);
printf(self::$t, __FUNCTION__, $message, round(microtime(true)-$start, 4), round(memory_get_peak_usage()/1024/1024, 2));
if($stress) {
for($i=0; $i<$stress; $i++) {
$fenom->fetch($tpl, $data);
}
} else {
$fenom->fetch($tpl, $data);
}
return microtime(true) - $start;
}
// public static function volt($tpl, $data, $double, $message) {
// $view = new \Phalcon\Mvc\View();
// //$view->setViewsDir(__DIR__.'/../templates');
// $volt = new \Phalcon\Mvc\View\Engine\Volt($view);
//
//
// $volt->setOptions(array(
// "compiledPath" => __DIR__.'/../compile'
// ));
// $tpl = __DIR__.'/../templates/'.$tpl;
// if($double) {
// $volt->render($tpl, $data);
// }
//
// $start = microtime(true);
// $volt->render($tpl, $data);
// printf(self::$t, __FUNCTION__, $message, round(microtime(true)-$start, 4), round(memory_get_peak_usage()/1024/1024, 2));
// }
public static function run($engine, $template, $data, $double, $message) {
passthru(sprintf(PHP_BINARY." -dmemory_limit=512M -dxdebug.max_nesting_level=1024 %s/run.php --engine '%s' --template '%s' --data '%s' --message '%s' %s", __DIR__, $engine, $template, $data, $message, $double ? '--double' : ''));
passthru(sprintf(PHP_BINARY." -n -dextension=phalcon.so -ddate.timezone=Europe/Moscow -dmemory_limit=512M %s/run.php --engine '%s' --template '%s' --data '%s' --message '%s' %s --stress %d %s", __DIR__, $engine, $template, $data, $message, $double ? '--double' : '', self::$stress, self::$auto_reload ? '--auto_reload' : ''));
}
/**
@ -92,7 +115,3 @@ class Benchmark {
echo "\n";
}
}
function t() {
if(isset($_SERVER["t"])) var_dump(round(microtime(1) - $_SERVER["t"], 4));
}

View File

@ -1,15 +1,32 @@
<?php
$opt = getopt("", array(
/** @var string $engine */
"engine:",
/** @var string $template */
"template:",
/** @var string $data */
"data:",
/** @var boolean $double */
"double",
"message:"
/** @var string $message */
"message:",
/** @var boolean $stress */
"stress:",
/** @var boolean $auto_reload */
"auto_reload"
));
require_once __DIR__.'/bootstrap.php';
$opt += array(
"message" => "plain",
"stress" => 0,
);
extract($opt);
Benchmark::$engine($template, json_decode(file_get_contents($data), true), isset($double), $message);
$time = Benchmark::$engine($template, json_decode(file_get_contents($data), true), isset($double), $stress, isset($auto_reload));
printf(Benchmark::OUTPUT, $engine, $message, round($time, 4), round(memory_get_peak_usage()/1024/1024, 2));