]> git.mxchange.org Git - friendica.git/commitdiff
Adding composer & tests
authorPhilipp Holzer <admin@philipp.info>
Sat, 16 Feb 2019 23:03:38 +0000 (00:03 +0100)
committerPhilipp Holzer <admin@philipp.info>
Sat, 16 Feb 2019 23:03:38 +0000 (00:03 +0100)
composer.json
composer.lock
src/Util/Profiler.php
tests/src/Util/ProfilerTest.php [new file with mode: 0644]

index 39d9358926ed905cfe06de144dccb48c8331d0b0..c3d5d4570504e85339e805c2613453f5cbf7d508 100644 (file)
                "michelf/php-markdown": "^1.7",
                "mobiledetect/mobiledetectlib": "2.8.*",
                "paragonie/random_compat": "^2.0",
-               "pear/Text_LanguageDetect": "1.*",
+               "pear/text_languageDetect": "1.*",
                "seld/cli-prompt": "^1.0",
                "smarty/smarty": "^3.1",
                "fxp/composer-asset-plugin": "~1.3",
                "bower-asset/base64": "^1.0",
-               "bower-asset/Chart-js": "^2.7",
+               "bower-asset/chart-js": "^2.7",
                "bower-asset/perfect-scrollbar": "^0.6",
                "bower-asset/vue": "^2.5",
                "npm-asset/jquery": "^2.0",
@@ -50,7 +50,8 @@
                "npm-asset/fullcalendar": "^3.0.1",
                "npm-asset/cropperjs": "1.2.2",
                "npm-asset/imagesloaded": "4.1.4",
-               "monolog/monolog": "^1.24"
+               "monolog/monolog": "^1.24",
+               "psr/container": "^1.0"
        },
        "repositories": [
                {
@@ -96,7 +97,7 @@
                "phpunit/dbunit": "^2.0",
                "phpdocumentor/reflection-docblock": "^3.0.2",
                "phpunit/php-token-stream": "^1.4.2",
-               "mikey179/vfsStream": "^1.6",
+               "mikey179/vfsstream": "^1.6",
                "mockery/mockery": "^1.2",
                "johnkary/phpunit-speedtrap": "1.1"
        },
index f91b833de74ad1b5ae902809377a90ad3d032fa3..e554b119992198d1d35d8ef65eaf31c88d7ab368 100644 (file)
@@ -4,7 +4,7 @@
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
         "This file is @generated automatically"
     ],
-    "content-hash": "b944adfb6ede89430ba3b7e9ebb45acb",
+    "content-hash": "19fabb14e0dd5d806ef841e51d5f6a0b",
     "packages": [
         {
             "name": "asika/simple-console",
             ],
             "time": "2016-08-06T20:24:11+00:00"
         },
+        {
+            "name": "psr/container",
+            "version": "1.0.0",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/php-fig/container.git",
+                "reference": "b7ce3b176482dbbc1245ebf52b181af44c2cf55f"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/php-fig/container/zipball/b7ce3b176482dbbc1245ebf52b181af44c2cf55f",
+                "reference": "b7ce3b176482dbbc1245ebf52b181af44c2cf55f",
+                "shasum": ""
+            },
+            "require": {
+                "php": ">=5.3.0"
+            },
+            "type": "library",
+            "extra": {
+                "branch-alias": {
+                    "dev-master": "1.0.x-dev"
+                }
+            },
+            "autoload": {
+                "psr-4": {
+                    "Psr\\Container\\": "src/"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "authors": [
+                {
+                    "name": "PHP-FIG",
+                    "homepage": "http://www.php-fig.org/"
+                }
+            ],
+            "description": "Common Container Interface (PHP FIG PSR-11)",
+            "homepage": "https://github.com/php-fig/container",
+            "keywords": [
+                "PSR-11",
+                "container",
+                "container-interface",
+                "container-interop",
+                "psr"
+            ],
+            "time": "2017-02-14T16:28:37+00:00"
+        },
         {
             "name": "psr/http-message",
             "version": "1.0.1",
                 "testing",
                 "xunit"
             ],
+            "abandoned": true,
             "time": "2016-12-02T14:39:14+00:00"
         },
         {
index f65bede152ae3cb149ade2eef99e192abdf2aac4..e2d070075d9435a0efe115a85600778e21fc205f 100644 (file)
@@ -47,8 +47,7 @@ class Profiler implements ContainerInterface
                $this->enabled = $enabled;
                $this->rendertime = $renderTime;
                $this->logger = $logger;
-               $this->performance = [];
-               $this->callstack   = [];
+               $this->reset();
        }
 
        /**
@@ -103,7 +102,7 @@ class Profiler implements ContainerInterface
                        $this->performance['rendering'] = 0;
                        $this->performance['parser'] = 0;
                        $this->performance['marktime'] = 0;
-                       $this->performance['markstart'] = microtime(true);
+                       $this->performance['marktime'] = microtime(true);
                }
 
                if ($callstack) {
@@ -123,7 +122,7 @@ class Profiler implements ContainerInterface
         *
         * @param string $message Additional message for the log
         */
