summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/tests/phpunit/Benchmark/QueryBenchmarkRunner.php
blob: 2ff457ddcdaa13fd934bfd443f0509bbb423f280 (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php

namespace SMW\Tests\Benchmark;

use RuntimeException;
use SMW\DIProperty;
use SMW\Query\PrintRequest as PrintRequest;
use SMW\Store;
use SMWPropertyValue as PropertyValue;
use SMWQuery as Query;
use SMWQueryParser as QueryParser;
use Title;

/**
 * @group semantic-mediawiki-benchmark
 *
 * @license GNU GPL v2+
 * @since 2.5
 *
 * @author mwjames
 */
class QueryBenchmarkRunner implements BenchmarkReporter {

	/**
	 * @var Store
	 */
	private $store;

	/**
	 * @var QueryParser
	 */
	private $queryParser;

	/**
	 * @var Benchmarker
	 */
	private $benchmarker;

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

	/**
	 * @since 2.5
	 *
	 * @param Store $store
	 * @param QueryParser $queryParser
	 * @param Benchmarker $benchmarker
	 */
	public function __construct( Store $store, QueryParser $queryParser, Benchmarker $benchmarker ) {
		$this->store = $store;
		$this->queryParser = $queryParser;
		$this->benchmarker = $benchmarker;
	}

	/**
	 * @since 2.5
	 *
	 * @param array
	 */
	public function getBenchmarkReport() {
		return $this->benchmarkReport;
	}

	/**
	 * @since 2.5
	 *
	 * @param array $case
	 */
	public function run( array $case ) {

		$this->benchmarkReport = [];
		$this->benchmarker->clear();

		if ( !isset( $case['query'] ) && !is_array( $case['query']  ) ) {
			throw new RuntimeException( 'Query specification is not available.' );
		}

		if ( !isset( $case['repetitionCount'] ) ) {
			throw new RuntimeException( 'repetitionCount is not available.' );
		}

		$start = microtime( true );
		$queryReports = [];

		$queryReports['count'] = $this->doQuery(
			$case, $this->createQuery( $case, Query::MODE_COUNT )
		);

		$queryReports['instance'] = $this->doQuery(
			$case, $this->createQuery( $case, Query::MODE_INSTANCES )
		);

		$this->benchmarkReport = [
			'type'  => $case['type'],
			'note'  => $case['query']['condition'] . ( isset( $case['note'] ) ? ' (' . $case['note'] . ')' : '' ),
			'query' => $queryReports,
			'time'  => microtime( true ) - $start
		];
	}

	private function doQuery( array $case, $query ) {

		$this->benchmarker->clear();

		$memoryBefore = memory_get_peak_usage( false );

		for ( $i = 0; $i < $case['repetitionCount']; $i++ ) {

			$start = microtime( true );

			$queryResult = $this->store->getQueryResult( $query );

			$this->benchmarker->addBenchmarkPoint( microtime( true ) - $start );
		}

		$count = $query->querymode === Query::MODE_COUNT ? $queryResult->getCountValue() : $queryResult->getCount();
		$columnCount = $queryResult->getColumnCount();

		return [
			'rowCount' => $count,
			'columnCount' => $columnCount,
			'repetitionCount' => $case['repetitionCount'],
			"memory" => memory_get_peak_usage( false ) - $memoryBefore,
			"time" => [
				'sum'  => $this->benchmarker->getSum(),
				'mean' => $this->benchmarker->getMean(),
				'sd'   => $this->benchmarker->getStandardDeviation(),
				'norm' => $this->benchmarker->getNormalizedValueBy( $case['repetitionCount'] )
			]
		];
	}

	private function createQuery( array $case, $mode, array $printouts = [] ) {

		$description = $this->queryParser->getQueryDescription(
			$case['query']['condition']
		);

		foreach ( $case['query']['printouts'] as $printout ) {
			$property = DIProperty::newFromUserLabel( $printout );

			$propertyValue = new PropertyValue( '__pro' );
			$propertyValue->setDataItem( $property );

			$description->addPrintRequest(
				new PrintRequest( PrintRequest::PRINT_PROP, null, $propertyValue )
			);
		}

		$query = new Query(
			$description
		);

		$query->setUnboundlimit(
			isset( $case['query']['parameters']['limit'] ) ? $case['query']['parameters']['limit'] : 500
		);

		$query->setOffset(
			isset( $case['query']['parameters']['offset'] ) ? $case['query']['parameters']['offset'] : 0
		);

		$query->setQueryMode( $mode );

		return $query;
	}

}