statistics.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php namespace HashOver;
  2. // Copyright (C) 2010-2017 Jacob Barkdull
  3. // This file is part of HashOver.
  4. //
  5. // HashOver is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as
  7. // published by the Free Software Foundation, either version 3 of the
  8. // License, or (at your option) any later version.
  9. //
  10. // HashOver is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with HashOver. If not, see <http://www.gnu.org/licenses/>.
  17. // Display source code
  18. if (basename ($_SERVER['PHP_SELF']) === basename (__FILE__)) {
  19. if (isset ($_GET['source'])) {
  20. header ('Content-type: text/plain; charset=UTF-8');
  21. exit (file_get_contents (basename (__FILE__)));
  22. } else {
  23. exit ('<b>HashOver</b>: This is a class file.');
  24. }
  25. }
  26. class Statistics
  27. {
  28. public $mode;
  29. public $executionStart;
  30. public $executionEnd;
  31. public $executionTime;
  32. public $scriptMemory;
  33. public $systemMemory;
  34. public function __construct ($mode = 'php')
  35. {
  36. $this->mode = $mode;
  37. }
  38. // Script execution starting time
  39. public function executionStart ()
  40. {
  41. // Start time in seconds
  42. $this->executionStart = microtime (true);
  43. }
  44. // Script execution ending time
  45. public function executionEnd ()
  46. {
  47. // End time in seconds
  48. $this->executionEnd = microtime (true);
  49. // Difference between start time and end time in seconds
  50. $this->executionTime = $this->executionEnd - $this->executionStart;
  51. // Unit to divided memory bytes by (1,024² for mibibytes)
  52. $division_unit = pow (1024, 2);
  53. // Memory the script consumed divided by division unit
  54. $this->scriptMemory = round (memory_get_peak_usage () / $division_unit, 2);
  55. // Memory the system consumed divided by division unit
  56. $this->systemMemory = round (memory_get_peak_usage (true) / $division_unit, 2);
  57. // Display execution time in millisecond(s)
  58. if ($this->executionTime < 1) {
  59. $execution_stat = round ($this->executionTime * 1000, 5) . ' ms';
  60. } else {
  61. // Display execution time in seconds
  62. $execution_stat = round ($this->executionTime, 5) . ' Second';
  63. // Add plural to any execution time other than one
  64. if ($this->executionTime !== 1) {
  65. $execution_stat .= 's';
  66. }
  67. }
  68. // Statistics inner-comment
  69. $statistics = PHP_EOL . PHP_EOL;
  70. $statistics .= "\t" . 'HashOver Statistics:' . PHP_EOL . PHP_EOL;
  71. $statistics .= "\t\t" . 'Execution Time : ' . $execution_stat . PHP_EOL;
  72. $statistics .= "\t\t" . 'Script Memory Peak : ' . $this->scriptMemory . ' MiB' . PHP_EOL;
  73. $statistics .= "\t\t" . 'System Memory Peak : ' . $this->systemMemory . ' MiB';
  74. $statistics .= PHP_EOL . PHP_EOL;
  75. // Return statistics as JavaScript comment
  76. if ($this->mode === 'javascript') {
  77. return PHP_EOL . '/*' . $statistics . '*/';
  78. }
  79. // Return statistics as HTML comment by default
  80. return PHP_EOL . '<!--' . $statistics . '-->';
  81. }
  82. }