summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/tests/phpunit/Benchmark/PageContentCopyBenchmarkRunner.php
blob: 57154279bd3e7a6275d9828f8eac3e726037c151 (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
<?php

namespace SMW\Tests\Benchmark;

use RuntimeException;
use SMW\Tests\Utils\PageCreator;
use SMW\Tests\Utils\PageReader;
use Title;

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

	/**
	 * @var PageImportBenchmarkRunner
	 */
	private $pageImportBenchmarkRunner;

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

	/**
	 * @var PageCreator
	 */
	private $pageCreator;

	/**
	 * @var PageReader
	 */
	private $pageReader;

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

	/**
	 * @var integer|count
	 */
	private $copyCount = null;

	/**
	 * @since 2.5
	 *
	 * @param PageImportBenchmarkRunner $pageImportBenchmarkRunner
	 * @param Benchmarker $benchmarker
	 * @param PageCreator $pageCreator
	 * @param PageReader $pageReader
	 */
	public function __construct( PageImportBenchmarkRunner $pageImportBenchmarkRunner, Benchmarker $benchmarker, PageCreator $pageCreator, PageReader $pageReader ) {
		$this->pageImportBenchmarkRunner = $pageImportBenchmarkRunner;
		$this->benchmarker = $benchmarker;
		$this->pageCreator = $pageCreator;
		$this->pageReader = $pageReader;
	}

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

	/**
	 * @since 2.5
	 *
	 * @param integer|null $copyCount
	 */
	public function setCopyCount( $copyCount = null ) {
		$this->copyCount = $copyCount;
	}

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

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

		$this->pageImportBenchmarkRunner->run( $case );
		$importBenchmarkReport = $this->pageImportBenchmarkRunner->getBenchmarkReport();

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

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

		$copyFrom = Title::newFromText( $case['copyFrom'] );

		if ( !$copyFrom->exists() ) {
			throw new RuntimeException( $case['copyFrom'] . ' is not available or readable for the copy process.' );
		}

		if ( !$this->canOverrideCount( $case ) ) {
			$this->copyCount = $case['copyCount'];
		}

		$copyMemory = $this->doCopy( $copyFrom, $case );

		$this->benchmarkReport = [
			'type'   => $case['type'],
			'source' => $case['name'],
			'import' => [
				'memory' => $importBenchmarkReport['memory'],
				'time'   => $importBenchmarkReport['time']
			],
			'copy' => [
				'copyFrom'  => $case['copyFrom'],
				'copyCount' => $this->copyCount,
				'memory'    => $copyMemory,
				'time'      => [
					'sum'  => $this->benchmarker->getSum(),
					'mean' => $this->benchmarker->getMean(),
					'sd'   => $this->benchmarker->getStandardDeviation(),
					'norm' => $this->benchmarker->getNormalizedValueBy( $this->copyCount )
				]
			],
			'time' => microtime( true ) - $start
		];
	}

	private function doCopy( $copyFrom, array $case ) {

		$copyText = $this->pageReader->getContentAsText( $copyFrom );
		$copyName = 'BenchmarkCopy-' . $copyFrom->getText();

		$memoryBefore = memory_get_peak_usage( false );

		for ( $i = 0; $i < $this->copyCount; $i++ ) {

			$start = microtime( true );

			$this->pageCreator->createPage(
				Title::newFromText( $copyName . '-' . $i ),
				$copyText
			);

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

		return memory_get_peak_usage( false ) - $memoryBefore;
	}

	private function canOverrideCount( $case ) {
		return isset( $case['canOverrideCopyCount'] ) && $case['canOverrideCopyCount'] && $this->copyCount !== null;
	}
}