/**
  * Database query benchmarks.
  *
  * @return  void
  */
 public function benchmarks()
 {
     $benchmarks = Benchmark::get(TRUE);
     // Moves the first benchmark (total execution time) to the end of the array
     $benchmarks = array_slice($benchmarks, 1) + array_slice($benchmarks, 0, 1);
     $table = array();
     $table[] = array('Benchmark', 'Time', 'Memory');
     foreach ($benchmarks as $name => $benchmark) {
         // Clean unique id from system benchmark names
         $name = ucwords(str_replace(array('_', '-'), ' ', str_replace(SYSTEM_BENCHMARK . '_', '', $name)));
         $table[] = array($name, number_format($benchmark['time'], 3), number_format($benchmark['memory'] / 1024 / 1024, 2) . 'MB');
     }
     $this->firephp->fb(array(count($benchmarks) . ' benchmarks took ' . number_format($benchmark['time'], 3) . ' seconds and used up ' . number_format($benchmark['memory'] / 1024 / 1024, 2) . 'MB' . ' memory', $table), FirePHP::TABLE);
 }
Example #2
0
 /**
  * Inserts global Kohana variables into the generated output and prints it.
  *
  * @param   string  final output that will displayed
  * @return  void
  */
 public static function render($output)
 {
     // Fetch memory usage in MB
     $memory = function_exists('memory_get_usage') ? memory_get_usage() / 1024 / 1024 : 0;
     // Fetch benchmark for page execution time
     $benchmark = Benchmark::get(SYSTEM_BENCHMARK . '_total_execution');
     if (self::config('core.render_stats') === TRUE) {
         // Replace the global template variables
         $output = str_replace(array('{kohana_version}', '{kohana_codename}', '{execution_time}', '{memory_usage}', '{included_files}'), array(KOHANA_VERSION, KOHANA_CODENAME, $benchmark['time'], number_format($memory, 2) . 'MB', count(get_included_files())), $output);
     }
     if ($level = self::config('core.output_compression') and ini_get('output_handler') !== 'ob_gzhandler' and (int) ini_get('zlib.output_compression') === 0) {
         if ($level < 1 or $level > 9) {
             // Normalize the level to be an integer between 1 and 9. This
             // step must be done to prevent gzencode from triggering an error
             $level = max(1, min($level, 9));
         }
         if (stripos(@$_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE) {
             $compress = 'gzip';
         } elseif (stripos(@$_SERVER['HTTP_ACCEPT_ENCODING'], 'deflate') !== FALSE) {
             $compress = 'deflate';
         }
     }
     if (isset($compress) and $level > 0) {
         switch ($compress) {
             case 'gzip':
                 // Compress output using gzip
                 $output = gzencode($output, $level);
                 break;
             case 'deflate':
                 // Compress output using zlib (HTTP deflate)
                 $output = gzdeflate($output, $level);
                 break;
         }
         // This header must be sent with compressed content to prevent
         // browser caches from breaking
         header('Vary: Accept-Encoding');
         // Send the content encoding header
         header('Content-Encoding: ' . $compress);
         // Sending Content-Length in CGI can result in unexpected behavior
         if (stripos(PHP_SAPI, 'cgi') === FALSE) {
             header('Content-Length: ' . strlen($output));
         }
     }
     echo $output;
 }
Example #3
0
 /**
  * Saves the benchmark results to a database
  * @todo Use the database libraries for this instead of platform-specific DB calls
  *
  * @return void
  */
 public static function save_results()
 {
     // Ignore all of these actions if we have benchmarking disabled
     if (Kohana::config('benchmark.enable') === FALSE) {
         return FALSE;
     }
     // Connect to the benchmark database
     $db = Kohana::config('benchmark.db');
     $link = mysql_connect($db['host'], $db['user'], $db['pass']) or die('Could not connect to benchmark database.');
     mysql_select_db($db['database']) or die('Could not select benchmark database.');
     $table = mysql_real_escape_string($db['table_prefix']) . 'benchmark';
     $benchmark_results = Benchmark::get(TRUE);
     foreach ($benchmark_results as $name => $data) {
         // Don't save the generic system benchmark results
         if (strstr($name, 'system_benchmark_') === FALSE) {
             $query = 'INSERT INTO ' . $table . ' (`name`, `time`, `memory`) VALUES (\'' . mysql_real_escape_string($name) . '\', \'' . mysql_real_escape_string($data['time']) . '\', \'' . mysql_real_escape_string($data['memory']) . '\');';
             // Execute the query
             mysql_query($query, $link);
         }
     }
     // Close the connection to the Benchmar DB
     mysql_close($link);
 }
Example #4
0
 /**
  * Benchmark times and memory usage from the Benchmark library.
  *
  * @return  void
  */
 public function benchmarks()
 {
     if (!($table = $this->table('benchmarks'))) {
         return;
     }
     $table->add_column();
     $table->add_column('kp-column kp-data');
     $table->add_column('kp-column kp-data');
     $table->add_row(array('Benchmarks', 'Time', 'Memory'), 'kp-title', 'background-color: #FFE0E0');
     $benchmarks = Benchmark::get(TRUE);
     // Moves the first benchmark (total execution time) to the end of the array
     $benchmarks = array_slice($benchmarks, 1) + array_slice($benchmarks, 0, 1);
     text::alternate();
     foreach ($benchmarks as $name => $benchmark) {
         // Clean unique id from system benchmark names
         $name = ucwords(str_replace(array('_', '-'), ' ', str_replace(SYSTEM_BENCHMARK . '_', '', $name)));
         $data = array($name, number_format($benchmark['time'], 3), number_format($benchmark['memory'] / 1024 / 1024, 2) . 'MB');
         $class = text::alternate('', 'kp-altrow');
         if ($name == 'Total Execution') {
             $class = 'kp-totalrow';
         }
         $table->add_row($data, $class);
     }
 }
Example #5
0
 /**
  * Benchmark times and memory usage from the Benchmark library.
  *
  * @return  void
  */
 public static function benchmarks()
 {
     if (!Profiler::show('benchmarks')) {
         return;
     }
     $table = new Profiler_Table();
     $table->add_column();
     $table->add_column('kp-column kp-data');
     $table->add_column('kp-column kp-data');
     $table->add_column('kp-column kp-data');
     $table->add_row(array(__('Benchmarks'), __('Count'), __('Time'), __('Memory')), 'kp-title', 'background-color: #FFE0E0');
     $benchmarks = Benchmark::get(TRUE);
     // Moves the first benchmark (total execution time) to the end of the array
     $benchmarks = array_slice($benchmarks, 1) + array_slice($benchmarks, 0, 1);
     text::alternate();
     foreach ($benchmarks as $name => $benchmark) {
         // Clean unique id from system benchmark names
         $name = ucwords(str_replace(array('_', '-'), ' ', str_replace(SYSTEM_BENCHMARK . '_', '', $name)));
         $data = array(__($name), $benchmark['count'], number_format($benchmark['time'], Kohana::config('profiler.time_decimals')), number_format($benchmark['memory'] / 1024 / 1024, Kohana::config('profiler.memory_decimals')) . 'MB');
         $class = text::alternate('', 'kp-altrow');
         if ($name == 'Total Execution') {
             // Clear the count column
             $data[1] = '';
             $class = 'kp-totalrow';
         }
         $table->add_row($data, $class);
     }
     Profiler::add($table);
 }
Example #6
0
 /**
  * Parse the CSS
  *
  * @return string - The processes css file as a string
  * @author Anthony Short
  **/
 public static function parse_css()
 {
     # If the cache is stale or doesn't exist
     if (self::config('core.cache.mod_time') <= self::config('core.request.mod_time')) {
         # Start the timer
         Benchmark::start("parse_css");
         # Compress it before parsing
         CSS::compress(CSS::$css);
         # Import CSS files
         Import::parse();
         if (self::config('core.auto_include_mixins') === true) {
             # Import the mixins in the plugin/module folders
             Mixins::import_mixins('framework/mixins');
         }
         # Parse our css through the plugins
         foreach (self::$plugins as $plugin) {
             call_user_func(array($plugin, 'import_process'));
         }
         # Compress it before parsing
         CSS::compress(CSS::$css);
         # Parse the constants
         Constants::parse();
         foreach (self::$plugins as $plugin) {
             call_user_func(array($plugin, 'pre_process'));
         }
         # Parse the @grid
         Layout::parse_grid();
         # Replace the constants
         Constants::replace();
         # Parse @for loops
         Iteration::parse();
         foreach (self::$plugins as $plugin) {
             call_user_func(array($plugin, 'process'));
         }
         # Compress it before parsing
         CSS::compress(CSS::$css);
         # Parse the mixins
         Mixins::parse();
         # Find missing constants
         Constants::replace();
         # Compress it before parsing
         CSS::compress(CSS::$css);
         foreach (self::$plugins as $plugin) {
             call_user_func(array($plugin, 'post_process'));
         }
         # Parse the expressions
         Expression::parse();
         # Parse the nested selectors
         NestedSelectors::parse();
         # Convert all url()'s to absolute paths if required
         if (self::config('core.absolute_urls') === true) {
             CSS::convert_to_absolute_urls();
         }
         # Replaces url()'s that start with ~ to lead to the CSS directory
         CSS::replace_css_urls();
         # Add the extra string we've been storing
         CSS::$css .= CSS::$append;
         # If they want to minify it
         if (self::config('core.minify_css') === true) {
             Minify::compress();
         } else {
             CSS::pretty();
         }
         # Formatting hook
         foreach (self::$plugins as $plugin) {
             call_user_func(array($plugin, 'formatting_process'));
         }
         # Validate the CSS
         if (self::config('core.validate') === true) {
             Validate::check();
         }
         # Stop the timer...
         Benchmark::stop("parse_css");
         if (self::config('core.show_header') === TRUE) {
             CSS::$css = "/* Processed by CSScaffold on " . gmdate('r') . " in " . Benchmark::get("parse_css", "time") . " seconds */\n\n" . CSS::$css;
         }
         # Write the css file to the cache
         self::cache_write(CSS::$css);
         # Output process hook for plugins to display views.
         # Doesn't run in production mode.
         if (!IN_PRODUCTION) {
             foreach (array_merge(self::$plugins, self::$modules) as $plugin) {
                 call_user_func(array($plugin, 'output'));
             }
         }
     }
 }
Example #7
0
 public static function benchmarks()
 {
     $benchmarks = array();
     foreach (Benchmark::get(true) as $name => $benchmark) {
         $benchmarks[$name] = array('name' => ucwords(str_replace(array('_', '-'), ' ', str_replace(SYSTEM_BENCHMARK . '_', '', $name))), 'time' => $benchmark['time'], 'memory' => $benchmark['memory']);
     }
     $benchmarks = array_slice($benchmarks, 1) + array_slice($benchmarks, 0, 1);
     return $benchmarks;
 }
Example #8
0
File: t.php Project: bgads/adsbg
    for ($i = 0; $i < 5000; $i++) {
        $r = mt_rand(0, 2);
        $db->query(sprintf($sql, $records[$r]->title, $records[$r]->contents));
    }
}
if (isset($_SERVER['argv'][1]) and $_SERVER['argv'][1] == 'fill') {
    echo 'Generating test data.' . PHP_EOL;
    for ($i = 0; $i < 10; $i++) {
        fill_db();
        echo ($i + 1) * 5000 . ' records generated.' . PHP_EOL;
    }
    exit;
}
Benchmark::start('select records');
$records = $db->query('SELECT id, title, contents FROM posts')->fetchAll(PDO::FETCH_OBJ);
Benchmark::stop('select records');
Benchmark::start('index');
$data = array('title' => 'Craiglist ads', 'records' => $records);
$index = parse('index.tpl', $data);
create('index', $index);
Benchmark::stop('index');
Benchmark::start('pages');
foreach ($records as $page) {
    $data = array('page' => $page);
    $content = parse('page.tpl', $data);
    create($page->id, $content);
}
Benchmark::stop('pages');
Benchmark::stop('page');
var_dump(Benchmark::get(TRUE));
Example #9
0
 public function partials()
 {
     $data = $this->templateData();
     Benchmark::start('grid');
     $grid = DataGrid_Factory::create('test');
     $grid->table->border = 1;
     for ($column = 1; $column <= count($data['head']); $column++) {
         $grid->addField('key' . $column, 'Header ' . $column);
     }
     $grid->addColumn('static data', 'Static Head');
     $template = $grid->render('partials');
     $this->view->template = $template['template'];
     Benchmark::stop('grid');
     $benchmark = Benchmark::get('grid');
     $this->view->time = $benchmark['time'];
     $this->view->partials = json_encode($template['partials']);
     $this->view->data = json_encode($this->templateData());
 }