]> git.mxchange.org Git - friendica.git/blob - tests/src/Util/ProfilerTest.php
Merge pull request #13375 from MrPetovan/bug/empty-timeline
[friendica.git] / tests / src / Util / ProfilerTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
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\Capability\IManageConfigValues;
25 use Friendica\Test\MockedTest;
26 use Friendica\Util\Profiler;
27 use Mockery\MockInterface;
28 use Psr\Log\LoggerInterface;
29
30 class ProfilerTest extends MockedTest
31 {
32         /**
33          * @var LoggerInterface|MockInterface
34          */
35         private $logger;
36
37         protected function setUp(): void
38         {
39                 parent::setUp();
40
41                 $this->logger = \Mockery::mock(LoggerInterface::class);
42         }
43
44         /**
45          * Test the Profiler setup
46          */
47         public function testSetUp()
48         {
49                 $config = \Mockery::mock(IManageConfigValues::class);
50                 $config->shouldReceive('get')
51                             ->withAnyArgs()
52                             ->andReturn(true)
53                             ->twice();
54                 $profiler = new Profiler($config);
55
56                 self::assertInstanceOf(Profiler::class, $profiler);
57         }
58
59         /**
60          * A dataset for different profiling settings
61          * @return array
62          */
63         public function dataPerformance()
64         {
65                 return [
66                         'database' => [
67                                 'timestamp' => time(),
68                                 'name' => 'database',
69                                 'functions' => ['test', 'it'],
70                         ],
71                         'database_write' => [
72                                 'timestamp' => time(),
73                                 'name' => 'database_write',
74                                 'functions' => ['test', 'it2'],
75                         ],
76                         'cache' => [
77                                 'timestamp' => time(),
78                                 'name' => 'cache',
79                                 'functions' => ['test', 'it3'],
80                         ],
81                         'cache_write' => [
82                                 'timestamp' => time(),
83                                 'name' => 'cache_write',
84                                 'functions' => ['test', 'it4'],
85                         ],
86                         'network' => [
87                                 'timestamp' => time(),
88                                 'name' => 'network',
89                                 'functions' => ['test', 'it5'],
90                         ],
91                         'file' => [
92                                 'timestamp' => time(),
93                                 'name' => 'file',
94                                 'functions' => [],
95                         ],
96                         'rendering' => [
97                                 'timestamp' => time(),
98                                 'name' => 'rendering',
99                                 'functions' => ['test', 'it7'],
100                         ],
101                         'session' => [
102                                 'timestamp' => time(),
103                                 'name' => 'session',
104                                 'functions' => ['test', 'it8'],
105                         ],
106                         'marktime' => [
107                                 'timestamp' => time(),
108                                 'name' => 'session',
109                                 'functions' => ['test'],
110                         ],
111                         // This one isn't set during reset
112                         'unknown' => [
113                                 'timestamp' => time(),
114                                 'name' => 'unknown',
115                                 'functions' => ['test'],
116                         ],
117                 ];
118         }
119
120         /**
121          * Test the Profiler savetimestamp
122          * @dataProvider dataPerformance
123          */
124         public function testSaveTimestamp($timestamp, $name, array $functions)
125         {
126                 $config = \Mockery::mock(IManageConfigValues::class);
127                 $config->shouldReceive('get')
128                             ->withAnyArgs()
129                             ->andReturn(true)
130                             ->twice();
131
132                 $profiler = new Profiler($config);
133
134                 foreach ($functions as $function) {
135                         $profiler->saveTimestamp($timestamp, $name, $function);
136                 }
137
138                 self::assertGreaterThanOrEqual(0, $profiler->get($name));
139         }
140
141         /**
142          * Test the Profiler reset
143          * @dataProvider dataPerformance
144          */
145         public function testReset($timestamp, $name)
146         {
147                 $config = \Mockery::mock(IManageConfigValues::class);
148                 $config->shouldReceive('get')
149                             ->withAnyArgs()
150                             ->andReturn(true)
151                             ->twice();
152
153                 $profiler = new Profiler($config);
154
155                 $profiler->saveTimestamp($timestamp, $name);
156                 $profiler->reset();
157
158                 self::assertEquals(0, $profiler->get($name));
159         }
160
161         public function dataBig()
162         {
163                 return [
164                         'big' => [
165                                 'data' => [
166                                         'database' => [
167                                                 'timestamp' => time(),
168                                                 'name' => 'database',
169                                                 'functions' => ['test', 'it'],
170                                         ],
171                                         'database_write' => [
172                                                 'timestamp' => time(),
173                                                 'name' => 'database_write',
174                                                 'functions' => ['test', 'it2'],
175                                         ],
176                                         'cache' => [
177                                                 'timestamp' => time(),
178                                                 'name' => 'cache',
179                                                 'functions' => ['test', 'it3'],
180                                         ],
181                                         'cache_write' => [
182                                                 'timestamp' => time(),
183                                                 'name' => 'cache_write',
184                                                 'functions' => ['test', 'it4'],
185                                         ],
186                                         'network' => [
187                                                 'timestamp' => time(),
188                                                 'name' => 'network',
189                                                 'functions' => ['test', 'it5'],
190                                         ],
191                                 ]
192                         ]
193                 ];
194         }
195
196         /**
197          * Test the output of the Profiler
198          * @dataProvider dataBig
199          */
200         public function testSaveLog($data)
201         {
202                 $this->logger
203                         ->shouldReceive('info')
204                         ->with('test', \Mockery::any())
205                         ->once();
206                 $this->logger
207                         ->shouldReceive('info')
208                         ->once();
209
210                 $config = \Mockery::mock(IManageConfigValues::class);
211                 $config->shouldReceive('get')
212                             ->withAnyArgs()
213                             ->andReturn(true)
214                             ->twice();
215
216                 $profiler = new Profiler($config);
217
218                 foreach ($data as $perf => $items) {
219                         foreach ($items['functions'] as $function) {
220                                 $profiler->saveTimestamp($items['timestamp'], $items['name'], $function);
221                         }
222                 }
223
224                 $profiler->saveLog($this->logger, 'test');
225
226                 $output = $profiler->getRendertimeString();
227
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);
232                         }
233                 }
234         }
235 }