Skip to content

Commit d69c4d9

Browse files
committed
injecting benchmarking
1 parent 18e5f52 commit d69c4d9

File tree

6 files changed

+100
-5
lines changed

6 files changed

+100
-5
lines changed

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,48 @@ PHPUnit is a programmer-oriented testing framework for PHP. It is an instance of
66
[![Minimum PHP Version](https://img.shields.io/badge/php-%3E%3D%205.3.3-8892BF.svg?style=flat-square)](https://php.net/)
77
[![Build Status](https://img.shields.io/travis/sebastianbergmann/phpunit/4.8.svg?style=flat-square)](https://phpunit.de/build-status.html)
88

9+
## This fork
10+
11+
This version of PHPUnit is patched to allow benchmarking mode with PHPUnit tests as a drop-in replacement of standard PHPUnit.
12+
13+
Benchmark is controlled by [`phpunit-bench.json`](https://github.com/phperf/phpunit-bench-data/blob/master/src/Config.php) file in current working dir.
14+
15+
Example:
16+
```
17+
{
18+
"calcPerformanceIndex": true,
19+
"defaultTestConfig": {
20+
"skipBenchmark": true,
21+
"numIterations": 500
22+
},
23+
"testConfigs":[
24+
{
25+
"testCaseNameRegex": "UserTest$",
26+
"forceBenchmark": true,
27+
"numIterations": 100
28+
},
29+
{
30+
"testNameRegex": "testSomething",
31+
"forceBenchmark": true,
32+
"numIterations": 1000
33+
},
34+
{
35+
"testCaseNameRegex": "MathTest",
36+
"dataNameRegex": "default",
37+
"forceBenchmark": true,
38+
"numIterations": 10000
39+
},
40+
]
41+
}
42+
```
43+
44+
## Installation
45+
46+
Download `PHAR` at releases page.
47+
48+
49+
# Original README:
50+
951
## Installation
1052

1153
We distribute a [PHP Archive (PHAR)](https://php.net/phar) that has all required (as well as some optional) dependencies of PHPUnit bundled in a single file:

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,12 @@
3838
"ext-json": "*",
3939
"ext-pcre": "*",
4040
"ext-reflection": "*",
41-
"ext-spl": "*"
41+
"ext-spl": "*",
42+
"phperf/phpunit-bench-data": "*|^0.0.1"
4243
},
4344
"config": {
4445
"platform": {
45-
"php": "5.3.3"
46+
"php": "5.4.0"
4647
}
4748
},
4849
"suggest": {

src/Framework/TestCase.php

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@
6464
*/
6565
abstract class PHPUnit_Framework_TestCase extends PHPUnit_Framework_Assert implements PHPUnit_Framework_Test, PHPUnit_Framework_SelfDescribing
6666
{
67+
/** @var \PHPUnitBenchmarkData\Suite */
68+
public $benchmarkSuite;
69+
6770
/**
6871
* Enable or disable the backup and restoration of the $GLOBALS array.
6972
* Overwrite this attribute in a child class of TestCase.
@@ -900,19 +903,27 @@ protected function runTest()
900903
$method = $class->getMethod($this->name);
901904
} catch (ReflectionException $e) {
902905
$this->fail($e->getMessage());
906+
return null;
903907
}
904908

909+
$args = array_merge($this->data, $this->dependencyInput);
905910
try {
906911
$testResult = $method->invokeArgs(
907912
$this,
908-
array_merge($this->data, $this->dependencyInput)
913+
$args
909914
);
910915
} catch (Throwable $_e) {
911916
$e = $_e;
912917
} catch (Exception $_e) {
913918
$e = $_e;
914919
}
915920

921+
$numIterations = $this->benchmarkSuite->getNumIterations(get_class($this), $this->name, $this->dataName);
922+
if ($numIterations > 1) {
923+
$result = new \PHPUnitBenchmarkData\Result($this->dataName, null, null);
924+
$this->benchmarkSuite->addResult($result, $this->name, get_class($this));
925+
}
926+
916927
if (isset($e)) {
917928
$checkException = false;
918929

@@ -969,7 +980,24 @@ protected function runTest()
969980
);
970981
}
971982

972-
return;
983+
if ($numIterations > 1) {
984+
$start = microtime(1);
985+
for ($i = 0; $i < $numIterations - 1; ++$i) {
986+
try {
987+
$method->invokeArgs(
988+
$this,
989+
$args
990+
);
991+
} catch (Throwable $_e) {
992+
} catch (Exception $_e) {
993+
}
994+
}
995+
$timeSpent = microtime(1) - $start;
996+
$result = new \PHPUnitBenchmarkData\Result($this->dataName, $timeSpent, $numIterations);
997+
$this->benchmarkSuite->addResult($result, $this->name, get_class($this));
998+
}
999+
1000+
return null;
9731001
} else {
9741002
throw $e;
9751003
}
@@ -984,6 +1012,19 @@ protected function runTest()
9841012
);
9851013
}
9861014

1015+
if ($numIterations > 1) {
1016+
$start = microtime(1);
1017+
for ($i = 0; $i < $numIterations - 1; ++$i) {
1018+
$method->invokeArgs(
1019+
$this,
1020+
$args
1021+
);
1022+
}
1023+
$timeSpent = microtime(1) - $start;
1024+
$result = new \PHPUnitBenchmarkData\Result($this->dataName, $timeSpent, $numIterations);
1025+
$this->benchmarkSuite->addResult($result, $this->name, get_class($this));
1026+
}
1027+
9871028
return $testResult;
9881029
}
9891030

src/Framework/TestResult.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ class PHPUnit_Framework_TestResult implements Countable
7272
*/
7373
protected $codeCoverage;
7474

75+
/** @var \PHPUnitBenchmarkData\Suite */
76+
public $benchmarkSuite;
77+
7578
/**
7679
* @var bool
7780
*/
@@ -609,6 +612,12 @@ public function run(PHPUnit_Framework_Test $test)
609612
$invoker = new PHP_Invoker;
610613
$invoker->invoke(array($test, 'runBare'), array(), $_timeout);
611614
} else {
615+
if (null === $this->benchmarkSuite) {
616+
$this->benchmarkSuite = new \PHPUnitBenchmarkData\Suite();
617+
}
618+
619+
$test->benchmarkSuite = $this->benchmarkSuite;
620+
612621
$test->runBare();
613622
}
614623
} catch (PHPUnit_Framework_AssertionFailedError $e) {

src/TextUI/TestRunner.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,7 @@ class_exists($arguments['printer'], false)) {
441441

442442
unset($suite);
443443
$result->flushListeners();
444+
$result->benchmarkSuite->save();
444445

445446
if ($this->printer instanceof PHPUnit_TextUI_ResultPrinter) {
446447
$this->printer->printResult($result);

src/Util/Getopt.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ public static function getopt(array $args, $short_options, $long_options = null)
3535
reset($args);
3636
array_map('trim', $args);
3737

38-
while (list($i, $arg) = each($args)) {
38+
foreach ($args as $i => $arg) {
39+
//while (list($i, $arg) = each($args)) {
3940
if ($arg == '') {
4041
continue;
4142
}

0 commit comments

Comments
 (0)