From fdee002f73bce767c364b58daafeccc8eff5927e Mon Sep 17 00:00:00 2001
From: Michael Vogel <icarus@dabo.de>
Date: Sat, 26 Jan 2013 18:35:39 +0100
Subject: [PATCH] Several improvements for performance measuring

---
 boot.php            |  7 +++++
 include/bbcode.php  |  4 +++
 include/dba.php     |  7 ++++-
 include/items.php   |  2 ++
 include/network.php | 13 ++++-----
 include/plugin.php  | 17 +++++++----
 include/tags.php    |  2 ++
 include/text.php    | 69 ++++++++++++++++++++++++++++++---------------
 mod/item.php        |  2 ++
 9 files changed, 87 insertions(+), 36 deletions(-)

diff --git a/boot.php b/boot.php
index b851068f4..b0e77a71f 100644
--- a/boot.php
+++ b/boot.php
@@ -417,6 +417,7 @@ if(! class_exists('App')) {
 			$this->performance["database"] = 0;
 			$this->performance["network"] = 0;
 			$this->performance["rendering"] = 0;
+			$this->performance["parser"] = 0;
 
 			$this->config = array();
 			$this->page = array();
@@ -725,6 +726,12 @@ if(! class_exists('App')) {
 			return $this->rdelim[$engine];
 		}
 
+		function save_timestamp($stamp1, $value) {
+			$stamp2 = microtime(true);
+			$duration = (float)($stamp2-$stamp1);
+			$this->performance[$value] += (float)$duration;
+		}
+
 	}
 }
 
