Adding composer & tests

This commit is contained in:
Philipp Holzer 2019-02-17 00:03:38 +01:00
parent 68c904bbf4
commit 6d73dcbe3d
No known key found for this signature in database
GPG key ID: 517BE60E2CE5C8A5
4 changed files with 245 additions and 28 deletions

View file

@ -35,12 +35,12 @@
"michelf/php-markdown": "^1.7", "michelf/php-markdown": "^1.7",
"mobiledetect/mobiledetectlib": "2.8.*", "mobiledetect/mobiledetectlib": "2.8.*",
"paragonie/random_compat": "^2.0", "paragonie/random_compat": "^2.0",
"pear/Text_LanguageDetect": "1.*", "pear/text_languageDetect": "1.*",
"seld/cli-prompt": "^1.0", "seld/cli-prompt": "^1.0",
"smarty/smarty": "^3.1", "smarty/smarty": "^3.1",
"fxp/composer-asset-plugin": "~1.3", "fxp/composer-asset-plugin": "~1.3",
"bower-asset/base64": "^1.0", "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/perfect-scrollbar": "^0.6",
"bower-asset/vue": "^2.5", "bower-asset/vue": "^2.5",
"npm-asset/jquery": "^2.0", "npm-asset/jquery": "^2.0",
@ -50,7 +50,8 @@
"npm-asset/fullcalendar": "^3.0.1", "npm-asset/fullcalendar": "^3.0.1",
"npm-asset/cropperjs": "1.2.2", "npm-asset/cropperjs": "1.2.2",
"npm-asset/imagesloaded": "4.1.4", "npm-asset/imagesloaded": "4.1.4",
"monolog/monolog": "^1.24" "monolog/monolog": "^1.24",
"psr/container": "^1.0"
}, },
"repositories": [ "repositories": [
{ {
@ -96,7 +97,7 @@
"phpunit/dbunit": "^2.0", "phpunit/dbunit": "^2.0",
"phpdocumentor/reflection-docblock": "^3.0.2", "phpdocumentor/reflection-docblock": "^3.0.2",
"phpunit/php-token-stream": "^1.4.2", "phpunit/php-token-stream": "^1.4.2",
"mikey179/vfsStream": "^1.6", "mikey179/vfsstream": "^1.6",
"mockery/mockery": "^1.2", "mockery/mockery": "^1.2",
"johnkary/phpunit-speedtrap": "1.1" "johnkary/phpunit-speedtrap": "1.1"
}, },

52
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "b944adfb6ede89430ba3b7e9ebb45acb", "content-hash": "19fabb14e0dd5d806ef841e51d5f6a0b",
"packages": [ "packages": [
{ {
"name": "asika/simple-console", "name": "asika/simple-console",
@ -1882,6 +1882,55 @@
], ],
"time": "2016-08-06T20:24:11+00:00" "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", "name": "psr/http-message",
"version": "1.0.1", "version": "1.0.1",
@ -2650,6 +2699,7 @@
"testing", "testing",
"xunit" "xunit"
], ],
"abandoned": true,
"time": "2016-12-02T14:39:14+00:00" "time": "2016-12-02T14:39:14+00:00"
}, },
{ {

View file

@ -47,8 +47,7 @@ class Profiler implements ContainerInterface
$this->enabled = $enabled; $this->enabled = $enabled;
$this->rendertime = $renderTime; $this->rendertime = $renderTime;
$this->logger = $logger; $this->logger = $logger;
$this->performance = []; $this->reset();
$this->callstack = [];
} }
/** /**
@ -103,7 +102,7 @@ class Profiler implements ContainerInterface
$this->performance['rendering'] = 0; $this->performance['rendering'] = 0;
$this->performance['parser'] = 0; $this->performance['parser'] = 0;
$this->performance['marktime'] = 0; $this->performance['marktime'] = 0;
$this->performance['markstart'] = microtime(true); $this->performance['marktime'] = microtime(true);
} }
if ($callstack) { if ($callstack) {
@ -123,7 +122,7 @@ class Profiler implements ContainerInterface
* *
* @param string $message Additional message for the log * @param string $message Additional message for the log
*/ */
public function saveLog($message) public function saveLog($message = '')
{ {
// Write down the performance values into the log // Write down the performance values into the log
if ($this->enabled) { 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"; $o .= "\nCache Read:\n";
foreach ($this->callstack["dache"] as $func => $time) { foreach ($this->callstack["cache"] as $func => $time) {
$time = round($time, 3); $time = round($time, 3);
if ($time > 0) { if ($time > 0) {
$o .= $func.": ".$time."\n"; $o .= $func.": ".$time."\n";
} }
} }
} }
if (isset($this->callstack["dache_write"])) { if (isset($this->callstack["cache_write"])) {
$o .= "\nCache Write:\n"; $o .= "\nCache Write:\n";
foreach ($this->callstack["dache_write"] as $func => $time) { foreach ($this->callstack["cache_write"] as $func => $time) {
$time = round($time, 3); $time = round($time, 3);
if ($time > 0) { if ($time > 0) {
$o .= $func.": ".$time."\n"; $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)
)
);
} }
} }

View file

@ -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');
}
}