From 6c4c00b1564fcd250e965c52a767d353aee306d8 Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Sat, 20 Aug 2016 11:27:26 +0100
Subject: [PATCH] Added size + decode time for major indexes

---
 build/index.php            | 31 ++++++++++++++++++++++++++-----
 core.php                   |  6 +++++-
 module_index.json          |  6 +++---
 modules/feature-search.php |  8 ++++++++
 modules/page-help.php      | 15 ++++++++++++---
 5 files changed, 54 insertions(+), 12 deletions(-)

diff --git a/build/index.php b/build/index.php
index f806914..ff68e6e 100755
--- a/build/index.php
+++ b/build/index.php
@@ -316,6 +316,7 @@ $env->user = "Anonymous";
 $env->is_logged_in = false;
 $env->is_admin = false;
 $env->storage_prefix = $settings->data_storage_dir . DIRECTORY_SEPARATOR;
+$env->perfdata = new stdClass();
 /// Paths ///
 $paths = new stdClass();
 $paths->pageindex = "pageindex.json"; // The pageindex
@@ -867,7 +868,8 @@ else
 {
 	$pageindex_read_start = microtime(true);
 	$pageindex = json_decode(file_get_contents($paths->pageindex));
-	header("x-pageindex-decode-time: " . round((microtime(true) - $pageindex_read_start)*1000, 3) . "ms");
+	$env->perfdata->pageindex_decode_time = round((microtime(true) - $pageindex_read_start)*1000, 3);
+	header("x-pageindex-decode-time: " . $env->perfdata->pageindex_decode_time . "ms");
 }
 
 //////////////////////////
@@ -875,7 +877,9 @@ else
 //////////////////////////
 if(!file_exists($paths->idindex))
 	file_put_contents($paths->idindex, "{}");
+$idindex_decode_start = microtime(true);
 $idindex = json_decode(file_get_contents($paths->idindex));
