Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes
StatTest Class Reference
Inheritance diagram for StatTest:

Public Member Functions

 testActions (array $actions, array $expected)
 
 actionsDataProvider ()
 
 testGetWithInvalidTimer ()
 
 testStopWithInvalidTimer ()
 
 testClear ()
 
 testTimersSorting ($timers, $expectedTimerIds)
 
 timersSortingDataProvider ()
 
 testTimersFiltering ($timers, $thresholds, $filterPattern, $expectedTimerIds)
 
 timersFilteringDataProvider ()
 
 fetchDataProvider ()
 
 testFetchInvalidTimer ()
 
 testFetchInvalidKey ()
 

Protected Member Functions

 setUp ()
 
 _executeTimerAction ($action, $timerId, $time=0, $realMemory=0, $emallocMemory=0)
 

Protected Attributes

 $_stat
 

Detailed Description

Definition at line 10 of file StatTest.php.

Member Function Documentation

◆ _executeTimerAction()

_executeTimerAction (   $action,
  $timerId,
  $time = 0,
  $realMemory = 0,
  $emallocMemory = 0 
)
protected

Executes stop or start methods on $_stat object

Parameters
string$action
string$timerId
int$time
int$realMemory
int$emallocMemory

Definition at line 424 of file StatTest.php.

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  }

◆ actionsDataProvider()

actionsDataProvider ( )

Data provider for testActions

Returns
array

Definition at line 52 of file StatTest.php.

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  }

◆ fetchDataProvider()

fetchDataProvider ( )
Returns
array

Definition at line 332 of file StatTest.php.

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  }

◆ setUp()

setUp ( )
protected

Definition at line 17 of file StatTest.php.

18  {
19  $this->_stat = new \Magento\Framework\Profiler\Driver\Standard\Stat();
20  }

◆ testActions()

testActions ( array  $actions,
array  $expected 
)

Test start and stop methods of \Magento\Framework\Profiler\Driver\Standard\Stat

@dataProvider actionsDataProvider

Parameters
array$actions
array$expected

Definition at line 29 of file StatTest.php.

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  }
_executeTimerAction($action, $timerId, $time=0, $realMemory=0, $emallocMemory=0)
Definition: StatTest.php:424

◆ testClear()

testClear ( )

Test clear method

Definition at line 163 of file StatTest.php.

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  }

◆ testFetchInvalidKey()

testFetchInvalidKey ( )

@expectedException \InvalidArgumentException @expectedMessage Timer "foo" doesn't have value for "bar".

Definition at line 409 of file StatTest.php.

410  {
411  $this->_stat->start('foo', 0, 0, 0);
412  $this->_stat->fetch('foo', 'bar');
413  }

◆ testFetchInvalidTimer()

testFetchInvalidTimer ( )

@expectedException \InvalidArgumentException @expectedMessage Timer "foo" doesn't exist.

Definition at line 400 of file StatTest.php.

401  {
402  $this->_stat->fetch('foo', 'bar');
403  }

◆ testGetWithInvalidTimer()

testGetWithInvalidTimer ( )

Test get method with invalid timer id

@expectedException \InvalidArgumentException @expectedExceptionMessage Timer "unknown_timer" doesn't exist.

Definition at line 144 of file StatTest.php.

145  {
146  $this->_stat->get('unknown_timer');
147  }

◆ testStopWithInvalidTimer()

testStopWithInvalidTimer ( )

Test stop method with invalid timer id

@expectedException \InvalidArgumentException @expectedExceptionMessage Timer "unknown_timer" doesn't exist.

Definition at line 155 of file StatTest.php.

156  {
157  $this->_stat->stop('unknown_timer', 1, 2, 3);
158  }

◆ testTimersFiltering()

testTimersFiltering (   $timers,
  $thresholds,
  $filterPattern,
  $expectedTimerIds 
)

Test getFilteredTimerIds for filtering

@dataProvider timersFilteringDataProvider

Parameters
array$timers
array$thresholds
string$filterPattern
array$expectedTimerIds

Definition at line 258 of file StatTest.php.

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  }
_executeTimerAction($action, $timerId, $time=0, $realMemory=0, $emallocMemory=0)
Definition: StatTest.php:424

◆ testTimersSorting()

testTimersSorting (   $timers,
  $expectedTimerIds 
)

Test getFilteredTimerIds for sorting

@dataProvider timersSortingDataProvider

Parameters
array$timers
array$expectedTimerIds

Definition at line 184 of file StatTest.php.

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  }
_executeTimerAction($action, $timerId, $time=0, $realMemory=0, $emallocMemory=0)
Definition: StatTest.php:424

◆ timersFilteringDataProvider()

timersFilteringDataProvider ( )
Returns
array

Definition at line 271 of file StatTest.php.

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  }

◆ timersSortingDataProvider()

timersSortingDataProvider ( )
Returns
array

Definition at line 197 of file StatTest.php.

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  }

Field Documentation

◆ $_stat

$_stat
protected

Definition at line 15 of file StatTest.php.


The documentation for this class was generated from the following file: