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