summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--core/classes/Benchmark.php106
-rw-r--r--docker-compose.redis.yml34
-rw-r--r--public/app/config/setup_framework.php19
-rw-r--r--public/index.php4
-rwxr-xr-xtests/code/echo.php2
-rwxr-xr-xtests/compare/compress.php2
-rwxr-xr-xtests/compare/md5-v-sha1.php4
-rwxr-xr-xtests/compare/memcached-v-filecache.php7
-rwxr-xr-xtests/compare/redis-v-filecache.php2
-rwxr-xr-xtests/compare/serialize-v-jsonencode.php6
-rwxr-xr-xtests/compare/sha1-v-sha256.php3
11 files changed, 130 insertions, 59 deletions
diff --git a/core/classes/Benchmark.php b/core/classes/Benchmark.php
index a7e93ea..8aa040d 100644
--- a/core/classes/Benchmark.php
+++ b/core/classes/Benchmark.php
@@ -1,14 +1,52 @@
<?php
-// define('BENCHMARKED', true); // then you know if class is active
-
+/** Benchmarn class
+ * -----------------------------------------------------------------------------
+ *
+ * Handy class to evaluate the apprication's performace
+ *
+ * [*] calulates time difference between any two or more marked timespots;
+ * [*] tracks memory usage for the marked timespots
+ * [*] creates reports in various formats
+ *
+ * TODO: create an (optional) ascii graph ??
+ *
+ * -----------------------------------------------------------------------------
+ */
class Benchmark {
+ /** PROPERTIES
+ * -------------------------------------------------------------------------
+ */
+
+
+ /** @var array
+ * array of microtimes in [key => val] pairs, where
+ * + key (string): is a custom label used for refering
+ * + val (float) : the microtime when the timespot was created
+ */
private static $timeSpots = Array();
+
+ /** @var array
+ * array of memory used in [key => val] pairs, where
+ * + key (string): is a custom label used for refering (same as in $timeSpots)
+ * + val (float) : the memory usage (in bytes) when the timespot was created
+ */
private static $memoryUse = Array();
- private static $enableReport = true;
+
+ /** @var boolean
+ * flag to determin if reporting is enaabled
+ */
+ private static $reportEnabled = true;
+
+
+
+
+ /** METHODS
+ * -------------------------------------------------------------------------
+ */
/** add_timespot
@@ -37,7 +75,7 @@ class Benchmark {
*/
public static function disableReport()
{
- self::$enableReport = false;
+ self::$reportEnabled = false;
}
@@ -45,31 +83,47 @@ class Benchmark {
* ---
* renders performance report
* as html comment at the end of webpage in various modes
- * @param $mode (string)
- * + 'comment' (default): include report as html comment
- * + 'html' : include report as a html div
- * + 'code' : include report as a pre/code block
+ *
+ * @param $mode (int): report format id
+ * ... default: BENCH_REPORT_COMMENT (5) = report as html comment;
+ * ... for more report-formats check the BENCH_REPORT_* defines
*/
- public static function render_report(string $mode = 'comment' )
+ public static function report(string $mode = BENCH_REPORT_COMMENT)
{
- if (self::$enableReport) {
+ if ( (self::$reportEnabled) && (!empty(self::$timeSpots)) ) {
switch ($mode) {
- case 'html':
+ case BENCH_REPORT_ARRAY:
+ return [
+ 'timespots' => self::$timeSpots,
+ 'memory_usage' => self::$memoryUse
+ ];
+ break;
+
+ case BENCH_REPORT_JSON:
+ return json_encode([
+ 'timespots' => self::$timeSpots,
+ 'memory_usage' => self::$memoryUse
+ ], JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE);
+ break;
+
+ case BENCH_REPORT_HTML:
$open = '<p>';
$close = '</b></p>';
$divider = ":<b>";
$prefix = '<div>';
$suffix = '</div>';
break;
- case 'code':
+
+ case BENCH_REPORT_CODE:
$open = '';
$close = '';
$divider = ": ";
$prefix = '<pre>';
$suffix = '</pre>';
break;
- case 'comment':
+
+ case BENCH_REPORT_COMMENT:
default:
$open = '<!--';
$close = '-->';
@@ -79,11 +133,11 @@ class Benchmark {
break;
}
- echo $prefix;
+ $output = $prefix;
// TIME REPORT
// ---------------------------------------------------------------------
- echo "\n\n{$open} Timings {$close}";
+ $output .= "\n\n{$open} Timings {$close}";
// valid from php 7.3
// $start = self::$timeSpots[ array_key_first(self::$timespots) ];
@@ -95,7 +149,7 @@ class Benchmark {
// if more than 2 timespots
- // echo dt between each spot
+ // $output .= dt between each spot
if (count(self::$timeSpots) > 2) {
// calculate dt between spots
@@ -118,33 +172,35 @@ class Benchmark {
}
}
- // echo timings
+ // $output .= timings
foreach ($time_results as $key => $val) {
- echo "\n\t{$open} {$val['part']} {$divider} {$val['dt']} {$close}";
+ $output .= "\n\t{$open} {$val['part']} {$divider} {$val['dt']} {$close}";
}
}
- // echo total time
- echo "\n\t{$open} total {$divider} {$all_dt} {$close}";
+ // $output .= total time
+ $output .= "\n\t{$open} total {$divider} {$all_dt} {$close}";
// MEMORY REPORT
// ---------------------------------------------------------------------
- echo "\n\n{$open} Memory Usage {$close}";
+ $output .= "\n\n{$open} Memory Usage {$close}";
if (count(self::$memoryUse) > 2) {
foreach (self::$memoryUse as $key => $val) {
- echo "\n\t{$open} {$key} {$divider} {$val}MB {$close}";
+ $output .= "\n\t{$open} {$key} {$divider} {$val}MB {$close}";
}
} else {
- echo "\n\t{$open} memory usage {$divider} ". round(memory_get_usage()/(1024*1024),2) ."MB {$close}";
+ $output .= "\n\t{$open} memory usage {$divider} ". round(memory_get_usage()/(1024*1024),2) ."MB {$close}";
}
- echo $suffix;
+ $output .= $suffix;
}
-
+ return $output;
+
}
+
}
diff --git a/docker-compose.redis.yml b/docker-compose.redis.yml
index 80f8021..d938019 100644
--- a/docker-compose.redis.yml
+++ b/docker-compose.redis.yml
@@ -109,23 +109,23 @@ services:
# - MYSQL_ROOT_PASSWORD=root
# - MYSQL_DATABASE=development
- # phpmyadmin:
- # image: library/phpmyadmin
- # container_name: skeleton-docker.phpmyadmin
- # tty: true
- # networks:
- # - skeleton-docker.network
- # depends_on:
- # - mysql
- # - mariadb
- # environment:
- # PMA_USER: root
- # PMA_PASSWORD: root
- # PMA_HOSTS: mysql,mariadb
- # PMA_PORT: 3306
- # PMA_ARBITRARY: 1
- # ports:
- # - '8080:80'
+ # phpmyadmin:
+ # image: library/phpmyadmin
+ # container_name: skeleton-docker.phpmyadmin
+ # tty: true
+ # networks:
+ # - skeleton-docker.network
+ # depends_on:
+ # - mysql
+ # - mariadb
+ # environment:
+ # PMA_USER: root
+ # PMA_PASSWORD: root
+ # PMA_HOSTS: mysql,mariadb
+ # PMA_PORT: 3306
+ # PMA_ARBITRARY: 1
+ # ports:
+ # - '8080:80'
# postgres:
# build: ./docker/postgres
diff --git a/public/app/config/setup_framework.php b/public/app/config/setup_framework.php
index 384a643..0198e10 100644
--- a/public/app/config/setup_framework.php
+++ b/public/app/config/setup_framework.php
@@ -424,7 +424,8 @@ define('MEDIA_STORAGE_ROOT', APP_ROOT.'/storage/uploads/'); // Media files ro
*
* Anom suggests:
* + Common Content Types, and
- * + Proxy flags
+ * + Proxy flags
+ * + Benchmark report formats
*
* -----------------------------------------------------------------------------
*/
@@ -470,6 +471,19 @@ define('PROXY_DO_NOT_CACHE', 4); // do not cache result
+/** BENCHMARK REPORT TYPES
+ * -----------------------------------------------------------------------------
+ */
+
+define('BENCH_REPORT_ARRAY', 1); // repost as array
+define('BENCH_REPORT_JSON' , 2); // report as json oblject
+define('BENCH_REPORT_HTML' , 3); // report as HTML code
+define('BENCH_REPORT_CODE' , 4); // report as CODE/text
+define('BENCH_REPORT_COMMENT' , 5); // report as HTML comment
+
+
+
+
/** need even more constants? //////////////////////////////////////////////////
* -----------------------------------------------------------------------------
*
@@ -478,4 +492,5 @@ define('PROXY_DO_NOT_CACHE', 4); // do not cache result
*
* -----------------------------------------------------------------------------
*/
-require_once WEB_ROOT.'/app/config/setup_application.php';
+
+ require_once WEB_ROOT.'/app/config/setup_application.php';
diff --git a/public/index.php b/public/index.php
index c0b0b68..10029c5 100644
--- a/public/index.php
+++ b/public/index.php
@@ -57,7 +57,7 @@ if (!PRODUCTION) Benchmark::add_spot('start'); // benchmark request's life
// Initialize Application
-// finalize framework; handle credentials; setup app-engine; insert routes
+// finalize framework; setup app-engine; insert routes; investigate request
// -----------------------------------------------------------------------------
require_once APPLICATION_BOOTSTRAP;
@@ -71,5 +71,5 @@ Route::run(Registry::get('REQUEST')); // Run baby, run!
if (!PRODUCTION) Benchmark::add_spot('end'); // request ended
-if (!PRODUCTION) Benchmark::render_report(); // echo benchmark report
+if (!PRODUCTION) echo( Benchmark::report()); // echo benchmark report
ob_flush(); // flush output
diff --git a/tests/code/echo.php b/tests/code/echo.php
index b8b0c34..8d37a45 100755
--- a/tests/code/echo.php
+++ b/tests/code/echo.php
@@ -40,4 +40,4 @@ Etiam eu iaculis augue. Phasellus dapibus nec orci sed fermentum. Fusce nisi tor
Duis sed tincidunt augue, et posuere tortor. Ut facilisis eros id lacus luctus finibus. Duis a libero tristique, viverra enim id, fermentum sem. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Maecenas a posuere magna. Fusce maximus nibh sit amet dignissim suscipit. Maecenas lacinia purus vel sapien tempor, at suscipit ipsum finibus. Vestibulum varius, metus eu scelerisque cursus, massa magna scelerisque nunc, nec venenatis lacus augue vitae ante. Phasellus massa lacus, lacinia condimentum dui in, porttitor aliquam sapien. In placerat auctor gravida. Phasellus sit amet justo in ipsum tristique laoreet quis ac mi. Pellentesque tincidunt dignissim ligula, et finibus nisl interdum quis. Mauris rhoncus arcu dignissim, interdum odio vestibulum, rutrum mi. Donec consectetur volutpat justo eget feugiat. Phasellus enim enim, hendrerit nec tempus ac, ornare vitae quam.
";
Benchmark::add_spot('end'); // request ended
-Benchmark::render_report(); \ No newline at end of file
+echo( Benchmark::report()); \ No newline at end of file
diff --git a/tests/compare/compress.php b/tests/compare/compress.php
index 74fdea1..f36baa2 100755
--- a/tests/compare/compress.php
+++ b/tests/compare/compress.php
@@ -74,7 +74,7 @@ foreach($products as $product) {
Benchmark::add_spot('end');
-Benchmark::render_report('code');
+echo(Benchmark::report(BENCH_REPORT_COMMENT));
echo "test completed!";
die();
diff --git a/tests/compare/md5-v-sha1.php b/tests/compare/md5-v-sha1.php
index 44b5148..74d0371 100755
--- a/tests/compare/md5-v-sha1.php
+++ b/tests/compare/md5-v-sha1.php
@@ -11,8 +11,8 @@ Benchmark::add_spot('100.000 x sha');
for($i = 0 ; $i<100000 ; $i++) $x = sha1($i);
Benchmark::add_spot('end');
-Benchmark::render_report('code');
-
+echo(Benchmark::report(BENCH_REPORT_COMMENT));
echo "test completed!";
+
die();
\ No newline at end of file
diff --git a/tests/compare/memcached-v-filecache.php b/tests/compare/memcached-v-filecache.php
index 539b0c5..4ffdfed 100755
--- a/tests/compare/memcached-v-filecache.php
+++ b/tests/compare/memcached-v-filecache.php
@@ -27,9 +27,8 @@ foreach($products as $product) {
}
Benchmark::add_spot('end');
-Benchmark::render_report('code');
-
+echo(Benchmark::report(BENCH_REPORT_COMMENT));
echo "test completed!";
-die();
-
+
+die();
diff --git a/tests/compare/redis-v-filecache.php b/tests/compare/redis-v-filecache.php
index e59c390..3cb6536 100755
--- a/tests/compare/redis-v-filecache.php
+++ b/tests/compare/redis-v-filecache.php
@@ -27,7 +27,7 @@ foreach($products as $product) {
}
Benchmark::add_spot('end');
-Benchmark::render_report('code');
+echo(Benchmark::report(BENCH_REPORT_COMMENT));
echo "test completed!";
die();
diff --git a/tests/compare/serialize-v-jsonencode.php b/tests/compare/serialize-v-jsonencode.php
index ba9ccc9..c66c21b 100755
--- a/tests/compare/serialize-v-jsonencode.php
+++ b/tests/compare/serialize-v-jsonencode.php
@@ -29,7 +29,7 @@ foreach($products as $product) {
}
Benchmark::add_spot('end');
-Benchmark::render_report('code');
-
+echo(Benchmark::report(BENCH_REPORT_COMMENT));
echo "test completed!";
-die(); \ No newline at end of file
+
+die();
diff --git a/tests/compare/sha1-v-sha256.php b/tests/compare/sha1-v-sha256.php
index 4d10e7f..d4bf5dc 100755
--- a/tests/compare/sha1-v-sha256.php
+++ b/tests/compare/sha1-v-sha256.php
@@ -15,7 +15,8 @@ Benchmark::add_spot('100.000 x sha256');
for($i = 0 ; $i<100000 ; $i++) $x = hash('sha256', $i);
Benchmark::add_spot('end');
-Benchmark::render_report('code');
+
+echo (Benchmark::report(BENCH_REPORT_COMMENT));
echo "test completed!";
die();