3 * @copyright Copyright (C) 2020, Friendica
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);
59 * A dataset for different profiling settings
62 public function dataPerformance()
66 'timestamp' => time(),
68 'functions' => ['test', 'it'],
71 'timestamp' => time(),
72 'name' => 'database_write',
73 'functions' => ['test', 'it2'],
76 'timestamp' => time(),
78 'functions' => ['test', 'it3'],
81 'timestamp' => time(),
82 'name' => 'cache_write',
83 'functions' => ['test', 'it4'],
86 'timestamp' => time(),
88 'functions' => ['test', 'it5'],
91 'timestamp' => time(),
96 'timestamp' => time(),
97 'name' => 'rendering',
98 'functions' => ['test', 'it7'],
101 'timestamp' => time(),
103 'functions' => ['test', 'it8'],
106 'timestamp' => time(),
108 'functions' => ['test'],
110 // This one isn't set during reset
112 'timestamp' => time(),
114 'functions' => ['test'],
120 * Test the Profiler savetimestamp
121 * @dataProvider dataPerformance
123 public function testSaveTimestamp($timestamp, $name, array $functions)
125 $configCache = \Mockery::mock(Cache::class);
126 $configCache->shouldReceive('get')
131 $profiler = new Profiler($configCache);
133 foreach ($functions as $function) {
134 $profiler->saveTimestamp($timestamp, $name, $function);
137 $this->assertGreaterThanOrEqual(0, $profiler->get($name));
141 * Test the Profiler reset
142 * @dataProvider dataPerformance
144 public function testReset($timestamp, $name, array $functions)
146 $configCache = \Mockery::mock(Cache::class);
147 $configCache->shouldReceive('get')
152 $profiler = new Profiler($configCache);
154 $profiler->saveTimestamp($timestamp, $name);
157 $this->assertEquals(0, $profiler->get($name));
160 public function dataBig()
166 'timestamp' => time(),
167 'name' => 'database',
168 'functions' => ['test', 'it'],
170 'database_write' => [
171 'timestamp' => time(),
172 'name' => 'database_write',
173 'functions' => ['test', 'it2'],
176 'timestamp' => time(),
178 'functions' => ['test', 'it3'],
181 'timestamp' => time(),
182 'name' => 'cache_write',
183 'functions' => ['test', 'it4'],
186 'timestamp' => time(),
188 'functions' => ['test', 'it5'],
196 * Test the output of the Profiler
197 * @dataProvider dataBig
199 public function testSaveLog($data)
202 ->shouldReceive('info')
203 ->with('test', \Mockery::any())
206 ->shouldReceive('info')
209 $configCache = \Mockery::mock(Cache::class);
210 $configCache->shouldReceive('get')
215 $profiler = new Profiler($configCache);
217 foreach ($data as $perf => $items) {
218 foreach ($items['functions'] as $function) {
219 $profiler->saveTimestamp($items['timestamp'], $items['name'], $function);
223 $profiler->saveLog($this->logger, 'test');
225 $output = $profiler->getRendertimeString();
227 foreach ($data as $perf => $items) {
228 foreach ($items['functions'] as $function) {
229 // assert that the output contains the functions
230 $this->assertRegExp('/' . $function . ': \d+/', $output);
236 * Test different enable and disable states of the profiler
238 public function testEnableDisable()
240 $configCache = \Mockery::mock(Cache::class);
241 $configCache->shouldReceive('get')
242 ->with('system', 'profiler')
245 $configCache->shouldReceive('get')
246 ->with('rendertime', 'callstack')
250 $profiler = new Profiler($configCache);
252 $this->assertFalse($profiler->isRendertime());
253 $this->assertEmpty($profiler->getRendertimeString());
255 $profiler->saveTimestamp(time(), 'network', 'test1');
257 $config = \Mockery::mock(IConfig::class);
258 $config->shouldReceive('get')
259 ->with('system', 'profiler')
262 $config->shouldReceive('get')
263 ->with('rendertime', 'callstack')
267 $profiler->update($config);
269 $this->assertFalse($profiler->isRendertime());
270 $this->assertEmpty($profiler->getRendertimeString());
272 $config->shouldReceive('get')
273 ->with('system', 'profiler')
276 $config->shouldReceive('get')
277 ->with('rendertime', 'callstack')
281 $profiler->update($config);
283 $profiler->saveTimestamp(time(), 'database', 'test2');
285 $this->assertTrue($profiler->isRendertime());
286 $output = $profiler->getRendertimeString();
287 $this->assertRegExp('/test1: \d+/', $output);
288 $this->assertRegExp('/test2: \d+/', $output);