Quality improvements

This commit is contained in:
Jens True 2024-02-02 11:04:31 +00:00
parent 23fc14af0a
commit 36dea4ea03
8 changed files with 269 additions and 4 deletions

134
benchmark/BasicBench.php Normal file

@ -0,0 +1,134 @@
<?php
namespace DNW\Skills\Benchmark;
use DNW\Skills\TrueSkill\TwoPlayerTrueSkillCalculator;
use DNW\Skills\TrueSkill\TwoTeamTrueSkillCalculator;
use DNW\Skills\TrueSkill\FactorGraphTrueSkillCalculator;
use DNW\Skills\GameInfo;
use DNW\Skills\Player;
use DNW\Skills\Team;
use DNW\Skills\Teams;
class BasicBench
{
/**
* @Revs(20)
* @Iterations(20)
*/
public function benchBasic2PlayersUsingTwoPlayerTrueSkillCalculator(): void
{
$gameInfo = new GameInfo();
$p1 = new Player("Winner");
$p2 = new Player("Average");
$team1 = new Team($p1, $gameInfo->getDefaultRating());
$team2 = new Team($p2, $gameInfo->getDefaultRating());
for ($i = 0; $i < 10; $i++) {
$teams = Teams::concat($team1, $team2);
$calculator = new TwoPlayerTrueSkillCalculator();
$newRatings = $calculator->calculateNewRatings($gameInfo, $teams, [1, 2]);
$team1 = new Team($p1, $newRatings->getRating($p1));
$team2 = new Team($p2, $newRatings->getRating($p2));
$newRatings->getRating($p1)->getConservativeRating();
$newRatings->getRating($p2)->getConservativeRating();
}
}
/**
* @Revs(20)
* @Iterations(20)
*/
public function benchBasic2PlayersUsingTwoTeamTrueSkillCalculator(): void
{
$gameInfo = new GameInfo();
$p1 = new Player("Winner");
$p2 = new Player("Average");
$team1 = new Team($p1, $gameInfo->getDefaultRating());
$team2 = new Team($p2, $gameInfo->getDefaultRating());
for ($i = 0; $i < 10; $i++) {
$teams = Teams::concat($team1, $team2);
$calculator = new TwoTeamTrueSkillCalculator();
$newRatings = $calculator->calculateNewRatings($gameInfo, $teams, [1, 2]);
$team1 = new Team($p1, $newRatings->getRating($p1));
$team2 = new Team($p2, $newRatings->getRating($p2));
$newRatings->getRating($p1)->getConservativeRating();
$newRatings->getRating($p2)->getConservativeRating();
}
}
/**
* @Revs(20)
* @Iterations(20)
*/
public function benchBasic2PlayersUsingFactorGraphTrueSkillCalculator(): void
{
$gameInfo = new GameInfo();
$p1 = new Player("Winner");
$p2 = new Player("Average");
$team1 = new Team($p1, $gameInfo->getDefaultRating());
$team2 = new Team($p2, $gameInfo->getDefaultRating());
for ($i = 0; $i < 10; $i++) {
$teams = Teams::concat($team1, $team2);
$calculator = new FactorGraphTrueSkillCalculator();
$newRatings = $calculator->calculateNewRatings($gameInfo, $teams, [1, 2]);
$team1 = new Team($p1, $newRatings->getRating($p1));
$team2 = new Team($p2, $newRatings->getRating($p2));
$newRatings->getRating($p1)->getConservativeRating();
$newRatings->getRating($p2)->getConservativeRating();
}
}
/**
* @Revs(20)
* @Iterations(20)
*/
public function bench3Teams(): void
{
$gameInfo = new GameInfo();
$p1 = new Player("Winner");
$p2 = new Player("Average");
$p3 = new Player("Looser");
$team1 = new Team($p1, $gameInfo->getDefaultRating());
$team2 = new Team($p2, $gameInfo->getDefaultRating());
$team3 = new Team($p3, $gameInfo->getDefaultRating());
for ($i = 0; $i < 10; $i++) {
$teams = Teams::concat($team1, $team2, $team3);
$calculator = new FactorGraphTrueSkillCalculator();
$newRatings = $calculator->calculateNewRatings($gameInfo, $teams, [1, 2, 3]);
$team1 = new Team($p1, $newRatings->getRating($p1));
$team2 = new Team($p2, $newRatings->getRating($p2));
$team3 = new Team($p3, $newRatings->getRating($p3));
$newRatings->getRating($p1)->getConservativeRating();
$newRatings->getRating($p2)->getConservativeRating();
$newRatings->getRating($p3)->getConservativeRating();
}
}
}