-       public function saveLog($message)
+       public function saveLog($message = '')
        {
                // Write down the performance values into the log
                if ($this->enabled) {
@@ -166,18 +165,18 @@ class Profiler implements ContainerInterface
                                                }
                                        }
                                }
-                               if (isset($this->callstack["dache"])) {
+                               if (isset($this->callstack["cache"])) {
                                        $o .= "\nCache Read:\n";
-                                       foreach ($this->callstack["dache"] as $func => $time) {
+                                       foreach ($this->callstack["cache"] as $func => $time) {
                                                $time = round($time, 3);
                                                if ($time > 0) {
                                                        $o .= $func.": ".$time."\n";
                                                }
                                        }
                                }
-                               if (isset($this->callstack["dache_write"])) {
+                               if (isset($this->callstack["cache_write"])) {
                                        $o .= "\nCache Write:\n";
-                                       foreach ($this->callstack["dache_write"] as $func => $time) {
+                                       foreach ($this->callstack["cache_write"] as $func => $time) {
                                                $time = round($time, 3);
                                                if ($time > 0) {
                                                        $o .= $func.": ".$time."\n";
@@ -193,23 +192,9 @@ class Profiler implements ContainerInterface
                                                }
                                        }
                                }
+                               $this->logger->info($message . ": " . $o);
                        }
 
-                       $this->logger->info(
-                               $message . ": " . sprintf(
-                                       "DB: %s/%s, Cache: %s/%s, Net: %s, I/O: %s, Other: %s, Total: %s".$o,
-                                       number_format($this->get('database') - $this->get('database_write'), 2),
-                                       number_format($this->get('database_write'), 2),
-                                       number_format($this->get('cache'), 2),
-                                       number_format($this->get('cache_write'), 2),
-                                       number_format($this->get('network'), 2),
-                                       number_format($this->get('file'), 2),
-                                       number_format($duration - ($this->get('database')
-                                                       + $this->get('cache') + $this->get('cache_write')
-                                                       + $this->get('network') + $this->get('file')), 2),
-                                       number_format($duration, 2)
-                               )
-                       );
                }
        }
 
diff --git a/tests/src/Util/ProfilerTest.php b/tests/src/Util/ProfilerTest.php
new file mode 100644 (file)
index 0000000..f9febea
--- /dev/null
@@ -0,0 +1,181 @@
+<?php
+
+namespace src\Util;
+
+use Friendica\Test\MockedTest;
+use Friendica\Util\Profiler;
+use Mockery\MockInterface;
+use Psr\Log\LoggerInterface;
+
+class ProfilerTest extends MockedTest
+{
+       /**
+        * @var LoggerInterface|MockInterface
+        */
+       private $logger;
+
+       protected function setUp()
+       {
+               parent::setUp();
+
+               $this->logger = \Mockery::mock('Psr\Log\LoggerInterface');
+       }
+
+       /**
+        * Test the Profiler setup
+        */
+       public function testSetUp()
+       {
+               $profiler = new Profiler($this->logger, true, true);
+       }
+
+       /**
+        * A dataset for different profiling settings
+        * @return array
+        */
+       public function dataPerformance()
+       {
+               return [
+                       'database' => [
+                               'timestamp' => time(),
+                               'name' => 'database',
+                               'functions' => ['test', 'it'],
+                       ],
+                       'database_write' => [
+                               'timestamp' => time(),
+                               'name' => 'database_write',
+                               'functions' => ['test', 'it2'],
+                       ],
+                       'cache' => [
+                               'timestamp' => time(),
+                               'name' => 'cache',
+                               'functions' => ['test', 'it3'],
+                       ],
+                       'cache_write' => [
+                               'timestamp' => time(),
+                               'name' => 'cache_write',
+                               'functions' => ['test', 'it4'],
+                       ],
+                       'network' => [
+                               'timestamp' => time(),
+                               'name' => 'network',
+                               'functions' => ['test', 'it5'],
+                       ],
+                       'file' => [
+                               'timestamp' => time(),
+                               'name' => 'file',
+                               'functions' => [],
+                       ],
+                       'rendering' => [
+                               'timestamp' => time(),
+                               'name' => 'rendering',
+                               'functions' => ['test', 'it7'],
+                       ],
+                       'parser' => [
+                               'timestamp' => time(),
+                               'name' => 'parser',
+                               'functions' => ['test', 'it8'],
+                       ],
+                       'marktime' => [
+                               'timestamp' => time(),
+                               'name' => 'parser',
+                               'functions' => ['test'],
+                       ],
+                       // This one isn't set during reset
+                       'unknown' => [
+                               'timestamp' => time(),
+                               'name' => 'unknown',
+                               'functions' => ['test'],
+                       ],
+               ];
+       }
+
+       /**
+        * Test the Profiler savetimestamp
+        * @dataProvider dataPerformance
+        */
+       public function testSaveTimestamp($timestamp, $name, array $functions)
+       {
+               $profiler = new Profiler($this->logger, true, true);
+
+               foreach ($functions as $function) {
+                       $profiler->saveTimestamp($timestamp, $name, $function);
+               }
+
+               $this->assertGreaterThanOrEqual(0, $profiler->get($name));
+       }
+
+       /**
+        * Test the Profiler reset
+        * @dataProvider dataPerformance
+        */
+       public function testReset($timestamp, $name, array $functions)
+       {
+               $profiler = new Profiler($this->logger, true, true);
+
+               $profiler->saveTimestamp($timestamp, $name);
+               $profiler->reset();
+
+               $this->assertEquals(0, $profiler->get($name));
+       }
+
+       public function dataBig()
+       {
+               return [
+                       'big' => [
+                               'data' => [
+                                       'database' => [
+                                               'timestamp' => time(),
+                                               'name' => 'database',
+                                               'functions' => ['test', 'it'],
+                                       ],
+                                       'database_write' => [
+                                               'timestamp' => time(),
+                                               'name' => 'database_write',
+                                               'functions' => ['test', 'it2'],
+                                       ],
+                                       'cache' => [
+                                               'timestamp' => time(),
+                                               'name' => 'cache',
+                                               'functions' => ['test', 'it3'],
+                                       ],
+                                       'cache_write' => [
+                                               'timestamp' => time(),
+                                               'name' => 'cache_write',
+                                               'functions' => ['test', 'it4'],
+                                       ],
+                                       'network' => [
+                                               'timestamp' => time(),
+                                               'name' => 'network',
+                                               'functions' => ['test', 'it5'],
+                                       ],
+                               ]
+                       ]
+               ];
+       }
+
+       /**
+        * Test the output of the Profiler
+        * @dataProvider dataBig
+        */
+       public function testSaveLog($data)
+       {
+               $this->logger
+                       ->shouldReceive('info')
+                       ->with('test', \Mockery::any())
+                       ->once();
+               $this->logger
+                       ->shouldReceive('info')
+                       ->once();
+
+               $profiler = new Profiler($this->logger, true, true);
+
+               foreach ($data as $perf => $items) {
+                       foreach ($items['functions'] as $function) {
+                               $profiler->saveTimestamp($items['timestamp'], $items['name'], $function);
+                       }
+               }
+
+               $profiler->saveLog('test');
+       }
+}