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

namespace SMW\Tests\Benchmark;

use RuntimeException;
use SMW\Tests\Utils\Runners\XmlImportRunner;

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

	/**
	 * @var XmlImportRunner
	 */
	private $xmlImportRunner;

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

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

	/**
	 * @var string
	 */
	private $testCaseLocation;

	/**
	 * @since 2.5
	 *
	 * @param XmlImportRunner $xmlImportRunner
	 * @param Benchmarker $benchmarker
	 */
	public function __construct( XmlImportRunner $xmlImportRunner, Benchmarker $benchmarker ) {
		$this->xmlImportRunner = $xmlImportRunner;
		$this->benchmarker = $benchmarker;
	}

	/**
	 * @since 2.5
	 *
	 * @param string $testCaseLocation
	 */
	public function setTestCaseLocation( $testCaseLocation ) {
		$this->testCaseLocation = $testCaseLocation;
	}

	/**
	 * @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['importFrom'] ) ) {
			throw new RuntimeException( 'No import file is available.' );
		}

		$file = $this->testCaseLocation . $case['importFrom'];

		if ( !is_readable( $file ) ) {
			throw new RuntimeException( $file  . ' as import file is not available.' );
		}

		$ext = pathinfo( $file, PATHINFO_EXTENSION );

		switch ( $ext ) {
			case 'xml':
				return $this->doXmlImport( $file, $case );
				break;
			default:
				# code...
				break;
		}
	}

	private function doXmlImport( $file, array $case ) {

		$this->xmlImportRunner->setFile( $file );
		$this->xmlImportRunner->setVerbose( true );

		$memoryBefore = memory_get_peak_usage( false );

		if ( !$this->xmlImportRunner->run() ) {
			$this->xmlImportRunner->reportFailedImport();
		}

		$this->benchmarkReport = [
			'type'   => $case['type'],
			'source' => $case['name'],
			'memory' => memory_get_peak_usage( false ) - $memoryBefore,
			'time'   => [
				'sum' => $this->xmlImportRunner->getElapsedImportTimeInSeconds()
			]
		];
	}

}