diff --git a/include/bbcode.php b/include/bbcode.php
index 613c2b7db..45213cc3b 100644
--- a/include/bbcode.php
+++ b/include/bbcode.php
@@ -283,6 +283,8 @@ function bb_ShareAttributes($match) {
 
 function bbcode($Text,$preserve_nl = false, $tryoembed = true) {
 
+	$stamp1 = microtime(true);
+
 	$a = get_app();
 
 	// Hide all [noparse] contained bbtags by spacefying them
@@ -637,6 +639,8 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true) {
 
 	call_hooks('bbcode',$Text);
 
+	$a->save_timestamp($stamp1, "parser");
+
 	return $Text;
 }
 
diff --git a/include/dba.php b/include/dba.php
index dd7f56bfd..50354b7f4 100644
--- a/include/dba.php
+++ b/include/dba.php
@@ -23,6 +23,9 @@ class dba {
 	public  $error = false;
 
 	function __construct($server,$user,$pass,$db,$install = false) {
+		global $a;
+
+		$stamp1 = microtime(true);
 
 		$server = trim($server);
 		$user = trim($user);
@@ -64,6 +67,8 @@ class dba {
 			if(! $install)
 				system_unavailable();
 		}
+
+		$a->save_timestamp($stamp1, "network");
 	}
 
 	public function getdb() {
@@ -87,7 +92,6 @@ class dba {
 
 		$stamp2 = microtime(true);
 		$duration = (float)($stamp2-$stamp1);
-		$a->performance["database"] += (float)$duration;
 
 		if(x($a->config,'system') && x($a->config['system'],'db_log')) {
 			if (($duration > $a->config["system"]["db_loglimit"])) {
@@ -164,6 +168,7 @@ class dba {
 			}
 		}
 
+		$a->save_timestamp($stamp1, "database");
 
 		if($this->debug)
 			logger('dba: ' . printable(print_r($r, true)));
diff --git a/include/items.php b/include/items.php
index 54b392b38..1a50ac204 100755
--- a/include/items.php
+++ b/include/items.php
@@ -1159,7 +1159,9 @@ function item_store($arr,$force_parent = false) {
 
 	if (($cachefile != '') AND !file_exists($cachefile)) {
 		$s = prepare_text($arr['body']);
+		$stamp1 = microtime(true);
 		file_put_contents($cachefile, $s);
+		$a->save_timestamp($stamp1, "file");
 		logger('item_store: put item '.$current_post.' into cachefile '.$cachefile);
 	}
 
diff --git a/include/network.php b/include/network.php
index 10590f8bf..400c1386b 100644
--- a/include/network.php
+++ b/include/network.php
@@ -104,9 +104,7 @@ function fetch_url($url,$binary = false, &$redirects = 0, $timeout = 0, $accept_
 	$a->set_curl_headers($header);
 	@curl_close($ch);
 
-	$stamp2 = microtime(true);
-	$duration = (float)($stamp2-$stamp1);
-	$a->performance["network"] += (float)$duration;
+	$a->save_timestamp($stamp1, "network");
 
 	return($body);
 }}
@@ -201,9 +199,7 @@ function post_url($url,$params, $headers = null, &$redirects = 0, $timeout = 0)
 
 	curl_close($ch);
 
-	$stamp2 = microtime(true);
-	$duration = (float)($stamp2-$stamp1);
-	$a->performance["network"] += (float)$duration;
+	$a->save_timestamp($stamp1, "network");
 
 	return($body);
 }}
@@ -851,8 +847,11 @@ function scale_external_images($s, $include_link = true, $scale_replace = false)
 			$i = fetch_url($scaled);
 
 			$cachefile = get_cachefile(hash("md5", $scaled));
-			if ($cachefile != '')
+			if ($cachefile != '') {
+				$stamp1 = microtime(true);
 				file_put_contents($cachefile, $i);
+				$a->save_timestamp($stamp1, "file");
+			}
 
 			// guess mimetype from headers or filename
 			$type = guess_image_type($mtch[1],true);
diff --git a/include/plugin.php b/include/plugin.php
index db3224f29..a6928793d 100644
--- a/include/plugin.php
+++ b/include/plugin.php
@@ -201,12 +201,15 @@ function get_plugin_info($plugin){
 		'author' => array(),
 		'version' => ""
 	);
-	
+
 	if (!is_file("addon/$plugin/$plugin.php")) return $info;
-	
+
+	$stamp1 = microtime(true);
 	$f = file_get_contents("addon/$plugin/$plugin.php");
+	$a->save_timestamp($stamp1, "file");
+
 	$r = preg_match("|/\*.*\*/|msU", $f, $m);
-	
+
 	if ($r){
 		$ll = explode("\n", $m[0]);
 		foreach( $ll as $l ) {
@@ -266,11 +269,13 @@ function get_theme_info($theme){
 		$info['unsupported'] = true;
 
 	if (!is_file("view/theme/$theme/theme.php")) return $info;
-	
+
+	$stamp1 = microtime(true);
 	$f = file_get_contents("view/theme/$theme/theme.php");
+	$a->save_timestamp($stamp1, "file");
+
 	$r = preg_match("|/\*.*\*/|msU", $f, $m);
-	
-	
+
 	if ($r){
 		$ll = explode("\n", $m[0]);
 		foreach( $ll as $l ) {
diff --git a/include/tags.php b/include/tags.php
index b02adc3d8..c81a752d5 100644
--- a/include/tags.php
+++ b/include/tags.php
@@ -46,7 +46,9 @@ function create_tags_from_item($itemid) {
 
 	if (($cachefile != '') AND !file_exists($cachefile)) {
 		$s = prepare_text($message['body']);
+		$stamp1 = microtime(true);
 		file_put_contents($cachefile, $s);
+		$a->save_timestamp($stamp1, "file");
 		logger('create_tags_from_item: put item '.$message["id"].' into cachefile '.$cachefile);
 	}
 
diff --git a/include/text.php b/include/text.php
index 8f0b07bc9..83639ce85 100644
--- a/include/text.php
+++ b/include/text.php
@@ -428,15 +428,26 @@ function load_view_file($s) {
 		$lang = 'en';
 	$b = basename($s);
 	$d = dirname($s);
-	if(file_exists("$d/$lang/$b"))
-		return file_get_contents("$d/$lang/$b");
+	if(file_exists("$d/$lang/$b")) {
+		$stamp1 = microtime(true);
+		$content = file_get_contents("$d/$lang/$b");
+		$a->save_timestamp($stamp1, "file");
+		return $content;
+	}
 
 	$theme = current_theme();
 
-	if(file_exists("$d/theme/$theme/$b"))
-		return file_get_contents("$d/theme/$theme/$b");
+	if(file_exists("$d/theme/$theme/$b")) {
+		$stamp1 = microtime(true);
+		$content = file_get_contents("$d/theme/$theme/$b");
+		$a->save_timestamp($stamp1, "file");
+		return $content;
+	}
 
-	return file_get_contents($s);
+	$stamp1 = microtime(true);
+	$content = file_get_contents($s);
+	$a->save_timestamp($stamp1, "file");
+	return $content;
 }}
 
 if(! function_exists('get_intltext_template')) {
@@ -451,12 +462,22 @@ function get_intltext_template($s) {
 	if(! isset($lang))
 		$lang = 'en';
 
-	if(file_exists("view/$lang$engine/$s"))
-		return file_get_contents("view/$lang$engine/$s");
-	elseif(file_exists("view/en$engine/$s"))
-		return file_get_contents("view/en$engine/$s");
-	else
-		return file_get_contents("view$engine/$s");
+	if(file_exists("view/$lang$engine/$s")) {
+		$stamp1 = microtime(true);
+		$content = file_get_contents("view/$lang$engine/$s");
+		$a->save_timestamp($stamp1, "file");
+		return $content;
+	} elseif(file_exists("view/en$engine/$s")) {
+		$stamp1 = microtime(true);
+		$content = file_get_contents("view/en$engine/$s");
+		$a->save_timestamp($stamp1, "file");
+		return $content;
+	} else {
+		$stamp1 = microtime(true);
+		$content = file_get_contents("view$engine/$s");
+		$a->save_timestamp($stamp1, "file");
+		return $content;
+	}
 }}
 
 if(! function_exists('get_markup_template')) {
@@ -470,21 +491,19 @@ function get_markup_template($s, $root = '') {
 
 		$template = new FriendicaSmarty();
 		$template->filename = $template_file;
-
-		$stamp2 = microtime(true);
-		$duration = (float)($stamp2-$stamp1);
-		$a->performance["rendering"] += (float)$duration;
+		$a->save_timestamp($stamp1, "rendering");
 
 		return $template;
 	}
 	else {
 		$template_file = get_template_file($a, $s, $root);
+		$a->save_timestamp($stamp1, "rendering");
 
-		$stamp2 = microtime(true);
-		$duration = (float)($stamp2-$stamp1);
-		$a->performance["rendering"] += (float)$duration;
+		$stamp1 = microtime(true);
+		$content = file_get_contents($template_file);
+		$a->save_timestamp($stamp1, "file");
+		return $content;
 
-		return file_get_contents($template_file);
 	}
 }}
 
@@ -541,8 +560,10 @@ function logger($msg,$level = 0) {
 
 	if((! $debugging) || (! $logfile) || ($level > $loglevel))
 		return;
-	
+
+	$stamp1 = microtime(true);
 	@file_put_contents($logfile, datetime_convert() . ':' . session_id() . ' ' . $msg . "\n", FILE_APPEND);
+	$a->save_timestamp($stamp1, "file");
 	return;
 }}
 
@@ -1029,11 +1050,15 @@ function prepare_body($item,$attach = false) {
 	$cachefile = get_cachefile($item["guid"]."-".hash("md5", $item['body']));
 
 	if (($cachefile != '')) {
-		if (file_exists($cachefile))
+		if (file_exists($cachefile)) {
+			$stamp1 = microtime(true);
 			$s = file_get_contents($cachefile);
-		else {
+			$a->save_timestamp($stamp1, "file");
+		} else {
 			$s = prepare_text($item['body']);
+			$stamp1 = microtime(true);
 			file_put_contents($cachefile, $s);
+			$a->save_timestamp($stamp1, "file");
 			logger('prepare_body: put item '.$item["id"].' into cachefile '.$cachefile);
 		}
 	} else
diff --git a/mod/item.php b/mod/item.php
index 452ec8a36..926fbbf52 100644
--- a/mod/item.php
+++ b/mod/item.php
@@ -762,7 +762,9 @@ function item_post(&$a) {
 
 		if (($cachefile != '') AND !file_exists($cachefile)) {
 			$s = prepare_text($datarray['body']);
+			$stamp1 = microtime(true);
 			file_put_contents($cachefile, $s);
+			$a->save_timestamp($stamp1, "file");
 			logger('mod_item: put item '.$r[0]['id'].' into cachefile '.$cachefile);
 		}