2019-02-16 18:03:38 -05:00
|
|
|
<?php
|
2020-02-09 09:45:36 -05:00
|
|
|
/**
|
2021-03-29 02:40:20 -04:00
|
|
|
* @copyright Copyright (C) 2010-2021, the Friendica project
|
2020-02-09 09:45:36 -05:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
2019-02-16 18:03:38 -05:00
|
|
|
|
2019-02-27 06:32:56 -05:00
|
|
|
namespace Friendica\Test\src\Util;
|
2019-02-16 18:03:38 -05:00
|
|
|
|
2021-10-23 05:29:16 -04:00
|
|
|
use Friendica\Core\Config\Cache\Cache;
|
2020-01-19 15:29:36 -05:00
|
|
|
use Friendica\Core\Config\IConfig;
|
2019-02-16 18:03:38 -05:00
|
|
|
use Friendica\Test\MockedTest;
|
|
|
|
use Friendica\Util\Profiler;
|
|
|
|
use Mockery\MockInterface;
|
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
|
|
|
|
class ProfilerTest extends MockedTest
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var LoggerInterface|MockInterface
|
|
|
|
*/
|
|
|
|
private $logger;
|
|
|
|
|
2021-04-01 17:04:30 -04:00
|
|
|
protected function setUp(): void
|
2019-02-16 18:03:38 -05:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2019-02-24 07:40:54 -05:00
|
|
|
$this->logger = \Mockery::mock(LoggerInterface::class);
|
2019-02-16 18:03:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the Profiler setup
|
|
|
|
*/
|
|
|
|
public function testSetUp()
|
|
|
|
{
|
2020-01-19 16:23:44 -05:00
|
|
|
$configCache = \Mockery::mock(Cache::class);
|
2019-07-20 19:22:10 -04:00
|
|
|
$configCache->shouldReceive('get')
|
|
|
|
->withAnyArgs()
|
|
|
|
->andReturn(true)
|
|
|
|
->twice();
|
|
|
|
$profiler = new Profiler($configCache);
|
2020-10-18 14:31:57 -04:00
|
|
|
|
|
|
|
self::assertInstanceOf(Profiler::class, $profiler);
|
2019-02-16 18:03:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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'],
|
|
|
|
],
|
2021-07-27 09:56:28 -04:00
|
|
|
'session' => [
|
2019-02-16 18:03:38 -05:00
|
|
|
'timestamp' => time(),
|
2021-07-27 09:56:28 -04:00
|
|
|
'name' => 'session',
|
2019-02-16 18:03:38 -05:00
|
|
|
'functions' => ['test', 'it8'],
|
|
|
|
],
|
|
|
|
'marktime' => [
|
|
|
|
'timestamp' => time(),
|
2021-07-27 09:56:28 -04:00
|
|
|
'name' => 'session',
|
2019-02-16 18:03:38 -05:00
|
|
|
'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)
|
|
|
|
{
|
2020-01-19 16:23:44 -05:00
|
|
|
$configCache = \Mockery::mock(Cache::class);
|
2019-07-20 19:22:10 -04:00
|
|
|
$configCache->shouldReceive('get')
|
|
|
|
->withAnyArgs()
|
|
|
|
->andReturn(true)
|
|
|
|
->twice();
|
|
|
|
|
|
|
|
$profiler = new Profiler($configCache);
|
2019-02-16 18:03:38 -05:00
|
|
|
|
|
|
|
foreach ($functions as $function) {
|
|
|
|
$profiler->saveTimestamp($timestamp, $name, $function);
|
|
|
|
}
|
|
|
|
|
2020-10-17 08:19:57 -04:00
|
|
|
self::assertGreaterThanOrEqual(0, $profiler->get($name));
|
2019-02-16 18:03:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the Profiler reset
|
|
|
|
* @dataProvider dataPerformance
|
|
|
|
*/
|
2020-10-18 14:31:57 -04:00
|
|
|
public function testReset($timestamp, $name)
|
2019-02-16 18:03:38 -05:00
|
|
|
{
|
2020-01-19 16:23:44 -05:00
|
|
|
$configCache = \Mockery::mock(Cache::class);
|
2019-07-20 19:22:10 -04:00
|
|
|
$configCache->shouldReceive('get')
|
|
|
|
->withAnyArgs()
|
|
|
|
->andReturn(true)
|
|
|
|
->twice();
|
|
|
|
|
|
|
|
$profiler = new Profiler($configCache);
|
2019-02-16 18:03:38 -05:00
|
|
|
|
|
|
|
$profiler->saveTimestamp($timestamp, $name);
|
|
|
|
$profiler->reset();
|
|
|
|
|
2020-10-17 08:19:57 -04:00
|
|
|
self::assertEquals(0, $profiler->get($name));
|
2019-02-16 18:03:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
2020-01-19 16:23:44 -05:00
|
|
|
$configCache = \Mockery::mock(Cache::class);
|
2019-07-20 19:22:10 -04:00
|
|
|
$configCache->shouldReceive('get')
|
|
|
|
->withAnyArgs()
|
|
|
|
->andReturn(true)
|
|
|
|
->twice();
|
|
|
|
|
|
|
|
$profiler = new Profiler($configCache);
|
2019-02-16 18:03:38 -05:00
|
|
|
|
|
|
|
foreach ($data as $perf => $items) {
|
|
|
|
foreach ($items['functions'] as $function) {
|
|
|
|
$profiler->saveTimestamp($items['timestamp'], $items['name'], $function);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-17 15:12:12 -05:00
|
|
|
$profiler->saveLog($this->logger, 'test');
|
2019-02-20 11:20:17 -05:00
|
|
|
|
|
|
|
$output = $profiler->getRendertimeString();
|
|
|
|
|
|
|
|
foreach ($data as $perf => $items) {
|
|
|
|
foreach ($items['functions'] as $function) {
|
|
|
|
// assert that the output contains the functions
|
2020-10-17 08:19:57 -04:00
|
|
|
self::assertRegExp('/' . $function . ': \d+/', $output);
|
2019-02-20 11:20:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-02-22 19:37:29 -05:00
|
|
|
* Test different enable and disable states of the profiler
|
2019-02-20 11:20:17 -05:00
|
|
|
*/
|
2019-02-22 19:37:29 -05:00
|
|
|
public function testEnableDisable()
|
2019-02-20 11:20:17 -05:00
|
|
|
{
|
2020-01-19 16:23:44 -05:00
|
|
|
$configCache = \Mockery::mock(Cache::class);
|
2019-07-20 19:22:10 -04:00
|
|
|
$configCache->shouldReceive('get')
|
|
|
|
->with('system', 'profiler')
|
|
|
|
->andReturn(true)
|
|
|
|
->once();
|
|
|
|
$configCache->shouldReceive('get')
|
|
|
|
->with('rendertime', 'callstack')
|
|
|
|
->andReturn(false)
|
|
|
|
->once();
|
|
|
|
|
|
|
|
$profiler = new Profiler($configCache);
|
2019-02-20 11:20:17 -05:00
|
|
|
|
2020-10-17 08:19:57 -04:00
|
|
|
self::assertFalse($profiler->isRendertime());
|
|
|
|
self::assertEmpty($profiler->getRendertimeString());
|
2019-02-22 19:37:29 -05:00
|
|
|
|
2021-07-27 02:35:44 -04:00
|
|
|
$profiler->saveTimestamp(time(), 'network', 'test1');
|
2019-02-22 19:37:29 -05:00
|
|
|
|
2020-01-19 15:29:36 -05:00
|
|
|
$config = \Mockery::mock(IConfig::class);
|
2019-07-20 19:22:10 -04:00
|
|
|
$config->shouldReceive('get')
|
|
|
|
->with('system', 'profiler')
|
|
|
|
->andReturn(false)
|
|
|
|
->once();
|
|
|
|
$config->shouldReceive('get')
|
|
|
|
->with('rendertime', 'callstack')
|
|
|
|
->andReturn(false)
|
|
|
|
->once();
|
|
|
|
|
|
|
|
$profiler->update($config);
|
2019-02-22 19:37:29 -05:00
|
|
|
|
2020-10-17 08:19:57 -04:00
|
|
|
self::assertFalse($profiler->isRendertime());
|
|
|
|
self::assertEmpty($profiler->getRendertimeString());
|
2019-02-22 19:37:29 -05:00
|
|
|
|
2019-07-20 19:22:10 -04:00
|
|
|
$config->shouldReceive('get')
|
|
|
|
->with('system', 'profiler')
|
|
|
|
->andReturn(true)
|
|
|
|
->once();
|
|
|
|
$config->shouldReceive('get')
|
|
|
|
->with('rendertime', 'callstack')
|
|
|
|
->andReturn(true)
|
|
|
|
->once();
|
|
|
|
|
|
|
|
$profiler->update($config);
|
2019-02-22 19:37:29 -05:00
|
|
|
|
2021-07-27 02:35:44 -04:00
|
|
|
$profiler->saveTimestamp(time(), 'database', 'test2');
|
2019-02-22 19:37:29 -05:00
|
|
|
|
2020-10-17 08:19:57 -04:00
|
|
|
self::assertTrue($profiler->isRendertime());
|
2019-02-22 19:37:29 -05:00
|
|
|
$output = $profiler->getRendertimeString();
|
2020-10-17 08:19:57 -04:00
|
|
|
self::assertRegExp('/test1: \d+/', $output);
|
|
|
|
self::assertRegExp('/test2: \d+/', $output);
|
2019-02-16 18:03:38 -05:00
|
|
|
}
|
|
|
|
}
|