From 2c93067682cf193fa4b155928ba6ccaf824498f2 Mon Sep 17 00:00:00 2001 From: "Michael - piratica.eu" Date: Sat, 26 Jan 2013 16:46:43 +0100 Subject: [PATCH] Performance: Adding several variables for performance measuring. --- boot.php | 10 ++++++++-- include/dba.php | 10 ++++++---- include/network.php | 31 +++++++++++++++++++++++-------- include/text.php | 41 ++++++++++++++++++++++++----------------- 4 files changed, 61 insertions(+), 31 deletions(-) diff --git a/boot.php b/boot.php index cd7fa03b59..b851068f40 100644 --- a/boot.php +++ b/boot.php @@ -355,7 +355,8 @@ if(! class_exists('App')) { public $identities; public $is_mobile; public $is_tablet; - + public $performance = array(); + public $nav_sel; public $category; @@ -403,7 +404,7 @@ if(! class_exists('App')) { private $cached_profile_image; private $cached_profile_picdate; - + function __construct() { global $default_timezone, $argv, $argc; @@ -412,6 +413,11 @@ if(! class_exists('App')) { date_default_timezone_set($this->timezone); + $this->performance["start"] = microtime(true); + $this->performance["database"] = 0; + $this->performance["network"] = 0; + $this->performance["rendering"] = 0; + $this->config = array(); $this->page = array(); $this->pager= array(); diff --git a/include/dba.php b/include/dba.php index a9f70aa40d..dd7f56bfd9 100644 --- a/include/dba.php +++ b/include/dba.php @@ -78,18 +78,20 @@ class dba { $this->error = ''; - if(x($a->config,'system') && x($a->config['system'],'db_log')) - $stamp1 = microtime(true); + $stamp1 = microtime(true); if($this->mysqli) $result = @$this->db->query($sql); else $result = @mysql_query($sql,$this->db); + $stamp2 = microtime(true); + $duration = (float)($stamp2-$stamp1); + $a->performance["database"] += (float)$duration; + if(x($a->config,'system') && x($a->config['system'],'db_log')) { - $stamp2 = microtime(true); - $duration = round($stamp2-$stamp1, 3); if (($duration > $a->config["system"]["db_loglimit"])) { + $duration = round($duration, 3); $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); @file_put_contents($a->config["system"]["db_log"], $duration."\t". basename($backtrace[1]["file"])."\t". diff --git a/include/network.php b/include/network.php index 1de65c42cb..10590f8bf4 100644 --- a/include/network.php +++ b/include/network.php @@ -7,10 +7,12 @@ if(! function_exists('fetch_url')) { function fetch_url($url,$binary = false, &$redirects = 0, $timeout = 0, $accept_content=Null) { + $stamp1 = microtime(true); + $a = get_app(); $ch = @curl_init($url); - if(($redirects > 8) || (! $ch)) + if(($redirects > 8) || (! $ch)) return false; @curl_setopt($ch, CURLOPT_HEADER, true); @@ -101,6 +103,11 @@ function fetch_url($url,$binary = false, &$redirects = 0, $timeout = 0, $accept_ $body = substr($s,strlen($header)); $a->set_curl_headers($header); @curl_close($ch); + + $stamp2 = microtime(true); + $duration = (float)($stamp2-$stamp1); + $a->performance["network"] += (float)$duration; + return($body); }} @@ -108,6 +115,9 @@ function fetch_url($url,$binary = false, &$redirects = 0, $timeout = 0, $accept_ if(! function_exists('post_url')) { function post_url($url,$params, $headers = null, &$redirects = 0, $timeout = 0) { + + $stamp1 = microtime(true); + $a = get_app(); $ch = curl_init($url); if(($redirects > 8) || (! $ch)) @@ -190,6 +200,11 @@ function post_url($url,$params, $headers = null, &$redirects = 0, $timeout = 0) $a->set_curl_headers($header); curl_close($ch); + + $stamp2 = microtime(true); + $duration = (float)($stamp2-$stamp1); + $a->performance["network"] += (float)$duration; + return($body); }} @@ -299,9 +314,9 @@ function webfinger_dfrn($s,&$hcard) { if($link['@attributes']['rel'] === NAMESPACE_DFRN) $profile_link = $link['@attributes']['href']; if($link['@attributes']['rel'] === NAMESPACE_OSTATUSSUB) - $profile_link = 'stat:' . $link['@attributes']['template']; + $profile_link = 'stat:' . $link['@attributes']['template']; if($link['@attributes']['rel'] === 'http://microformats.org/profile/hcard') - $hcard = $link['@attributes']['href']; + $hcard = $link['@attributes']['href']; } } return $profile_link; @@ -417,7 +432,7 @@ function lrdd($uri, $debug = false) { elseif(x($link['@attributes'],'href')) $href = $link['@attributes']['href']; } - } + } } if((! isset($tpl)) || (! strpos($tpl,'{uri}'))) @@ -436,7 +451,7 @@ function lrdd($uri, $debug = false) { $lines = explode("\n",$headers); if(count($lines)) { - foreach($lines as $line) { + foreach($lines as $line) { if((stristr($line,'link:')) && preg_match('/<([^>].*)>.*rel\=[\'\"]lrdd[\'\"]/',$line,$matches)) { return(fetch_xrd_links($matches[1])); break; @@ -482,7 +497,7 @@ function lrdd($uri, $debug = false) { $lines = explode("\n",$headers); if(count($lines)) { - foreach($lines as $line) { + foreach($lines as $line) { // TODO alter the following regex to support multiple relations (space separated) if((stristr($line,'link:')) && preg_match('/<([^>].*)>.*rel\=[\'\"]lrdd[\'\"]/',$line,$matches)) { $pagelink = $matches[1]; @@ -598,14 +613,14 @@ function fetch_xrd_links($url) { if(! function_exists('validate_url')) { function validate_url(&$url) { - + // no naked subdomains (allow localhost for tests) if(strpos($url,'.') === false && strpos($url,'/localhost/') === false) return false; if(substr($url,0,4) != 'http') $url = 'http://' . $url; $h = @parse_url($url); - + if(($h) && (dns_get_record($h['host'], DNS_A + DNS_CNAME + DNS_PTR) || filter_var($h['host'], FILTER_VALIDATE_IP) )) { return true; } diff --git a/include/text.php b/include/text.php index 53dd06d1fd..8f0b07bc93 100644 --- a/include/text.php +++ b/include/text.php @@ -6,16 +6,17 @@ // returns substituted string. // WARNING: this is pretty basic, and doesn't properly handle search strings that are substrings of each other. // For instance if 'test' => "foo" and 'testing' => "bar", testing could become either bar or fooing, -// depending on the order in which they were declared in the array. +// depending on the order in which they were declared in the array. require_once("include/template_processor.php"); require_once("include/friendica_smarty.php"); -if(! function_exists('replace_macros')) { +if(! function_exists('replace_macros')) { function replace_macros($s,$r) { global $t; -// $ts = microtime(); + $stamp1 = microtime(true); + $a = get_app(); if($a->theme['template_engine'] === 'smarty3') { @@ -34,12 +35,14 @@ function replace_macros($s,$r) { } else { $r = $t->replace($s,$r); - + $output = template_unescape($r); } -// $tt = microtime() - $ts; -// $a = get_app(); -// $a->page['debug'] .= "$tt
\n"; + $a = get_app(); + $stamp2 = microtime(true); + $duration = (float)($stamp2-$stamp1); + $a->performance["rendering"] += (float)$duration; + return $output; }} @@ -427,12 +430,12 @@ function load_view_file($s) { $d = dirname($s); if(file_exists("$d/$lang/$b")) return file_get_contents("$d/$lang/$b"); - + $theme = current_theme(); if(file_exists("$d/theme/$theme/$b")) return file_get_contents("$d/theme/$theme/$b"); - + return file_get_contents($s); }} @@ -458,7 +461,8 @@ function get_intltext_template($s) { if(! function_exists('get_markup_template')) { function get_markup_template($s, $root = '') { -// $ts = microtime(); + $stamp1 = microtime(true); + $a = get_app(); if($a->theme['template_engine'] === 'smarty3') { @@ -467,18 +471,21 @@ function get_markup_template($s, $root = '') { $template = new FriendicaSmarty(); $template->filename = $template_file; -// $tt = microtime() - $ts; -// $a->page['debug'] .= "$tt
\n"; + $stamp2 = microtime(true); + $duration = (float)($stamp2-$stamp1); + $a->performance["rendering"] += (float)$duration; + return $template; } else { $template_file = get_template_file($a, $s, $root); -// $file_contents = file_get_contents($template_file); -// $tt = microtime() - $ts; -// $a->page['debug'] .= "$tt
\n"; -// return $file_contents; + + $stamp2 = microtime(true); + $duration = (float)($stamp2-$stamp1); + $a->performance["rendering"] += (float)$duration; + return file_get_contents($template_file); - } + } }} if(! function_exists("get_template_file")) {