Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Timer.php « core - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 731651ccbf6b8e2dd12cc38b5a3aad7ccae29129 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
/**
 * Matomo - free/libre analytics platform
 *
 * @link https://matomo.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 */
namespace Piwik;

use Piwik\Metrics\Formatter;

/**
 *
 */
class Timer
{
    private $timerStart;
    private $memoryStart;
    private $formatter;
    private $timerEnd;

    /**
     * @return \Piwik\Timer
     */
    public function __construct()
    {
        $this->formatter = new Formatter();

        $this->init();
    }

    /**
     * @return void
     */
    public function init()
    {
        $this->timerStart = $this->getMicrotime();
        $this->memoryStart = $this->getMemoryUsage();
    }

    public function finish()
    {
        $this->timerEnd = $this->getMicrotime();
    }

    /**
     * @param int $decimals
     * @return string
     */
    public function getTime($decimals = 3)
    {
        return number_format($this->getTimerEnd() - $this->timerStart, $decimals, '.', '');
    }

    /**
     * @param int $decimals
     * @return string
     */
    public function getTimeMs($decimals = 3)
    {
        return number_format(1000 * ($this->getTimerEnd() - $this->timerStart), $decimals, '.', '');
    }

    /**
     * @return string
     */
    public function getMemoryLeak()
    {
        return "Memory delta: " . $this->getMemoryLeakValue();
    }

    /**
     * @return string
     */
    public function getMemoryLeakValue()
    {
        return $this->formatter->getPrettySizeFromBytes($this->getMemoryUsage() - $this->memoryStart);
    }

    /**
     * @return string
     */
    public function getPeakMemoryValue()
    {
        return $this->formatter->getPrettySizeFromBytes($this->getPeakMemoryUsage());
    }

    /**
     * @return string
     */
    public function __toString()
    {
        return "Time elapsed: " . $this->getTime() . "s";
    }

    private function getTimerEnd()
    {
        return $this->timerEnd ?: $this->getMicrotime();
    }

    /**
     * @return float
     */
    private function getMicrotime()
    {
        list($micro_seconds, $seconds) = explode(" ", microtime());
        return ((float)$micro_seconds + (float)$seconds);
    }

    /**
     * Returns current memory usage, if available
     *
     * @return int
     */
    private function getMemoryUsage()
    {
        if (function_exists('memory_get_usage')) {
            return memory_get_usage();
        }
        return 0;
    }

    public function getPeakMemoryUsage()
    {
        if (function_exists('memory_get_peak_usage')) {
            return memory_get_peak_usage();
        }
        return 0;
    }
}