3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Test\src\Util;
24 use Friendica\Core\Config\Cache;
25 use Friendica\Core\Config\IConfig;
26 use Friendica\Test\MockedTest;
27 use Friendica\Util\Profiler;
28 use Mockery\MockInterface;
29 use Psr\Log\LoggerInterface;
31 class ProfilerTest extends MockedTest
34 * @var LoggerInterface|MockInterface
38 protected function setUp()
42 $this->logger = \Mockery::mock(LoggerInterface::class);
46 * Test the Profiler setup
48 public function testSetUp()
50 $configCache = \Mockery::mock(Cache::class);
51 $configCache->shouldReceive('get')
55 $profiler = new Profiler($configCache);
57 self::assertInstanceOf(Profiler::class, $profiler);
61 * A dataset for different profiling settings
64 public function dataPerformance()
68 'timestamp' => time(),
70 'functions' => ['test', 'it'],
73 'timestamp' => time(),
74 'name' => 'database_write',
75 'functions' => ['test', 'it2'],
78 'timestamp' => time(),
80 'functions' => ['test', 'it3'],
83 'timestamp' => time(),
84 'name' => 'cache_write',
85 'functions' => ['test', 'it4'],
88 'timestamp' => time(),
90 'functions' => ['test', 'it5'],
93 'timestamp' => time(),
98 'timestamp' => time(),
99 'name' => 'rendering',
100 'functions' => ['test', 'it7'],
103 'timestamp' => time(),
105 'functions' => ['test', 'it8'],
108 'timestamp' => time(),
110 'functions' => ['test'],
112 // This one isn't set during reset
114 'timestamp' => time(),
116 'functions' => ['test'],
122 * Test the Profiler savetimestamp
123 * @dataProvider dataPerformance
125 public function testSaveTimestamp($timestamp, $name, array $functions)
127 $configCache = \Mockery::mock(Cache::class);
128 $configCache->shouldReceive('get')
133 $profiler = new Profiler($configCache);
135 foreach ($functions as $function) {
136 $profiler->saveTimestamp($timestamp, $name, $function);
139 self::assertGreaterThanOrEqual(0, $profiler->get($name));
143 * Test the Profiler reset
144 * @dataProvider dataPerformance
146 public function testReset($timestamp, $name)
148 $configCache = \Mockery::mock(Cache::class);
149 $configCache->shouldReceive('get')
154 $profiler = new Profiler($configCache);
156 $profiler->saveTimestamp($timestamp, $name);
159 self::assertEquals(0, $profiler->get($name));
162 public function dataBig()
168 'timestamp' => time(),
169 'name' => 'database',
170 'functions' => ['test', 'it'],
172 'database_write' => [
173 'timestamp' => time(),
174 'name' => 'database_write',
175 'functions' => ['test', 'it2'],
178 'timestamp' => time(),
180 'functions' => ['test', 'it3'],
183 'timestamp' => time(),
184 'name' => 'cache_write',
185 'functions' => ['test', 'it4'],
188 'timestamp' => time(),
190 'functions' => ['test', 'it5'],
198 * Test the output of the Profiler
199 * @dataProvider dataBig
201 public function testSaveLog($data)
204 ->shouldReceive('info')
205 ->with('test', \Mockery::any())
208 ->shouldReceive('info')
211 $configCache = \Mockery::mock(Cache::class);
212 $configCache->shouldReceive('get')
217 $profiler = new Profiler($configCache);
219 foreach ($data as $perf => $items) {
220 foreach ($items['functions'] as $function) {
221 $profiler->saveTimestamp($items['timestamp'], $items['name'], $function);
225 $profiler->saveLog($this->logger, 'test');
227 $output = $profiler->getRendertimeString();
229 foreach ($data as $perf => $items) {
230 foreach ($items['functions'] as $function) {
231 // assert that the output contains the functions
232 self::assertRegExp('/' . $function . ': \d+/', $output);
238 * Test different enable and disable states of the profiler
240 public function testEnableDisable()
242 $configCache = \Mockery::mock(Cache::class);
243 $configCache->shouldReceive('get')
244 ->with('system', 'profiler')
247 $configCache->shouldReceive('get')
248 ->with('rendertime', 'callstack')
252 $profiler = new Profiler($configCache);
254 self::assertFalse($profiler->isRendertime());
255 self::assertEmpty($profiler->getRendertimeString());
257 $profiler->saveTimestamp(time(), 'network', 'test1');
259 $config = \Mockery::mock(IConfig::class);
260 $config->shouldReceive('get')
261 ->with('system', 'profiler')
264 $config->shouldReceive('get')
265 ->with('rendertime', 'callstack')
269 $profiler->update($config);
271 self::assertFalse($profiler->isRendertime());
272 self::assertEmpty($profiler->getRendertimeString());
274 $config->shouldReceive('get')
275 ->with('system', 'profiler')
278 $config->shouldReceive('get')
279 ->with('rendertime', 'callstack')
283 $profiler->update($config);
285 $profiler->saveTimestamp(time(), 'database', 'test2');
287 self::assertTrue($profiler->isRendertime());
288 $output = $profiler->getRendertimeString();
289 self::assertRegExp('/test1: \d+/', $output);
290 self::assertRegExp('/test2: \d+/', $output);