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