Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
StatTest.php
Go to the documentation of this file.
1 <?php
9 
10 class StatTest extends \PHPUnit\Framework\TestCase
11 {
15  protected $_stat;
16 
17  protected function setUp()
18  {
19  $this->_stat = new \Magento\Framework\Profiler\Driver\Standard\Stat();
20  }
21 
29  public function testActions(array $actions, array $expected)
30  {
31  foreach ($actions as $actionData) {
32  list($action, $timerId, $time, $realMemory, $emallocMemory) = array_values($actionData);
33  $this->_executeTimerAction($action, $timerId, $time, $realMemory, $emallocMemory);
34  }
35 
36  if (empty($expected)) {
37  $this->fail("\$expected mustn't be empty");
38  }
39 
40  foreach ($expected as $timerId => $expectedTimer) {
41  $actualTimer = $this->_stat->get($timerId);
42  $this->assertInternalType('array', $actualTimer, "Timer '{$timerId}' must be an array");
43  $this->assertEquals($expectedTimer, $actualTimer, "Timer '{$timerId}' has unexpected value");
44  }
45  }
46 
52  public function actionsDataProvider()
53  {
54  return [
55  'Start only once' => [
56  'actions' => [
57  ['start', 'timer1', 'time' => 25, 'realMemory' => 1500, 'emallocMemory' => 10],
58  ],
59  'expected' => [
60  'timer1' => [
61  \Magento\Framework\Profiler\Driver\Standard\Stat::START => 25,
62  \Magento\Framework\Profiler\Driver\Standard\Stat::TIME => 0,
63  \Magento\Framework\Profiler\Driver\Standard\Stat::REALMEM => 0,
64  \Magento\Framework\Profiler\Driver\Standard\Stat::EMALLOC => 0,
65  \Magento\Framework\Profiler\Driver\Standard\Stat::REALMEM_START => 1500,
66  \Magento\Framework\Profiler\Driver\Standard\Stat::EMALLOC_START => 10,
67  \Magento\Framework\Profiler\Driver\Standard\Stat::COUNT => 1,
68  ],
69  ],
70  ],
71  'Start only twice' => [
72  'actions' => [
73  ['start', 'timer1', 'time' => 25, 'realMemory' => 1500, 'emallocMemory' => 10],
74  ['start', 'timer1', 'time' => 75, 'realMemory' => 2000, 'emallocMemory' => 20],
75  ],
76  'expected' => [
77  'timer1' => [
78  \Magento\Framework\Profiler\Driver\Standard\Stat::START => 75,
79  \Magento\Framework\Profiler\Driver\Standard\Stat::TIME => 0,
80  \Magento\Framework\Profiler\Driver\Standard\Stat::REALMEM => 0,
81  \Magento\Framework\Profiler\Driver\Standard\Stat::EMALLOC => 0,
82  \Magento\Framework\Profiler\Driver\Standard\Stat::REALMEM_START => 2000,
83  \Magento\Framework\Profiler\Driver\Standard\Stat::EMALLOC_START => 20,
84  \Magento\Framework\Profiler\Driver\Standard\Stat::COUNT => 2,
85  ],
86  ],
87  ],
88  'Start and stop consequentially' => [
89  'actions' => [
90  ['start', 'timer1', 'time' => 25, 'realMemory' => 1500, 'emallocMemory' => 10],
91  ['stop', 'timer1', 'time' => 75, 'realMemory' => 2000, 'emallocMemory' => 20],
92  ['start', 'timer1', 'time' => 200, 'realMemory' => 3000, 'emallocMemory' => 50],
93  ['stop', 'timer1', 'time' => 250, 'realMemory' => 4000, 'emallocMemory' => 80],
94  ],
95  'expected' => [
96  'timer1' => [
97  \Magento\Framework\Profiler\Driver\Standard\Stat::START => false,
98  \Magento\Framework\Profiler\Driver\Standard\Stat::TIME => 100,
99  \Magento\Framework\Profiler\Driver\Standard\Stat::REALMEM => 1500,
100  \Magento\Framework\Profiler\Driver\Standard\Stat::EMALLOC => 40,
101  \Magento\Framework\Profiler\Driver\Standard\Stat::REALMEM_START => 3000,
102  \Magento\Framework\Profiler\Driver\Standard\Stat::EMALLOC_START => 50,
103  \Magento\Framework\Profiler\Driver\Standard\Stat::COUNT => 2,
104  ],
105  ],
106  ],
107  'Start and stop with inner timer' => [
108  'actions' => [
109  ['start', 'timer1', 'time' => 25, 'realMemory' => 1500, 'emallocMemory' => 10],
110  ['start', 'timer2', 'time' => 50, 'realMemory' => 2000, 'emallocMemory' => 20],
111  ['stop', 'timer2', 'time' => 80, 'realMemory' => 2500, 'emallocMemory' => 25],
112  ['stop', 'timer1', 'time' => 100, 'realMemory' => 4200, 'emallocMemory' => 55],
113  ],
114  'expected' => [
115  'timer1' => [
116  \Magento\Framework\Profiler\Driver\Standard\Stat::START => false,
117  \Magento\Framework\Profiler\Driver\Standard\Stat::TIME => 75,
118  \Magento\Framework\Profiler\Driver\Standard\Stat::REALMEM => 2700,
119  \Magento\Framework\Profiler\Driver\Standard\Stat::EMALLOC => 45,
120  \Magento\Framework\Profiler\Driver\Standard\Stat::REALMEM_START => 1500,
121  \Magento\Framework\Profiler\Driver\Standard\Stat::EMALLOC_START => 10,
122  \Magento\Framework\Profiler\Driver\Standard\Stat::COUNT => 1,
123  ],
124  'timer2' => [
125  \Magento\Framework\Profiler\Driver\Standard\Stat::START => false,
126  \Magento\Framework\Profiler\Driver\Standard\Stat::TIME => 30,
127  \Magento\Framework\Profiler\Driver\Standard\Stat::REALMEM => 500,
128  \Magento\Framework\Profiler\Driver\Standard\Stat::EMALLOC => 5,
129  \Magento\Framework\Profiler\Driver\Standard\Stat::REALMEM_START => 2000,
130  \Magento\Framework\Profiler\Driver\Standard\Stat::EMALLOC_START => 20,
131  \Magento\Framework\Profiler\Driver\Standard\Stat::COUNT => 1,
132  ],
133  ],
134  ]
135  ];
136  }
137 
144  public function testGetWithInvalidTimer()
145  {
146  $this->_stat->get('unknown_timer');
147  }
148 
155  public function testStopWithInvalidTimer()
156  {
157  $this->_stat->stop('unknown_timer', 1, 2, 3);
158  }
159 
163  public function testClear()
164  {
165  $this->_stat->start('timer1', 1, 20, 10);
166  $this->_stat->start('timer2', 2, 20, 10);
167  $this->_stat->start('timer3', 3, 20, 10);
168  $this->assertAttributeCount(3, '_timers', $this->_stat);
169 
170  $this->_stat->clear('timer1');
171  $this->assertAttributeCount(2, '_timers', $this->_stat);
172 
173  $this->_stat->clear();
174  $this->assertAttributeEmpty('_timers', $this->_stat);
175  }
176 
184  public function testTimersSorting($timers, $expectedTimerIds)
185  {
186  foreach ($timers as $timerData) {
187  list($action, $timerId) = $timerData;
188  $this->_executeTimerAction($action, $timerId);
189  }
190 
191  $this->assertEquals($expectedTimerIds, $this->_stat->getFilteredTimerIds());
192  }
193 
197  public function timersSortingDataProvider()
198  {
199  return [
200  'Without sorting' => [
201  'actions' => [
202  ['start', 'root'],
203  ['start', 'root->init'],
204  ['stop', 'root->init'],
205  ['stop', 'root'],
206  ],
207  'expected' => ['root', 'root->init'],
208  ],
209  'Simple sorting' => [
210  'actions' => [
211  ['start', 'root'],
212  ['start', 'root->di'],
213  ['stop', 'root->di'],
214  ['start', 'root->init'],
215  ['start', 'root->init->init_stores'],
216  ['start', 'root->init->init_stores->store_collection_load_after'],
217  ['stop', 'root->init->init_stores->store_collection_load_after'],
218  ['stop', 'root->init->init_stores'],
219  ['stop', 'root->init'],
220  ['start', 'root->dispatch'],
221  ['stop', 'root->dispatch'],
222  ['stop', 'root'],
223  ],
224  'expected' => [
225  'root',
226  'root->di',
227  'root->init',
228  'root->init->init_stores',
229  'root->init->init_stores->store_collection_load_after',
230  'root->dispatch',
231  ],
232  ],
233  'Nested sorting' => [
234  'actions' => [
235  ['start', 'root'],
236  ['start', 'root->init'],
237  ['start', 'root->system'],
238  ['stop', 'root->system'],
239  ['start', 'root->init->init_config'],
240  ['stop', 'root->init->init_config'],
241  ['stop', 'root->init'],
242  ['stop', 'root'],
243  ],
244  'expected' => ['root', 'root->init', 'root->init->init_config', 'root->system'],
245  ]
246  ];
247  }
248 
258  public function testTimersFiltering($timers, $thresholds, $filterPattern, $expectedTimerIds)
259  {
260  foreach ($timers as $timerData) {
261  list($action, $timerId, $time, $realMemory, $emallocMemory) = array_pad(array_values($timerData), 5, 0);
262  $this->_executeTimerAction($action, $timerId, $time, $realMemory, $emallocMemory);
263  }
264 
265  $this->assertEquals($expectedTimerIds, $this->_stat->getFilteredTimerIds($thresholds, $filterPattern));
266  }
267 
271  public function timersFilteringDataProvider()
272  {
273  return [
274  'Filtering by pattern' => [
275  'actions' => [
276  ['start', 'root'],
277  ['start', 'root->init'],
278  ['stop', 'root->init'],
279  ['stop', 'root'],
280  ],
281  'thresholds' => [],
282  'filterPattern' => '/^root$/',
283  'expected' => ['root'],
284  ],
285  'Filtering by thresholds' => [
286  'actions' => [
287  ['start', 'root', 'time' => 0, 'realMemory' => 0, 'emallocMemory' => 0],
288  ['start', 'root->init', 0],
289  ['start', 'root->init->init_cache', 'time' => 50, 'realMemory' => 1000],
290  ['stop', 'root->init->init_cache', 'time' => 100, 'realMemory' => 21000],
291  ['stop', 'root->init', 999],
292  ['stop', 'root', 'time' => 1000, 'realMemory' => 500, 'emallocMemory' => 0],
293  ],
294  'thresholds' => [
295  \Magento\Framework\Profiler\Driver\Standard\Stat::TIME => 1000,
296  \Magento\Framework\Profiler\Driver\Standard\Stat::REALMEM => 20000,
297  ],
298  'filterPattern' => null,
299  // TIME >= 1000, REALMEM >= 20000
300  'expected' => ['root', 'root->init->init_cache'],
301  ]
302  ];
303  }
304 
312  public function testFetch($timers, $expects)
313  {
314  foreach ($timers as $timerData) {
315  list($action, $timerId, $time, $realMemory, $emallocMemory) = array_pad(array_values($timerData), 5, 0);
316  $this->_executeTimerAction($action, $timerId, $time, $realMemory, $emallocMemory);
317  }
318  foreach ($expects as $expectedData) {
320  list($timerId, $key, $expectedValue) = array_values($expectedData);
321  if (!is_scalar($expectedValue)) {
322  $expectedValue->evaluate($this->_stat->fetch($timerId, $key));
323  } else {
324  $this->assertEquals($expectedValue, $this->_stat->fetch($timerId, $key));
325  }
326  }
327  }
328 
332  public function fetchDataProvider()
333  {
334  return [
335  [
336  'actions' => [
337  ['start', 'root', 'time' => 0, 'realMemory' => 0, 'emallocMemory' => 0],
338  ['stop', 'root', 'time' => 1000, 'realMemory' => 500, 'emallocMemory' => 10],
339  ],
340  'expects' => [
341  [
342  'timerId' => 'root',
343  'key' => \Magento\Framework\Profiler\Driver\Standard\Stat::START,
344  'expectedValue' => false,
345  ],
346  [
347  'timerId' => 'root',
348  'key' => \Magento\Framework\Profiler\Driver\Standard\Stat::TIME,
349  'expectedValue' => 1000
350  ],
351  [
352  'timerId' => 'root',
353  'key' => \Magento\Framework\Profiler\Driver\Standard\Stat::REALMEM,
354  'expectedValue' => 500
355  ],
356  [
357  'timerId' => 'root',
358  'key' => \Magento\Framework\Profiler\Driver\Standard\Stat::EMALLOC,
359  'expectedValue' => 10
360  ],
361  ],
362  ],
363  [
364  'actions' => [
365  ['start', 'root', 'time' => 0],
366  ['stop', 'root', 'time' => 10],
367  ['start', 'root', 'time' => 20],
368  ['stop', 'root', 'time' => 30],
369  ],
370  'expects' => [
371  [
372  'timerId' => 'root',
373  'key' => \Magento\Framework\Profiler\Driver\Standard\Stat::AVG,
374  'expectedValue' => 10,
375  ],
376  ]
377  ],
378  [
379  'actions' => [['start', 'root', 'time' => 0]],
380  'expects' => [
381  [
382  'timerId' => 'root',
383  'key' => \Magento\Framework\Profiler\Driver\Standard\Stat::TIME,
384  'expectedValue' => $this->greaterThan(microtime(true)),
385  ],
386  [
387  'timerId' => 'root',
388  'key' => \Magento\Framework\Profiler\Driver\Standard\Stat::ID,
389  'expectedValue' => 'root'
390  ],
391  ]
392  ]
393  ];
394  }
395 
400  public function testFetchInvalidTimer()
401  {
402  $this->_stat->fetch('foo', 'bar');
403  }
404 
409  public function testFetchInvalidKey()
410  {
411  $this->_stat->start('foo', 0, 0, 0);
412  $this->_stat->fetch('foo', 'bar');
413  }
414 
424  protected function _executeTimerAction($action, $timerId, $time = 0, $realMemory = 0, $emallocMemory = 0)
425  {
426  switch ($action) {
427  case 'start':
428  $this->_stat->start($timerId, $time, $realMemory, $emallocMemory);
429  break;
430  case 'stop':
431  $this->_stat->stop($timerId, $time, $realMemory, $emallocMemory);
432  break;
433  default:
434  $this->fail("Unexpected action '{$action}'");
435  break;
436  }
437  }
438 }
_executeTimerAction($action, $timerId, $time=0, $realMemory=0, $emallocMemory=0)
Definition: StatTest.php:424
testTimersFiltering($timers, $thresholds, $filterPattern, $expectedTimerIds)
Definition: StatTest.php:258