@ -9,7 +9,8 @@
"phpunit/phpunit": "^10",
"phpstan/phpstan": "^1",
"squizlabs/php_codesniffer": "*",
"vimeo/psalm": "^5.14"
"vimeo/psalm": "^5.14",
"phpmetrics/phpmetrics": "^3.0-dev"
},
"autoload": {
"psr-4": {
@ -25,6 +26,8 @@
"test": "vendor/bin/phpunit tests --display-warnings",
"test-coverage": "vendor/bin/phpunit tests --testdox --coverage-filter src --coverage-html output/coverage --coverage-text --testdox-html output/test.html --log-junit output/test.xml",
"document": "phpDocumentor --setting=graphs.enabled=true",
"benchmark": "phpbench run --report=default --output=build-artifact",
"metrics": "vendor/bin/phpmetrics --config=phpmetrics.json",
"analyze": [
"@analyze-phpstan",
"@analyze-psalm",
@ -32,6 +35,6 @@
],
"analyze-phpstan":"vendor/bin/phpstan analyze --error-format=raw",
"analyze-psalm": "vendor/bin/psalm --no-cache",
"analyze-phpcs": "vendor/bin/phpcs --report=emacs --standard=PSR1,PSR2,PSR12 --exclude=Generic.Files.LineLength src tests"
"analyze-phpcs": "vendor/bin/phpcs --report=emacs --standard=PSR1,PSR2,PSR12 --exclude=Generic.Files.LineLength src tests benchmark"
}
}

82
composer.lock generated

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "667bb75c25a72b3d35f64ada9d9d9e0d",
"content-hash": "9d079e1055ee327f2d46ed48b8a25cd6",
"packages": [],
"packages-dev": [
{
@ -1082,6 +1082,82 @@
},
"time": "2024-01-11T11:49:22+00:00"
},
{
"name": "phpmetrics/phpmetrics",
"version": "v3.0.0rc5",
"source": {
"type": "git",
"url": "https://github.com/phpmetrics/PhpMetrics.git",
"reference": "7a4ee5d6a8233f449fb9125ad586d3880c8a1b2f"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/phpmetrics/PhpMetrics/zipball/7a4ee5d6a8233f449fb9125ad586d3880c8a1b2f",
"reference": "7a4ee5d6a8233f449fb9125ad586d3880c8a1b2f",
"shasum": ""
},
"require": {
"ext-tokenizer": "*",
"nikic/php-parser": "^4",
"php": ">=8.1"
},
"replace": {
"halleck45/php-metrics": "*",
"halleck45/phpmetrics": "*"
},
"require-dev": {
"phake/phake": "^4.4.0",
"phpstan/extension-installer": "^1.3",
"phpstan/phpstan": "^1.10",
"phpstan/phpstan-deprecation-rules": "^1.1",
"phpstan/phpstan-phpunit": "^1.3",
"phpstan/phpstan-strict-rules": "^1.5",
"phpunit/phpunit": "^10.3",
"roave/security-advisories": "dev-latest",
"sebastian/comparator": ">=5.0.0",
"squizlabs/php_codesniffer": "^3.7",
"symfony/dom-crawler": "^6.3",
"vimeo/psalm": "^5.15"
},
"suggest": {
"ext-dom": "To allow XML parsing and report results.",
"ext-yaml": "To allow yaml parsing of configuration files."
},
"bin": [
"bin/phpmetrics"
],
"type": "library",
"autoload": {
"psr-4": {
"Hal\\": "src/Hal"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Jean-François Lépine",
"email": "lepinejeanfrancois@yahoo.fr",
"homepage": "http://www.lepine.pro",
"role": "Copyright Holder"
}
],
"description": "Static analyzer tool for PHP : Coupling, Cyclomatic complexity, Maintainability Index, Halstead's metrics... and more !",
"homepage": "http://www.phpmetrics.org",
"keywords": [
"analysis",
"qa",
"quality",
"testing"
],
"support": {
"issues": "https://github.com/PhpMetrics/PhpMetrics/issues",
"source": "https://github.com/phpmetrics/PhpMetrics/tree/v3.0.0rc5"
},
"time": "2024-01-17T10:59:12+00:00"
},
{
"name": "phpstan/phpdoc-parser",
"version": "1.25.0",
@ -3649,7 +3725,9 @@
],
"aliases": [],
"minimum-stability": "stable",
"stability-flags": [],
"stability-flags": {
"phpmetrics/phpmetrics": 20
},
"prefer-stable": false,
"prefer-lowest": false,
"platform": {

11
phpbench.json Normal file

@ -0,0 +1,11 @@
{
"runner.bootstrap": "vendor/autoload.php",
"runner.path": "benchmark/",
"report.outputs": {
"build-artifact": {
"renderer": "html",
"path": "output/benchmark.html",
"title": "Benchmarking"
}
}
}

20
phpmetrics.json Normal file

@ -0,0 +1,20 @@
{
"composer": true,
"includes": [
"src"
],
"excludes": [
"tests"
],
"report": {
"html": "output/metrics/"
},
"plugins": {
"git": {
"binary": "git"
},
"junit": {
"file": "output/test.xml"
}
}
}

@ -3,3 +3,4 @@ parameters:
paths:
- src
- tests
- benchmark

@ -11,6 +11,7 @@
<projectFiles>
<directory name="src" />
<directory name="tests" />
<directory name="benchmark" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>

17
tests/PlayerTest.php Normal file

@ -0,0 +1,17 @@
<?php
namespace DNW\Skills\Tests;
use DNW\Skills\Player;
use PHPUnit\Framework\TestCase;
class PlayerTest extends TestCase
{
public function test(): void
{
$p = new Player('dummy', 0.1, 0.2);
$this->assertEquals('dummy', (string)$p);
$this->assertEquals(0.1, $p->getPartialPlayPercentage());
$this->assertEquals(0.2, $p->getPartialUpdatePercentage());
}
}