Completed
Push — master ( c2654a...8b49d6 )
by Alec
21:15 queued 13:44
created

BenchmarkResult::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 4
dl 0
loc 6
ccs 0
cts 5
cp 0
crap 2
rs 10
c 2
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace AlecRabbit\Tools;
4
5
use AlecRabbit\Accessories\Pretty;
6
7
class BenchmarkResult
8
{
9
    /** @var float */
10
    protected $mean;
11
    /** @var float */
12
    protected $delta;
13
    /** @var int */
14
    protected $numberOfMeasurements;
15
    /** @var int */
16
    protected $numberOfRejections;
17
18
    public function __construct(float $mean, float $delta, int $numberOfMeasurements, int $numberOfRejections = 0)
19
    {
20
        $this->mean = $mean;
21
        $this->delta = $delta;
22
        $this->numberOfMeasurements = $numberOfMeasurements;
23
        $this->numberOfRejections = $numberOfRejections;
24
    }
25
26
    public function getDeltaPercent(): float
27
    {
28
        return $this->getDelta() / $this->getMean();
29
    }
30
31
    /**
32
     * @return float
33
     */
34
    public function getDelta(): float
35
    {
36
        return $this->delta;
37
    }
38
39
    /**
40
     * @return float
41
     */
42
    public function getMean(): float
43
    {
44
        return $this->mean;
45
    }
46
47
    public function getRejectionsPercent(): float
48
    {
49
        return $this->getNumberOfRejections() / $this->getNumberOfMeasurements();
50
    }
51
52
    /**
53
     * @return int
54
     */
55
    public function getNumberOfRejections(): int
56
    {
57
        return $this->numberOfRejections;
58
    }
59
60
    /**
61
     * @return int
62
     */
63
    public function getNumberOfMeasurements(): int
64
    {
65
        return $this->numberOfMeasurements;
66
    }
67
68
    public function __toString(): string
69
    {
70
        return
71
            sprintf(
72
                '%s±%s',
73
                Pretty::nanoseconds($this->getMean()),
74
                Pretty::percent($this->getDeltaPercent())
75
            );
76
    }
77
}
78