Passed
Push — master ( 51ac1b...cef3f3 )
by Thomas
09:07
created

src/Benchmark.php (2 issues)

1
<?php
2
3
namespace LeKoala\DevToolkit;
4
5
use LeKoala\Base\Helpers\ClassHelper;
6
use SilverStripe\Core\Injector\Injector;
7
8
class Benchmark
9
{
10
    /**
11
     * @return Monolog\Logger
12
     */
13
    public static function getLogger()
14
    {
15
        $class = array_pop(explode("\\", get_called_class()));
16
        return Injector::inst()->get(LoggerInterface::class)->withName(ClassHelper::getClassWithoutNamespace($class));
17
    }
18
19
    /**
20
     * A dead simple benchmark function
21
     *
22
     * Usage : bm(function() { // Insert here the code to benchmark });
23
     * Alternative usage : bm() ; // Code to test ; bm();
24
     *
25
     * @param callable $cb
26
     * @return void
27
     */
28
    public static function run($cb = null)
29
    {
30
        $data = self::benchmark($cb);
31
        if (!$data) {
0 ignored issues
show
$data is a non-empty array, thus ! $data is always false.
Loading history...
32
            return;
33
        }
34
35
        printf("It took %s seconds and used %s memory", $data['time'], $data['memory']);
36
        die();
37
    }
38
39
    /**
40
     * @param callable $cb
41
     * @return bool|array
42
     */
43
    protected static function benchmark($cb = null)
44
    {
45
        static $data = null;
46
47
        // No callback scenario
48
        if ($cb === null) {
49
            if ($data === null) {
50
                $data = [
51
                    'startTime' => microtime(true),
52
                    'startMemory' => memory_get_usage(),
53
                ];
54
                // Allow another call
55
                return false;
56
            } else {
57
                $startTime = $data['startTime'];
58
                $startMemory = $data['startMemory'];
59
60
                // Clear for future calls
61
                $data = null;
62
            }
63
        } else {
64
            $startTime = microtime(true);
65
            $startMemory = memory_get_usage();
66
67
            $cb();
68
        }
69
70
        $endTime = microtime(true);
71
        $endMemory = memory_get_usage();
72
73
        $time = sprintf("%.6f", $endTime - $startTime);
74
        $memory = self::bytesToHuman($endMemory - $startMemory);
75
76
        return [
77
            'time' => $time,
78
            'memory' => $memory,
79
        ];
80
    }
81
82
    protected static function bytesToHuman($bytes, $decimals = 2)
83
    {
84
        $size   = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
85
        $factor = floor((strlen($bytes) - 1) / 3);
86
87
        return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$size[$factor];
88
    }
89
90
    public static function log($name, $cb = null)
91
    {
92
        $data = self::benchmark($cb);
93
        if (!$data) {
0 ignored issues
show
$data is a non-empty array, thus ! $data is always false.
Loading history...
94
            return;
95
        }
96
97
        $time = $data['time'];
98
        $memory = $data['memory'];
99
100
        self::getLogger()->debug("$name : $time seconds and $memory memory.");
101
    }
102
}
103