3 * @copyright Copyright (C) 2010-2023, 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\Capability\IManageConfigValues;
25 use Friendica\Test\MockedTest;
26 use Friendica\Util\Profiler;
27 use Mockery\MockInterface;
28 use Psr\Log\LoggerInterface;
30 class ProfilerTest extends MockedTest
33 * @var LoggerInterface|MockInterface
37 protected function setUp(): void
41 $this->logger = \Mockery::mock(LoggerInterface::class);
45 * Test the Profiler setup
47 public function testSetUp()
49 $config = \Mockery::mock(IManageConfigValues::class);
50 $config->shouldReceive('get')
54 $profiler = new Profiler($config);
56 self::assertInstanceOf(Profiler::class, $profiler);
60 * A dataset for different profiling settings
63 public function dataPerformance()
67 'timestamp' => time(),
69 'functions' => ['test', 'it'],
72 'timestamp' => time(),
73 'name' => 'database_write',
74 'functions' => ['test', 'it2'],
77 'timestamp' => time(),
79 'functions' => ['test', 'it3'],
82 'timestamp' => time(),
83 'name' => 'cache_write',
84 'functions' => ['test', 'it4'],
87 'timestamp' => time(),
89 'functions' => ['test', 'it5'],
92 'timestamp' => time(),
97 'timestamp' => time(),
98 'name' => 'rendering',
99 'functions' => ['test', 'it7'],
102 'timestamp' => time(),
104 'functions' => ['test', 'it8'],
107 'timestamp' => time(),
109 'functions' => ['test'],
111 // This one isn't set during reset
113 'timestamp' => time(),
115 'functions' => ['test'],
121 * Test the Profiler savetimestamp
122 * @dataProvider dataPerformance
124 public function testSaveTimestamp($timestamp, $name, array $functions)
126 $config = \Mockery::mock(IManageConfigValues::class);
127 $config->shouldReceive('get')
132 $profiler = new Profiler($config);
134 foreach ($functions as $function) {
135 $profiler->saveTimestamp($timestamp, $name, $function);
138 self::assertGreaterThanOrEqual(0, $profiler->get($name));
142 * Test the Profiler reset
143 * @dataProvider dataPerformance
145 public function testReset($timestamp, $name)
147 $config = \Mockery::mock(IManageConfigValues::class);
148 $config->shouldReceive('get')
153 $profiler = new Profiler($config);
155 $profiler->saveTimestamp($timestamp, $name);
158 self::assertEquals(0, $profiler->get($name));
161 public function dataBig()
167 'timestamp' => time(),
168 'name' => 'database',
169 'functions' => ['test', 'it'],
171 'database_write' => [
172 'timestamp' => time(),
173 'name' => 'database_write',
174 'functions' => ['test', 'it2'],
177 'timestamp' => time(),
179 'functions' => ['test', 'it3'],
182 'timestamp' => time(),
183 'name' => 'cache_write',
184 'functions' => ['test', 'it4'],
187 'timestamp' => time(),
189 'functions' => ['test', 'it5'],
197 * Test the output of the Profiler
198 * @dataProvider dataBig
200 public function testSaveLog($data)
203 ->shouldReceive('info')
204 ->with('test', \Mockery::any())
207 ->shouldReceive('info')
210 $config = \Mockery::mock(IManageConfigValues::class);
211 $config->shouldReceive('get')
216 $profiler = new Profiler($config);
218 foreach ($data as $perf => $items) {
219 foreach ($items['functions'] as $function) {
220 $profiler->saveTimestamp($items['timestamp'], $items['name'], $function);
224 $profiler->saveLog($this->logger, 'test');
226 $output = $profiler->getRendertimeString();
228 foreach ($data as $perf => $items) {
229 foreach ($items['functions'] as $function) {
230 // assert that the output contains the functions
231 self::assertMatchesRegularExpression('/' . $function . ': \d+/', $output);