10 class StatTest extends \PHPUnit\Framework\TestCase
19 $this->_stat = new \Magento\Framework\Profiler\Driver\Standard\Stat();
31 foreach ($actions as $actionData) {
32 list($action, $timerId, $time, $realMemory, $emallocMemory) = array_values($actionData);
36 if (empty($expected)) {
37 $this->fail(
"\$expected mustn't be empty");
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");
55 'Start only once' => [
57 [
'start',
'timer1',
'time' => 25,
'realMemory' => 1500,
'emallocMemory' => 10],
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,
71 'Start only twice' => [
73 [
'start',
'timer1',
'time' => 25,
'realMemory' => 1500,
'emallocMemory' => 10],
74 [
'start',
'timer1',
'time' => 75,
'realMemory' => 2000,
'emallocMemory' => 20],
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,
88 'Start and stop consequentially' => [
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],
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,
107 'Start and stop with inner timer' => [
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],
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,
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,
146 $this->_stat->get(
'unknown_timer');
157 $this->_stat->stop(
'unknown_timer', 1, 2, 3);
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);
170 $this->_stat->clear(
'timer1');
171 $this->assertAttributeCount(2,
'_timers', $this->_stat);
173 $this->_stat->clear();
174 $this->assertAttributeEmpty(
'_timers', $this->_stat);
186 foreach ($timers as $timerData) {
187 list($action, $timerId) = $timerData;
191 $this->assertEquals($expectedTimerIds, $this->_stat->getFilteredTimerIds());
200 'Without sorting' => [
203 [
'start',
'root->init'],
204 [
'stop',
'root->init'],
207 'expected' => [
'root',
'root->init'],
209 'Simple sorting' => [
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'],
228 'root->init->init_stores',
229 'root->init->init_stores->store_collection_load_after',
233 'Nested sorting' => [
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'],
244 'expected' => [
'root',
'root->init',
'root->init->init_config',
'root->system'],
260 foreach ($timers as $timerData) {
261 list($action, $timerId, $time, $realMemory, $emallocMemory) = array_pad(array_values($timerData), 5, 0);
265 $this->assertEquals($expectedTimerIds, $this->_stat->getFilteredTimerIds($thresholds, $filterPattern));
274 'Filtering by pattern' => [
277 [
'start',
'root->init'],
278 [
'stop',
'root->init'],
282 'filterPattern' =>
'/^root$/',
283 'expected' => [
'root'],
285 'Filtering by thresholds' => [
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],
295 \Magento\Framework\Profiler\Driver\Standard\Stat::TIME => 1000,
296 \Magento\Framework\Profiler\Driver\Standard\Stat::REALMEM => 20000,
298 'filterPattern' =>
null,
300 'expected' => [
'root',
'root->init->init_cache'],
312 public function testFetch($timers, $expects)
314 foreach ($timers as $timerData) {
315 list($action, $timerId, $time, $realMemory, $emallocMemory) = array_pad(array_values($timerData), 5, 0);
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));
324 $this->assertEquals($expectedValue, $this->_stat->fetch($timerId, $key));
337 [
'start',
'root',
'time' => 0,
'realMemory' => 0,
'emallocMemory' => 0],
338 [
'stop',
'root',
'time' => 1000,
'realMemory' => 500,
'emallocMemory' => 10],
343 'key' => \Magento\Framework\Profiler\Driver\Standard\Stat::START,
344 'expectedValue' =>
false,
348 'key' => \Magento\Framework\Profiler\Driver\Standard\Stat::TIME,
349 'expectedValue' => 1000
353 'key' => \Magento\Framework\Profiler\Driver\Standard\Stat::REALMEM,
354 'expectedValue' => 500
358 'key' => \Magento\Framework\Profiler\Driver\Standard\Stat::EMALLOC,
359 'expectedValue' => 10
365 [
'start',
'root',
'time' => 0],
366 [
'stop',
'root',
'time' => 10],
367 [
'start',
'root',
'time' => 20],
368 [
'stop',
'root',
'time' => 30],
373 'key' => \Magento\Framework\Profiler\Driver\Standard\Stat::AVG,
374 'expectedValue' => 10,
379 'actions' => [[
'start',
'root',
'time' => 0]],
383 'key' => \Magento\Framework\Profiler\Driver\Standard\Stat::TIME,
384 'expectedValue' => $this->greaterThan(microtime(
true)),
388 'key' => \Magento\Framework\Profiler\Driver\Standard\Stat::ID,
389 'expectedValue' =>
'root' 402 $this->_stat->fetch(
'foo',
'bar');
411 $this->_stat->start(
'foo', 0, 0, 0);
412 $this->_stat->fetch(
'foo',
'bar');
428 $this->_stat->start($timerId, $time, $realMemory, $emallocMemory);
431 $this->_stat->stop($timerId, $time, $realMemory, $emallocMemory);
434 $this->fail(
"Unexpected action '{$action}'");
testStopWithInvalidTimer()
timersSortingDataProvider()
_executeTimerAction($action, $timerId, $time=0, $realMemory=0, $emallocMemory=0)
testTimersSorting($timers, $expectedTimerIds)
testGetWithInvalidTimer()
testTimersFiltering($timers, $thresholds, $filterPattern, $expectedTimerIds)
testActions(array $actions, array $expected)
timersFilteringDataProvider()