PageRenderTime 40ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/www/shop/engine/Shopware/Plugins/Default/Core/BenchmarkEvents/Bootstrap.php

https://bitbucket.org/weberlars/sot-shopware
PHP | 160 lines | 115 code | 13 blank | 32 comment | 13 complexity | fd19757439227da9fee3b45b6e7fa61b MD5 | raw file
Possible License(s): AGPL-3.0, MIT, BSD-3-Clause, LGPL-2.1, LGPL-3.0
  1. <?php
  2. /**
  3. * Shopware 4.0
  4. * Copyright Š 2012 shopware AG
  5. *
  6. * According to our dual licensing model, this program can be used either
  7. * under the terms of the GNU Affero General Public License, version 3,
  8. * or under a proprietary license.
  9. *
  10. * The texts of the GNU Affero General Public License with an additional
  11. * permission and of our proprietary license can be found at and
  12. * in the LICENSE file you have received along with this program.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * "Shopware" is a registered trademark of shopware AG.
  20. * The licensing of the program under the AGPLv3 does not imply a
  21. * trademark license. Therefore any rights, title and interest in
  22. * our trademarks remain entirely with us.
  23. *
  24. * @category Shopware
  25. * @package Shopware_Plugins
  26. * @subpackage BenchmarkEvents
  27. * @copyright Copyright (c) 2012, shopware AG (http://www.shopware.de)
  28. * @version $Id$
  29. * @author $Author$
  30. */
  31. /**
  32. * todo@all: Documentation
  33. */
  34. class Shopware_Plugins_Core_BenchmarkEvents_Bootstrap extends Shopware_Components_Plugin_Bootstrap
  35. {
  36. protected $results = array();
  37. public function install()
  38. {
  39. $this->subscribeEvent(
  40. 'Enlight_Controller_Front_StartDispatch',
  41. 'onStartDispatch'
  42. );
  43. $this->subscribeEvent(
  44. 'Enlight_Controller_Front_DispatchLoopShutdown',
  45. 'onDispatchLoopShutdown'
  46. );
  47. return true;
  48. }
  49. public function onStartDispatch(Enlight_Event_EventArgs $args)
  50. {
  51. if (!Shopware()->Bootstrap()->hasResource('Log')) {
  52. return;
  53. }
  54. Shopware()->Events()->registerSubscriber($this);
  55. }
  56. public function onDispatchLoopShutdown(Enlight_Event_EventArgs $args)
  57. {
  58. if (!Shopware()->Bootstrap()->hasResource('Log')) {
  59. return;
  60. }
  61. $this->logResults();
  62. }
  63. public function logResults()
  64. {
  65. foreach (array_keys($this->results) as $event) {
  66. if (empty($this->results[$event][0])) {
  67. unset($this->results[$event]);
  68. continue;
  69. }
  70. $listeners = array();
  71. foreach (Enlight()->Events()->getListeners($event) as $listener) {
  72. $listener = $listener->getListener();
  73. if ($listener[0] === $this) {
  74. continue;
  75. }
  76. if (is_array($listener) && is_object($listener[0])) {
  77. $listener[0] = get_class($listener[0]);
  78. }
  79. if (is_array($listener)) {
  80. $listener = implode('::', $listener);
  81. }
  82. $listeners[] = $listener;
  83. }
  84. $this->results[$event] = array(
  85. 0 => $event,
  86. 1 => $this->formatMemory(0 - $this->results[$event][1]),
  87. 2 => $this->formatTime(0 - $this->results[$event][2]),
  88. 3 => $listeners
  89. );
  90. }
  91. $this->results = array_values($this->results);
  92. foreach ($this->results as $result) {
  93. $order[] = $result[2];
  94. }
  95. array_multisort($order, SORT_NUMERIC, SORT_DESC, $this->results);
  96. array_unshift($this->results, array('name', 'memory', 'time', 'listeners'));
  97. $label = 'Benchmark Events';
  98. $table = array($label,
  99. $this->results
  100. );
  101. Shopware()->Log()->table($table);
  102. }
  103. public function onBenchmarkEvent(Enlight_Event_EventArgs $args)
  104. {
  105. $event = $args->getName();
  106. if (!isset($this->results[$event])) {
  107. $this->results[$event] = array(
  108. 0 => true,
  109. 1 => 0,
  110. 2 => 0
  111. );
  112. }
  113. if (empty($this->results[$event][0])) {
  114. $this->results[$event][0] = true;
  115. $this->results[$event][1] -= memory_get_peak_usage(true);
  116. $this->results[$event][2] -= microtime(true);
  117. } else {
  118. $this->results[$event][0] = false;
  119. $this->results[$event][1] += memory_get_peak_usage(true);
  120. $this->results[$event][2] += microtime(true);
  121. }
  122. return $args->getReturn();
  123. }
  124. public function getListeners()
  125. {
  126. $events = Shopware()->Events()->getEvents();
  127. $event_handlers = array();
  128. foreach ($events as $event) {
  129. if ($event == 'Enlight_Controller_Front_DispatchLoopShutdown') {
  130. continue;
  131. }
  132. $event_handlers[] = new Enlight_Event_Handler_Default($event, array($this, 'onBenchmarkEvent'), -1000);
  133. $event_handlers[] = new Enlight_Event_Handler_Default($event, array($this, 'onBenchmarkEvent'), 1000);
  134. }
  135. return $event_handlers;
  136. }
  137. public function formatMemory($size)
  138. {
  139. if (empty($size)) return '0.00 b';
  140. $unit = array('b', 'kb', 'mb', 'gb', 'tb', 'pb');
  141. return @number_format($size / pow(1024, ($i = floor(log($size, 1024)))), 2, '.', '') . ' ' . $unit[$i];
  142. }
  143. public function formatTime($time)
  144. {
  145. return number_format($time, 5, '.', '');
  146. }
  147. }