Skip to content

Commit

Permalink
Removed CLI mode and make error handling better
Browse files Browse the repository at this point in the history
  • Loading branch information
kocsismate committed Jan 16, 2017
1 parent 141850d commit 25b5c11
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 82 deletions.
3 changes: 1 addition & 2 deletions .env.dist
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
WEB_BENCHMARK=1
TEST_URL=benchmark-nginx
BENCHMARK_URL=benchmark-nginx
FPM_ADDR=benchmark-fpm
OPCACHE_ENABLED=1
15 changes: 0 additions & 15 deletions CHANGELOG.md

This file was deleted.

46 changes: 33 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
* [Install](#install)
* [Usage](#usage)
* [Results](#results)
* [Change Log](#change-log)
* [Contributing](#contributing)
* [Credits](#credits)
* [License](#license)
Expand All @@ -24,34 +23,55 @@ Unfortunately, [the implementation](https://github.com/TomBZombie/php-dependency
turned out to be quite controversial, so the benchmark itself wasn't really insightful.

I have been interested in the topic since then so I wanted to conduct a better benchmark than the last one was: I tried
to fix some of its flaws while keeping the good parts.
to fix some of its flaws while keeping its many good parts.

## Install

You can simply download this repository or install it via [Composer](https://getcomposer.org) by running the command
below:
You can simply download or clone this repository as well as install it via [Composer](https://getcomposer.org) by
running the command below:

```bash
$ composer require kocsismate/di-container-benchmarks:dev-master
```

Furthermore, [Docker Compose](https://www.docker.com/products/docker-compose) has to be installed on your machine.

## Usage

First, copy the ".env.dist" file as ".env" and feel free to overwrite the values in it. Now, you can run
### Usage with Docker

First of all, [Docker Compose](https://www.docker.com/products/docker-compose) has to be installed on your machine.

Then copy the ".env.dist" file to ".env" and feel free to overwrite the values in it. Now, you can run
`docker-compose up` in order to execute the tests. The html output will be generated in the "var" directory.

If you don't want to use Docker and you have PHP 7.1 at least on your host OS then you can simply run `./bin/benchmark`
after all Composer dependencies have been installed with `composer install`.
### Usage without Docker

## Results
If you don't want to use Docker then you have to take several steps before running the benchmark:

You can find the benchmark results [here](https://rawgit.com/kocsismate/php-di-container-benchmarks/master/var/benchmark.html).
- Install PHP 7.1 at least with OPcache enabled
- Install Composer
- Install a web server which is capable of executing PHP scripts (for instance Apache, nginx)
- Install dependencies by running `composer install`
- Define the `BENCHMARK_URL` environment variable where the "public/index.php" script is available. For instance:

```bash
export BENCHMARK_URL=http://localhost/index.php
```

Now you can simply type the following in order to run the benchmark:

## Change Log
```bash
./bin/benchmark
```

If you don't want to specify the benchmark URL as an environment variable then you can also pass it as a parameter:

```bash
./bin/benchmark http://localhost/index.php
```

## Results

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
You can find the benchmark results [here](https://rawgit.com/kocsismate/php-di-container-benchmarks/master/var/benchmark.html).

## Contributing

Expand Down
14 changes: 10 additions & 4 deletions bin/benchmark
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ declare(strict_types=1);
require __DIR__ . "/../app/bootstrap.php";

use DiContainerBenchmarks\Benchmark\Benchmark;
use DiContainerBenchmarks\Benchmark\CliBenchmarkContext;
use DiContainerBenchmarks\Benchmark\WebBenchmarkContext;
use DiContainerBenchmarks\Container\Aura\AuraContainer;
use DiContainerBenchmarks\Container\Auryn\AurynContainer;
Expand Down Expand Up @@ -57,8 +56,15 @@ $outputGenerators = [
new HtmlOutputGenerator(PROJECT_ROOT . "/var/benchmark.html"),
];

$benchmark = new Benchmark(
(bool) getenv("WEB_BENCHMARK") ? new WebBenchmarkContext() : new CliBenchmarkContext()
);
$benchmarkUrl = getenv("BENCHMARK_URL");
if (isset($argv[1])) {
$benchmarkUrl = $argv[1];
}
if (isset($benchmarkUrl) === false) {
echo "Either specify the 'BENCHMARK_URL' environment variable or pass it as the first parameter!";
exit(-1);
}

$benchmark = new Benchmark(new WebBenchmarkContext($benchmarkUrl));

$benchmark->runBenchmark($testSuites, $containers, $outputGenerators);
21 changes: 0 additions & 21 deletions bin/test

This file was deleted.

2 changes: 1 addition & 1 deletion build/container/fpm/custom-php.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[PHP]
allow_url_fopen=Off
allow_url_fopen=On
cgi.fix_pathinfo=0
date.timezone=UTC
default_socket_timeout=5
Expand Down
11 changes: 8 additions & 3 deletions public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@

use DiContainerBenchmarks\Test\TestRunner;

if (count($_GET) < 4) {
echo "<h1>Error!</h1>";
echo "<p>Please specifiy the 'test_suite', 'container', 'iterations' and 'test_type' query parameters!</p>";
if (empty($_GET["test_suite"]) || empty($_GET["container"]) || empty($_GET["iterations"]) || empty($_GET["test_type"])) {
echo "<h1>Please specifiy the required query parameters!</h1>";
echo "<p>";
echo '<b>test_suite</b>: An integer between 1-6 (inclusive)<br/>';
echo '<b>container</b>: Namespace of the container (e.g.: Symfony, PhpDi etc.)<br/>';
echo '<b>iterations</b>: A positive integer<br/>';
echo '<b>test_type</b>: either "cold", "semi_warm" or "warm"<br/>';
echo "</p>";
exit(-1);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Benchmark/Benchmark.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private function runTest(
BenchmarkResult $benchmarkResult
): void {

for ($run = 0; $run < 10; $run++) {
for ($run = 0; $run < 25; $run++) {
echo "Running " . $this->context->getName() . " test " .
$testSuite->getNumber() . "." . $testCase->getNumber() .
" (" . $container->getName() . '): ' . ($run+1) . "/10\n";
Expand Down
21 changes: 0 additions & 21 deletions src/Benchmark/CliBenchmarkContext.php

This file was deleted.

12 changes: 11 additions & 1 deletion src/Benchmark/WebBenchmarkContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@

class WebBenchmarkContext implements BenchmarkContextInterface
{
/**
* @var string
*/
private $benchmarkUrl;

public function __construct(string $benchmarkUrl)
{
$this->benchmarkUrl = $benchmarkUrl;
}

public function getName(): string
{
return "web";
Expand All @@ -16,7 +26,7 @@ public function getTestOutput(int $number, string $container, int $iterations, s
curl_setopt(
$ch,
CURLOPT_URL,
getenv("TEST_URL") . "?test_suite=$number&container=$container&iterations=$iterations&test_type=$testType"
$this->benchmarkUrl . "?test_suite=$number&container=$container&iterations=$iterations&test_type=$testType"
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

Expand Down
2 changes: 2 additions & 0 deletions src/OutputGenerator/OutputGeneratorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
namespace DiContainerBenchmarks\OutputGenerator;

use DiContainerBenchmarks\Benchmark\BenchmarkResult;
use DiContainerBenchmarks\Container\ContainerInterface;
use DiContainerBenchmarks\TestSuite\TestSuiteInterface;

interface OutputGeneratorInterface
{
/**
* @param TestSuiteInterface[] $testSuites
* @param ContainerInterface[] $containers
*/
public function generateOutput(array $testSuites, array $containers, BenchmarkResult $benchmarkResult): void;
}

0 comments on commit 25b5c11

Please sign in to comment.