+$env->perfdata->idindex_decode_time = round((microtime(true) - $idindex_decode_start)*1000, 3);
 class ids
 {
 	/*
@@ -2393,6 +2397,14 @@ class search
 		return $invindex;
 	}
 	
+	public static function measure_invindex_load_time($invindex_filename) {
+		global $env;
+		
+		$searchindex_decode_start = microtime(true);
+		search::load_invindex($invindex_filename);
+		$env->perfdata->searchindex_decode_time = round((microtime(true) - $searchindex_decode_start)*1000, 3);
+	}
+	
 	/*
 	 * @summary Merge an index into an inverted index.
 	 */
@@ -3944,7 +3956,7 @@ register_module([
 		 * ██   ██ ███████ ███████ ██      
 		 */
 		add_action("help", function() {
-			global $paths, $settings, $version, $help_sections, $actions;
+			global $env, $paths, $settings, $version, $help_sections, $actions;
 			
 			// Sort the help sections by key
 			ksort($help_sections, SORT_NATURAL);
@@ -3971,8 +3983,17 @@ register_module([
 				<p>The following actions are currently registered:</p>\n";
 				$content .= "<p>" . implode(", ", array_keys(get_object_vars($actions))) . "</p>";
 				$content .= "<h3>Environment</h3>\n";
-				$content .= "<p>$settings->sitename's root directory is " . (!is_writeable(__DIR__) ? "not " : "") . "writeable.</p>";
-				$content .= "<p>The page index is currently " . human_filesize(filesize($paths->pageindex)) . " in size.</p>";
+				$content .= "<ul>\n";
+				$content .= "<li>$settings->sitename's root directory is " . (!is_writeable(__DIR__) ? "not " : "") . "writeable.</li>\n";
+				$content .= "<li>The page index is currently " . human_filesize(filesize($paths->pageindex)) . " in size, and took " . $env->perfdata->pageindex_decode_time . "ms to decode.</li>";
+				if(module_exists("feature-search"))
+				{
+					search::measure_invindex_load_time($paths->searchindex);
+					$content .= "<li>The search index is currently " . human_filesize(filesize($paths->searchindex)) . " in size, and took " . $env->perfdata->searchindex_decode_time . "ms to decode.</li>";
+				}
+				
+				$content .= "<li>The id index is currently " . human_filesize(filesize($paths->idindex)) . " in size, and took " . $env->perfdata->idindex_decode_time . "ms to decode.</li>";
+				
 			}
 			else
 			{
@@ -4783,7 +4804,7 @@ register_module([
 		"<p>$settings->sitename's editor uses an extended version of <a href='http://parsedown.org/'>Parsedown</a> to render pages, which is a fantastic open source Github flavoured markdown parser. You can find a quick reference guide on Github flavoured markdown <a href='https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet'>here</a> by <a href='https://github.com/adam-p/'>adam-p</a>, or if you prefer a book <a href='https://www.gitbook.com/book/roachhd/master-markdown/details'>Mastering Markdown</a> by KB is a good read, and free too!</p>
 		<h3>Tips</h3>
 		<ul>
-			<li>Put 2 spaces at the end of a line to add a soft line break. Leave a blank line to add a head line break (i.e. new paragraph).</li>
+			<li>Put 2 spaces at the end of a line to add a soft line break. Leave a blank line to add a head line break (i.e. a new paragraph).</li>
 			<li>You can add an id to a header that you can link to. Put it in curly braces after the heading name like this: <code># Heading Name {#HeadingId}</code>. Then you can link to like like this: <code>[[Page name#HeadingId}]]</code>. You can also link to a heading id on the current page by omitting the page name: <code>[[#HeadingId]]</code>.</li>
 		</ul>
 		<h3>Extra Syntax</h3>
diff --git a/core.php b/core.php
index 5af5400..96d5c63 100644
--- a/core.php
+++ b/core.php
@@ -15,6 +15,7 @@ $env->user = "Anonymous";
 $env->is_logged_in = false;
 $env->is_admin = false;
 $env->storage_prefix = $settings->data_storage_dir . DIRECTORY_SEPARATOR;
+$env->perfdata = new stdClass();
 /// Paths ///
 $paths = new stdClass();
 $paths->pageindex = "pageindex.json"; // The pageindex
@@ -566,7 +567,8 @@ else
 {
 	$pageindex_read_start = microtime(true);
 	$pageindex = json_decode(file_get_contents($paths->pageindex));
-	header("x-pageindex-decode-time: " . round((microtime(true) - $pageindex_read_start)*1000, 3) . "ms");
+	$env->perfdata->pageindex_decode_time = round((microtime(true) - $pageindex_read_start)*1000, 3);
+	header("x-pageindex-decode-time: " . $env->perfdata->pageindex_decode_time . "ms");
 }
 
 //////////////////////////
@@ -574,7 +576,9 @@ else
 //////////////////////////
 if(!file_exists($paths->idindex))
 	file_put_contents($paths->idindex, "{}");
+$idindex_decode_start = microtime(true);
 $idindex = json_decode(file_get_contents($paths->idindex));
+$env->perfdata->idindex_decode_time = round((microtime(true) - $idindex_decode_start)*1000, 3);
 class ids
 {
 	/*
diff --git a/module_index.json b/module_index.json
index ee74c2a..36bc17b 100755
--- a/module_index.json
+++ b/module_index.json
@@ -68,7 +68,7 @@
         "author": "Starbeamrainbowlabs",
         "description": "Adds proper search functionality to Pepperminty Wiki using an inverted index to provide a full text search engine. If pages don't show up, then you might have hit a stop word. If not, try requesting the `invindex-rebuild` action to rebuild the inverted index from scratch.",
         "id": "feature-search",
-        "lastupdate": 1471632781,
+        "lastupdate": 1471688611,
         "optional": false
     },
     {
@@ -131,7 +131,7 @@
         "author": "Starbeamrainbowlabs",
         "description": "Adds a rather useful help page. Access through the 'help' action. This module also exposes help content added to Pepperminty Wiki's inbuilt invisible help section system.",
         "id": "page-help",
-        "lastupdate": 1466009992,
+        "lastupdate": 1471688804,
         "optional": false
     },
     {
@@ -203,7 +203,7 @@
         "author": "Emanuil Rusev & Starbeamrainbowlabs",
         "description": "An upgraded (now default!) parser based on Emanuil Rusev's Parsedown Extra PHP library (https:\/\/github.com\/erusev\/parsedown-extra), which is licensed MIT. Please be careful, as this module adds some weight to your installation, and also *requires* write access to the disk on first load.",
         "id": "parser-parsedown",
-        "lastupdate": 1471634318,
+        "lastupdate": 1471634426,
         "optional": false
     }
 ]
\ No newline at end of file
diff --git a/modules/feature-search.php b/modules/feature-search.php
index 2d9c139..f44a2f7 100644
--- a/modules/feature-search.php
+++ b/modules/feature-search.php
@@ -318,6 +318,14 @@ class search
 		return $invindex;
 	}
 	
+	public static function measure_invindex_load_time($invindex_filename) {
+		global $env;
+		
+		$searchindex_decode_start = microtime(true);
+		search::load_invindex($invindex_filename);
+		$env->perfdata->searchindex_decode_time = round((microtime(true) - $searchindex_decode_start)*1000, 3);
+	}
+	
 	/*
 	 * @summary Merge an index into an inverted index.
 	 */
diff --git a/modules/page-help.php b/modules/page-help.php
index 5ee9a63..5a698ca 100644
--- a/modules/page-help.php
+++ b/modules/page-help.php
@@ -26,7 +26,7 @@ register_module([
 		 * ██   ██ ███████ ███████ ██      
 		 */
 		add_action("help", function() {
-			global $paths, $settings, $version, $help_sections, $actions;
+			global $env, $paths, $settings, $version, $help_sections, $actions;
 			
 			// Sort the help sections by key
 			ksort($help_sections, SORT_NATURAL);
@@ -53,8 +53,17 @@ register_module([
 				<p>The following actions are currently registered:</p>\n";
 				$content .= "<p>" . implode(", ", array_keys(get_object_vars($actions))) . "</p>";
 				$content .= "<h3>Environment</h3>\n";
-				$content .= "<p>$settings->sitename's root directory is " . (!is_writeable(__DIR__) ? "not " : "") . "writeable.</p>";
-				$content .= "<p>The page index is currently " . human_filesize(filesize($paths->pageindex)) . " in size.</p>";
+				$content .= "<ul>\n";
+				$content .= "<li>$settings->sitename's root directory is " . (!is_writeable(__DIR__) ? "not " : "") . "writeable.</li>\n";
+				$content .= "<li>The page index is currently " . human_filesize(filesize($paths->pageindex)) . " in size, and took " . $env->perfdata->pageindex_decode_time . "ms to decode.</li>";
+				if(module_exists("feature-search"))
+				{
+					search::measure_invindex_load_time($paths->searchindex);
+					$content .= "<li>The search index is currently " . human_filesize(filesize($paths->searchindex)) . " in size, and took " . $env->perfdata->searchindex_decode_time . "ms to decode.</li>";
+				}
+				
+				$content .= "<li>The id index is currently " . human_filesize(filesize($paths->idindex)) . " in size, and took " . $env->perfdata->idindex_decode_time . "ms to decode.</li>";
+				
 			}
 			else
 			{
