add stress test in benchmark to measure complete cycle (init + rendering)

This commit is contained in:
klkvsk 2013-07-09 14:56:10 +04:00
parent 3583a2cdfd
commit ab9fa6bf24
3 changed files with 78 additions and 30 deletions

View File

@ -36,6 +36,12 @@ Benchmark::runs("fenom", 'inheritance/smarty/b100.tpl', __DIR__.'/templates/for
// Benchmark::runs("volt", 'inheritance/twig/b100.tpl', __DIR__.'/templates/foreach/data.json'); // Benchmark::runs("volt", 'inheritance/twig/b100.tpl', __DIR__.'/templates/foreach/data.json');
//} //}
echo "\nTesting " . Benchmark::STRESS_REQUEST_COUNT ." separate renderings...\n";
Benchmark::stress("smarty3", 'foreach/smarty.tpl', __DIR__.'/templates/foreach/data.json', Benchmark::STRESS_REQUEST_COUNT);
// twig takes minutes, so not comparable
//Benchmark::stress("twig", 'foreach/twig.tpl', __DIR__.'/templates/foreach/data.json', Benchmark::STRESS_REQUEST_COUNT);
Benchmark::stress("fenom", 'foreach/smarty.tpl', __DIR__.'/templates/foreach/data.json', Benchmark::STRESS_REQUEST_COUNT);
echo "\nDone. Cleanup.\n"; echo "\nDone. Cleanup.\n";
//passthru("rm -rf ".__DIR__."/compile/*"); //passthru("rm -rf ".__DIR__."/compile/*");
passthru("rm -f ".__DIR__."/templates/inheritance/smarty/*"); passthru("rm -f ".__DIR__."/templates/inheritance/smarty/*");

View File

@ -3,9 +3,18 @@
require(__DIR__.'/../../vendor/autoload.php'); require(__DIR__.'/../../vendor/autoload.php');
class Benchmark { 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) { const STRESS_REQUEST_COUNT = 1000;
const STRESS_FENOM_REINIT = true;
const STRESS_TWIG_REINIT = true;
/** @var Fenom */
protected static $_fenom;
/** @var Twig_Environment */
protected static $_twig;
public static function smarty3($tpl, $data, $double) {
$smarty = new Smarty(); $smarty = new Smarty();
$smarty->compile_check = false; $smarty->compile_check = false;
@ -21,18 +30,21 @@ class Benchmark {
$smarty->assign($data); $smarty->assign($data);
$smarty->fetch($tpl); $smarty->fetch($tpl);
printf(self::$t, __FUNCTION__, $message, round(microtime(true)-$start, 4), round(memory_get_peak_usage()/1024/1024, 2)); return microtime(true)-$start;
} }
public static function twig($tpl, $data, $double, $message) { public static function twig($tpl, $data, $double) {
if (self::STRESS_TWIG_REINIT || !self::$_twig) {
Twig_Autoloader::register();
$loader = new Twig_Loader_Filesystem(__DIR__.'/../templates');
self::$_twig = new Twig_Environment($loader, array(
'cache' => __DIR__."/../compile/",
'autoescape' => false,
'auto_reload' => false,
));
}
Twig_Autoloader::register(); $twig = self::$_twig;
$loader = new Twig_Loader_Filesystem(__DIR__.'/../templates');
$twig = new Twig_Environment($loader, array(
'cache' => __DIR__."/../compile/",
'autoescape' => false,
'auto_reload' => false,
));
if($double) { if($double) {
$twig->loadTemplate($tpl)->render($data); $twig->loadTemplate($tpl)->render($data);
@ -40,22 +52,25 @@ class Benchmark {
$start = microtime(true); $start = microtime(true);
$twig->loadTemplate($tpl)->render($data); $twig->loadTemplate($tpl)->render($data);
printf(self::$t, __FUNCTION__, $message, round(microtime(true)-$start, 4), round(memory_get_peak_usage()/1024/1024, 2)); return microtime(true)-$start;
} }
public static function fenom($tpl, $data, $double, $message) { public static function fenom($tpl, $data, $double) {
if (self::STRESS_FENOM_REINIT || !self::$_fenom) {
$fenom = Fenom::factory(__DIR__.'/../templates', __DIR__."/../compile"); self::$_fenom = Fenom::factory(__DIR__.'/../templates', __DIR__."/../compile");
}
$fenom = self::$_fenom;
$fenom->setOptions(Fenom::AUTO_RELOAD);
if($double) { if($double) {
$fenom->fetch($tpl, $data); $fenom->fetch($tpl, $data);
} }
$_SERVER["t"] = $start = microtime(true); $start = microtime(true);
$fenom->fetch($tpl, $data); $fenom->fetch($tpl, $data);
printf(self::$t, __FUNCTION__, $message, round(microtime(true)-$start, 4), round(memory_get_peak_usage()/1024/1024, 2));
return microtime(true)-$start;
} }
public static function volt($tpl, $data, $double, $message) { public static function volt($tpl, $data, $double) {
$view = new \Phalcon\Mvc\View(); $view = new \Phalcon\Mvc\View();
//$view->setViewsDir(__DIR__.'/../templates'); //$view->setViewsDir(__DIR__.'/../templates');
$volt = new \Phalcon\Mvc\View\Engine\Volt($view); $volt = new \Phalcon\Mvc\View\Engine\Volt($view);
@ -71,9 +86,8 @@ class Benchmark {
} }
$start = microtime(true); $start = microtime(true);
var_dump($tpl);
$volt->render(__DIR__.'/../templates/'.$tpl, $data); $volt->render(__DIR__.'/../templates/'.$tpl, $data);
printf(self::$t, __FUNCTION__, $message, round(microtime(true)-$start, 4), round(memory_get_peak_usage()/1024/1024, 2)); return microtime(true)-$start;
} }
public static function run($engine, $template, $data, $double, $message) { public static function run($engine, $template, $data, $double, $message) {
@ -91,8 +105,13 @@ class Benchmark {
self::run($engine, $template, $data, true, ' compiled and loaded'); self::run($engine, $template, $data, true, ' compiled and loaded');
echo "\n"; echo "\n";
} }
}
function t() { public static function stress($engine, $template, $data) {
if(isset($_SERVER["t"])) var_dump(round(microtime(1) - $_SERVER["t"], 4)); passthru(
sprintf(
PHP_BINARY." -dmemory_limit=2048M -dxdebug.max_nesting_level=1024 %s/run.php --engine '%s' --template '%s' --data '%s' --stress",
__DIR__, $engine, $template, $data
)
);
}
} }

View File

@ -1,15 +1,38 @@
<?php <?php
$opt = getopt("", array( $opt = getopt("", array(
"engine:", /** @var string $engine */
"template:", "engine:",
"data:", /** @var string $template */
"double", "template:",
"message:" /** @var string $data */
"data:",
/** @var boolean $double */
"double",
/** @var boolean $stress */
"stress",
/** @var string $message */
"message:"
)); ));
require_once __DIR__.'/bootstrap.php'; require_once __DIR__.'/bootstrap.php';
extract($opt); extract($opt);
Benchmark::$engine($template, json_decode(file_get_contents($data), true), isset($double), $message); if (isset($stress)) {
$start = microtime(true);
$message = 'stress test';
$data = json_decode(file_get_contents($data), true);
gc_enable();
for ($i = 0; $i < Benchmark::STRESS_REQUEST_COUNT; $i++) {
Benchmark::$engine($template, $data, false);
if ($i % 50 == 0) gc_collect_cycles();
}
$time = microtime(true) - $start;
} else {
$time = Benchmark::$engine($template, json_decode(file_get_contents($data), true), isset($double));
}
printf(Benchmark::OUTPUT, $engine, $message, round($time, 4), round(memory_get_peak_usage()/1024/1024, 2));