aboutsummaryrefslogtreecommitdiff
path: root/bootstrap/comments/backend/classes/statistics.php
blob: e08d0de17d5edff4bf3c898e0803fbab9d1da406 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php namespace HashOver;

// Copyright (C) 2010-2018 Jacob Barkdull
// This file is part of HashOver.
//
// HashOver is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// HashOver is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with HashOver.  If not, see <http://www.gnu.org/licenses/>.


class Statistics
{
	public $mode;
	public $executionStart;
	public $executionEnd;
	public $executionMicroTime;
	public $executionTime;
	public $scriptMemory;
	public $systemMemory;

	public function __construct ($mode = 'php')
	{
		$this->mode = $mode;
	}

	// Script execution starting time
	public function executionStart ()
	{
		// Start time in seconds
		$this->executionStart = microtime (true);
	}

	// Script execution ending time
	public function executionEnd ()
	{
		// End time in seconds
		$this->executionEnd = microtime (true);

		// Difference between start time and end time in seconds
		$this->executionMicroTime = $this->executionEnd - $this->executionStart;

		// Unit to divided memory bytes by (1,024² for mibibytes)
		$division_unit = pow (1024, 2);

		// Memory the script consumed divided by division unit
		$script_memory = round (memory_get_peak_usage () / $division_unit, 2);
		$this->scriptMemory = $script_memory . ' MiB';

		// Memory the system consumed divided by division unit
		$system_memory = round (memory_get_peak_usage (true) / $division_unit, 2);
		$this->systemMemory = $system_memory . ' MiB';

		// Display execution time in millisecond(s)
		if ($this->executionMicroTime < 1) {
			$this->executionTime = round ($this->executionMicroTime * 1000, 5) . ' ms';
		} else {
			// Display execution time in seconds
			$this->executionTime = round ($this->executionMicroTime, 5) . ' Second';

			// Add plural to any execution time other than one
			if ($this->executionMicroTime !== 1) {
				$this->executionTime .= 's';
			}
		}

		// Statistics inner-comment
		$statistics  = PHP_EOL . PHP_EOL;
		$statistics .= "\t" . 'HashOver Statistics' . PHP_EOL . PHP_EOL;
		$statistics .= "\t" . 'Execution Time     : ' . $this->executionTime . PHP_EOL;
		$statistics .= "\t" . 'Script Memory Peak : ' . $this->scriptMemory . PHP_EOL;
		$statistics .= "\t" . 'System Memory Peak : ' . $this->systemMemory;
		$statistics .= PHP_EOL . PHP_EOL;

		// Return statistics as JavaScript comment
		if ($this->mode !== 'php') {
			return PHP_EOL . '/*' . $statistics . '*/';
		}

		// Return statistics as HTML comment by default
		return PHP_EOL . '<!--' . $statistics . '-->';
	}
}