]> git.mxchange.org Git - friendica.git/blob - tests/src/Util/ProfilerTest.php
Merge pull request #8261 from MrPetovan/task/8251-use-about-for-pdesc
[friendica.git] / tests / src / Util / ProfilerTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Test\src\Util;
23
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;
30
31 class ProfilerTest extends MockedTest
32 {
33         /**
34          * @var LoggerInterface|MockInterface
35          */
36         private $logger;
37
38         protected function setUp()
39         {
40                 parent::setUp();
41
42                 $this->logger = \Mockery::mock(LoggerInterface::class);
43         }
44
45         /**
46          * Test the Profiler setup
47          */
48         public function testSetUp()
49         {
50                 $configCache = \Mockery::mock(Cache::class);
51                 $configCache->shouldReceive('get')
52                             ->withAnyArgs()
53                             ->andReturn(true)
54                             ->twice();
55                 $profiler = new Profiler($configCache);
56         }
57
58         /**
59          * A dataset for different profiling settings
60          * @return array
61          */
62         public function dataPerformance()
63         {
64                 return [
65                         'database' => [
66                                 'timestamp' => time(),
67                                 'name' => 'database',
68                                 'functions' => ['test', 'it'],
69                         ],
70                         'database_write' => [
71                                 'timestamp' => time(),
72                                 'name' => 'database_write',
73                                 'functions' => ['test', 'it2'],
74                         ],
75                         'cache' => [
76                                 'timestamp' => time(),
77                                 'name' => 'cache',
78                                 'functions' => ['test', 'it3'],
79                         ],
80                         'cache_write' => [
81                                 'timestamp' => time(),
82                                 'name' => 'cache_write',
83                                 'functions' => ['test', 'it4'],
84                         ],
85                         'network' => [
86                                 'timestamp' => time(),
87                                 'name' => 'network',
88                                 'functions' => ['test', 'it5'],
89                         ],
90                         'file' => [
91                                 'timestamp' => time(),
92                                 'name' => 'file',
93                                 'functions' => [],
94                         ],
95                         'rendering' => [
96                                 'timestamp' => time(),
97                                 'name' => 'rendering',
98                                 'functions' => ['test', 'it7'],
99                         ],
100                         'parser' => [
101                                 'timestamp' => time(),
102                                 'name' => 'parser',
103                                 'functions' => ['test', 'it8'],
104                         ],
105                         'marktime' => [
106                                 'timestamp' => time(),
107                                 'name' => 'parser',
108                                 'functions' => ['test'],
109                         ],
110                         // This one isn't set during reset
111                         'unknown' => [
112                                 'timestamp' => time(),
113                                 'name' => 'unknown',
114                                 'functions' => ['test'],
115                         ],
116                 ];
117         }
118
119         /**
120          * Test the Profiler savetimestamp
121          * @dataProvider dataPerformance
122          */
123         public function testSaveTimestamp($timestamp, $name, array $functions)
124         {
125                 $configCache = \Mockery::mock(Cache::class);
126                 $configCache->shouldReceive('get')
127                             ->withAnyArgs()
128                             ->andReturn(true)
129                             ->twice();
130
131                 $profiler = new Profiler($configCache);
132
133                 foreach ($functions as $function) {
134                         $profiler->saveTimestamp($timestamp, $name, $function);
135                 }
136
137                 $this->assertGreaterThanOrEqual(0, $profiler->get($name));
138         }
139
140         /**
141          * Test the Profiler reset
142          * @dataProvider dataPerformance
143          */
144         public function testReset($timestamp, $name, array $functions)
145         {
146                 $configCache = \Mockery::mock(Cache::class);
147                 $configCache->shouldReceive('get')
148                             ->withAnyArgs()
149                             ->andReturn(true)
150                             ->twice();
151
152                 $profiler = new Profiler($configCache);
153
154                 $profiler->saveTimestamp($timestamp, $name);
155                 $profiler->reset();
156
157                 $this->assertEquals(0, $profiler->get($name));
158         }
159
160         public function dataBig()
161         {
162                 return [
163                         'big' => [
164                                 'data' => [
165                                         'database' => [
166                                                 'timestamp' => time(),
167                                                 'name' => 'database',
168                                                 'functions' => ['test', 'it'],
169                                         ],
170                                         'database_write' => [
171                                                 'timestamp' => time(),
172                                                 'name' => 'database_write',
173                                                 'functions' => ['test', 'it2'],
174                                         ],
175                                         'cache' => [
176                                                 'timestamp' => time(),
177                                                 'name' => 'cache',
178                                                 'functions' => ['test', 'it3'],
179                                         ],
180                                         'cache_write' => [
181                                                 'timestamp' => time(),
182                                                 'name' => 'cache_write',
183                                                 'functions' => ['test', 'it4'],
184                                         ],
185                                         'network' => [
186                                                 'timestamp' => time(),
187                                                 'name' => 'network',
188                                                 'functions' => ['test', 'it5'],
189                                         ],
190                                 ]
191                         ]
192                 ];
193         }
194
195         /**
196          * Test the output of the Profiler
197          * @dataProvider dataBig
198          */
199         public function testSaveLog($data)
200         {
201                 $this->logger
202                         ->shouldReceive('info')
203                         ->with('test', \Mockery::any())
204                         ->once();
205                 $this->logger
206                         ->shouldReceive('info')
207                         ->once();
208
209                 $configCache = \Mockery::mock(Cache::class);
210                 $configCache->shouldReceive('get')
211                             ->withAnyArgs()
212                             ->andReturn(true)
213                             ->twice();
214
215                 $profiler = new Profiler($configCache);
216
217                 foreach ($data as $perf => $items) {
218                         foreach ($items['functions'] as $function) {
219                                 $profiler->saveTimestamp($items['timestamp'], $items['name'], $function);
220                         }
221                 }
222
223                 $profiler->saveLog($this->logger, 'test');
224
225                 $output = $profiler->getRendertimeString();
226
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);
231                         }
232                 }
233         }
234
235         /**
236          * Test different enable and disable states of the profiler
237          */
238         public function testEnableDisable()
239         {
240                 $configCache = \Mockery::mock(Cache::class);
241                 $configCache->shouldReceive('get')
242                             ->with('system', 'profiler')
243                             ->andReturn(true)
244                             ->once();
245                 $configCache->shouldReceive('get')
246                             ->with('rendertime', 'callstack')
247                             ->andReturn(false)
248                             ->once();
249
250                 $profiler = new Profiler($configCache);
251
252                 $this->assertFalse($profiler->isRendertime());
253                 $this->assertEmpty($profiler->getRendertimeString());
254
255                 $profiler->saveTimestamp(time(), 'network', 'test1');
256
257                 $config = \Mockery::mock(IConfig::class);
258                 $config->shouldReceive('get')
259                             ->with('system', 'profiler')
260                             ->andReturn(false)
261                             ->once();
262                 $config->shouldReceive('get')
263                             ->with('rendertime', 'callstack')
264                             ->andReturn(false)
265                             ->once();
266
267                 $profiler->update($config);
268
269                 $this->assertFalse($profiler->isRendertime());
270                 $this->assertEmpty($profiler->getRendertimeString());
271
272                 $config->shouldReceive('get')
273                        ->with('system', 'profiler')
274                        ->andReturn(true)
275                        ->once();
276                 $config->shouldReceive('get')
277                        ->with('rendertime', 'callstack')
278                        ->andReturn(true)
279                        ->once();
280
281                 $profiler->update($config);
282
283                 $profiler->saveTimestamp(time(), 'database', 'test2');
284
285                 $this->assertTrue($profiler->isRendertime());
286                 $output = $profiler->getRendertimeString();
287                 $this->assertRegExp('/test1: \d+/', $output);
288                 $this->assertRegExp('/test2: \d+/', $output);
289         }
290 }