Skip to content

Commit 5857fab

Browse files
committed
initial
0 parents  commit 5857fab

File tree

11 files changed

+574
-0
lines changed

11 files changed

+574
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea
2+
benchmark*.json
3+
vendor

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# PHPUnit benchmark data structures

composer.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "phperf/phpunit-bench-data",
3+
"require": {
4+
"swaggest/json-schema": "*|^0.10"
5+
},
6+
"autoload": {
7+
"psr-4": {
8+
"PHPUnitBenchmarkData\\": "src/"
9+
}
10+
}
11+
}

composer.lock

Lines changed: 149 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Config.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace PHPUnitBenchmarkData;
4+
5+
use Swaggest\JsonSchema\Constraint\Properties;
6+
use Swaggest\JsonSchema\Schema;
7+
use Swaggest\JsonSchema\Structure\ClassStructure;
8+
9+
class Config extends ClassStructure
10+
{
11+
public $calcPerformanceIndex;
12+
13+
public $resultPrecision;
14+
public $resultFilename;
15+
16+
/** @var TestConfig */
17+
public $defaultTestConfig;
18+
19+
/** @var TestConfig[] map of test name to config */
20+
public $testConfigs;
21+
22+
/**
23+
* @param Properties|static $properties
24+
* @param Schema $ownerSchema
25+
* @throws \Exception
26+
* @throws \Swaggest\JsonSchema\InvalidValue
27+
*/
28+
public static function setUpProperties($properties, Schema $ownerSchema)
29+
{
30+
$properties->calcPerformanceIndex = Schema::boolean()->setDefault(true)
31+
->setDescription('Calc host performance index');
32+
33+
$properties->resultPrecision = Schema::integer()->setDefault(5)
34+
->setDescription('Result JSON precision');
35+
36+
$properties->resultFilename = Schema::string()->setDefault('benchmark-result.json')
37+
->setDescription('Result filename');
38+
39+
$defaultTestConfig = new TestConfig();
40+
$defaultTestConfig->numIterations = 100;
41+
$properties->defaultTestConfig = clone TestConfig::schema();
42+
$properties->defaultTestConfig->setDefault(TestConfig::export($defaultTestConfig))
43+
->setDescription('Default configuration');
44+
45+
$properties->testConfigs = Schema::object()->setAdditionalProperties(TestConfig::schema())
46+
->setDescription('Test settings');
47+
}
48+
49+
/**
50+
* @return static
51+
* @throws \Exception
52+
* @throws \Swaggest\JsonSchema\Exception
53+
* @throws \Swaggest\JsonSchema\InvalidValue
54+
*/
55+
public static function load()
56+
{
57+
if (file_exists('phpunit-bench.json')) {
58+
$config = Config::import(json_decode(file_get_contents('phpunit-bench.json')));
59+
} else {
60+
$config = Config::import(new \stdClass);
61+
}
62+
return $config;
63+
}
64+
}

src/PerformanceIndex.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace PHPUnitBenchmarkData;
4+
5+
class PerformanceIndex
6+
{
7+
public static function measure()
8+
{
9+
$start = microtime(1);
10+
$numIterations = 50000;
11+
12+
for ($i = 0; $i < $numIterations; ++$i) {
13+
$a = md5('abracadabra');
14+
$a = eval('md5($a);');
15+
$a = sha1($a);
16+
17+
$group = strstr('group#foobar', '#', true);
18+
$foobar = substr('group#foobar', strlen($group) + 1);
19+
self::checkParts($group, $foobar);
20+
preg_match('{^(.*?)#(.*)$}', 'group#foobar', $matches);
21+
self::checkParts($matches[1], $matches[2]);
22+
$parts = explode('#', 'group#foobar');
23+
self::checkParts($parts[0], $parts[1]);
24+
25+
26+
self::euler(20, 10);
27+
self::euler(2166, 6099);
28+
self::euler(1239432166, 2221248099);
29+
}
30+
31+
return (microtime(1) - $start) / $numIterations;
32+
}
33+
34+
private static function euler($x, $y)
35+
{
36+
$r = $x % $y;
37+
if ($r == 0) {
38+
return $y;
39+
}
40+
return self::euler($y, $r);
41+
}
42+
43+
private static function checkParts($group, $foobar)
44+
{
45+
return $group === 'group' && $foobar === 'foobar';
46+
}
47+
}

src/Result.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace PHPUnitBenchmarkData;
4+
5+
use Swaggest\JsonSchema\Constraint\Properties;
6+
use Swaggest\JsonSchema\Schema;
7+
use Swaggest\JsonSchema\Structure\ClassStructure;
8+
9+
class Result extends ClassStructure
10+
{
11+
public $dataName;
12+
public $timeSpent;
13+
public $iterations;
14+
15+
/**
16+
* Result constructor.
17+
* @param $dataName
18+
* @param $timeSpent
19+
* @param $iterations
20+
*/
21+
public function __construct($dataName = null, $timeSpent = null, $iterations = null)
22+
{
23+
$this->dataName = $dataName;
24+
$this->timeSpent = $timeSpent;
25+
$this->iterations = $iterations;
26+
}
27+
28+
29+
/**
30+
* @param Properties|static $properties
31+
* @param Schema $ownerSchema
32+
*/
33+
public static function setUpProperties($properties, Schema $ownerSchema)
34+
{
35+
$properties->timeSpent = Schema::number();
36+
//$properties->dataName = Schema::string();
37+
$properties->iterations = Schema::integer();
38+
}
39+
40+
41+
}

0 commit comments

Comments
 (0)