summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/tests/phpunit/Benchmark/Benchmarker.php
blob: 41f4c9a01b4dc0ee78de4cb0c122273880425a58 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php

namespace SMW\Tests\Benchmark;

/**
 * @license GNU GPL v2+
 * @since 2.1
 *
 * @author mwjames
 */
class Benchmarker {

	/**
	 * @var array
	 */
	private $container = [];

	/**
	 * @var boolean
	 */
	private $useAsSample = false;

	/**
	 * @var integer
	 */
	private $roundFactor;

	/**
	 * @since 2.1
	 */
	public function __construct( $roundFactor = 7 ) {
		$this->roundFactor = $roundFactor;
	}

	/**
	 * @since 2.1
	 */
	public function clear() {
		$this->container = [];
		return $this;
	}

	/**
	 * @since 2.1
	 *
	 * @param boalean $state false when takling about the entire population;
	 * true when it is a sample (a selection from a population)
	 */
	public function useAsSample( $state = true ) {
		$this->useAsSample = $state;
		return $this;
	}

	/**
	 * @since 2.1
	 *
	 * @param integer $roundFactor
	 */
	public function roundBy( $roundFactor ) {
		$this->roundFactor = $roundFactor;
		return $this;
	}

	/**
	 * @since 2.1
	 *
	 * @param integer $point
	 * @param integer $decimals
	 */
	public function addBenchmarkPoint( $point, $decimals = 10 ) {
		$this->container[] = number_format( $point, $decimals );
	}

	/**
	 * @since 2.1
	 *
	 * @param integer[] $poins
	 */
	public function addBenchmarkPoints( array $poins ) {
		$this->container = $poins;
	}

	/**
	 * @since 2.1
	 *
	 * @return integer
	 */
	public function getSum() {
		return $this->round( array_sum( $this->container ) );
	}

	/**
	 * @since 2.1
	 *
	 * @return integer
	 */
	public function getMean() {
		return $this->round( $this->getSum() / count( $this->container ) );
	}

	/**
	 * @since 2.1
	 *
	 * @return integer
	 */
	public function getVariance() {

		$mean  = $this->getMean();
		$count = count( $this->container );

		$sumOfSquares = 0;

		foreach ( $this->container as $value ) {
			$sumOfSquares += pow( $value - $mean, 2 );
		}

		return $sumOfSquares / ( $this->useAsSample ? $count - 1 : $count );
	}

	/**
	 * @since 2.1
	 *
	 * @return integer
	 */
	public function getStandardDeviation() {
		return $this->round( (float)sqrt( $this->getVariance() ) );
	}

	/**
	 * @since 2.1
	 *
	 * @param integer $basis
	 *
	 * @return integer
	 */
	public function getStandardScoreBy( $basis = 0 ) {
		return $this->round( ( $basis - $this->getMean() ) / $this->getStandardDeviation() );
	}

	/**
	 * @since 2.1
	 *
	 * @param integer $basis
	 *
	 * @return integer
	 */
	public function getNormalizedValueBy( $basis = 1 ) {
		return $this->round( $this->getMean() / $basis );
	}

	private function round( $value ) {
		return round( $value, $this->roundFactor );
	}

}