Skip to content

Commit

Permalink
Add PHPBench tool and first benchmark example (#581)
Browse files Browse the repository at this point in the history
  • Loading branch information
stloyd authored Oct 16, 2023
1 parent 0f24912 commit 68916ab
Show file tree
Hide file tree
Showing 24 changed files with 1,877 additions and 8 deletions.
60 changes: 60 additions & 0 deletions .github/workflows/benchmark-tag.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: Benchmark History Tag

on:
push:
branches: [ 1.x ]

jobs:
benchmark-tag:
name: "Benchmark History Tag"

runs-on: ${{ matrix.operating-system }}

strategy:
fail-fast: false
matrix:
dependencies:
- "locked"
php-version:
- "8.1"
operating-system:
- "ubuntu-latest"

steps:
- name: "Checkout"
uses: "actions/checkout@v4"

- name: "Install PHP"
uses: "shivammathur/setup-php@v2"
with:
coverage: none
tools: composer:v2
php-version: "${{ matrix.php-version }}"
ini-values: memory_limit=-1

- name: "Get Composer Cache Directory"
id: composer-cache
run: |
echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
- name: "Cache Composer dependencies"
uses: "actions/cache@v3"
with:
path: "${{ steps.composer-cache.outputs.dir }}"
key: "php-${{ matrix.php-version }}-locked-composer-${{ hashFiles('**/composer.lock') }}"
restore-keys: |
php-${{ matrix.php-version }}-locked-composer-
- name: "Install locked dependencies"
run: "composer install --no-interaction --no-progress"

- name: "Cache PHPBench reports"
uses: "actions/cache@v3"
with:
path: "var/phpbench"
key: "php-${{ matrix.php-version }}-phpbench"
restore-keys: |
php-${{ matrix.php-version }}-phpbench-
- name: "Benchmark Tag"
run: "composer test:benchmark -- --tag=original --progress=plain"
59 changes: 59 additions & 0 deletions .github/workflows/test-benchmark.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: Benchmark Suite

on:
pull_request:

jobs:
benchmark:
name: "Benchmark"

runs-on: ${{ matrix.operating-system }}

strategy:
fail-fast: false
matrix:
dependencies:
- "locked"
php-version:
- "8.1"
operating-system:
- "ubuntu-latest"

steps:
- name: "Checkout"
uses: "actions/checkout@v4"

- name: "Install PHP"
uses: "shivammathur/setup-php@v2"
with:
coverage: none
tools: composer:v2
php-version: "${{ matrix.php-version }}"
ini-values: memory_limit=-1

- name: "Get Composer Cache Directory"
id: composer-cache
run: |
echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
- name: "Cache Composer dependencies"
uses: "actions/cache@v3"
with:
path: "${{ steps.composer-cache.outputs.dir }}"
key: "php-${{ matrix.php-version }}-locked-composer-${{ hashFiles('**/composer.lock') }}"
restore-keys: |
php-${{ matrix.php-version }}-locked-composer-
- name: "Install locked dependencies"
run: "composer install --no-interaction --no-progress"

- name: "Cache PHPBench reports"
uses: "actions/cache@v3"
with:
path: "var/phpbench"
key: "php-${{ matrix.php-version }}-phpbench"
restore-keys: |
php-${{ matrix.php-version }}-phpbench-
- name: "Benchmark"
run: "composer test:benchmark -- --ref=original --progress=plain"
2 changes: 1 addition & 1 deletion .github/workflows/test-suite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
- name: "Get Composer Cache Directory"
id: composer-cache
run: |
echo "::set-output name=dir::$(composer config cache-files-dir)"
echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
- name: "Cache Composer dependencies"
uses: "actions/cache@v3"
Expand Down
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@
"test": [
"tools/phpunit/vendor/bin/phpunit"
],
"test:benchmark": [
"tools/phpbench/vendor/bin/phpbench run --report=aggregate --retry-threshold=5"
],
"test:mutation": [
"tools/infection/vendor/bin/infection -j2"
],
Expand Down Expand Up @@ -193,6 +196,7 @@
"composer install --working-dir=./tools/cs-fixer",
"composer install --working-dir=./tools/infection",
"composer install --working-dir=./tools/monorepo",
"composer install --working-dir=./tools/phpbench",
"composer install --working-dir=./tools/phpstan",
"composer install --working-dir=./tools/psalm",
"composer install --working-dir=./tools/phpunit",
Expand Down
15 changes: 15 additions & 0 deletions phpbench.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"$schema": "./tools/phpbench/vendor/phpbench/phpbench/phpbench.schema.json",
"runner.bootstrap": "vendor/autoload.php",
"runner.path": [
"src/adapter/etl-adapter-avro/tests/Flow/ETL/Adapter/Avro/FlixTech/Tests/Benchmark/",
"src/adapter/etl-adapter-csv/tests/Flow/ETL/Adapter/CSV/Tests/Benchmark/",
"src/adapter/etl-adapter-json/tests/Flow/ETL/Tests/Benchmark/",
"src/adapter/etl-adapter-parquet/tests/Flow/ETL/Adapter/Parquet/Tests/Benchmark/",
"src/adapter/etl-adapter-text/tests/Flow/ETL/Adapter/Text/Tests/Benchmark/",
"src/adapter/etl-adapter-xml/tests/Flow/ETL/Tests/Benchmark/",
"src/core/etl/tests/Flow/ETL/Tests/Benchmark/"
],
"runner.progress": "dots",
"storage.xml_storage_path": "var/phpbench"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php declare(strict_types=1);

namespace Flow\ETL\Adapter\Text\Tests\Benchmark;

use Flow\ETL\DSL\Avro;
use PhpBench\Attributes\Iterations;
use PhpBench\Attributes\Revs;

#[Iterations(5)]
final class AvroExtractorBench
{
#[Revs(1000)]
public function bench_extract() : void
{
Avro::from(__DIR__ . '/../Fixtures/data.avro');
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php declare(strict_types=1);

namespace Flow\ETL\Adapter\Text\Tests\Benchmark;

use Flow\ETL\DSL\CSV;
use PhpBench\Attributes\Iterations;
use PhpBench\Attributes\Revs;

#[Iterations(5)]
final class CSVExtractorBench
{
#[Revs(1000)]
public function bench_extract() : void
{
CSV::from(__DIR__ . '/../Fixtures/annual-enterprise-survey-2019-financial-year-provisional-csv.csv');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php declare(strict_types=1);

namespace Flow\ETL\Tests\Benchmark\Adapter\JSON\JSONMachine;

use Flow\ETL\DSL\Json;
use PhpBench\Attributes\Iterations;
use PhpBench\Attributes\Revs;

#[Iterations(5)]
final class JsonExtractorBench
{
#[Revs(1000)]
public function bench_extract() : void
{
Json::from(__DIR__ . '/../../../../Fixtures/timezones.json');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ final class JsonExtractorTest extends TestCase
public function test_extracting_json_from_local_file_stream() : void
{
$rows = (new Flow(Config::builder()->putInputIntoRows()))
->read(Json::from(__DIR__ . '/../Fixtures/timezones.json', 5))
->read(Json::from(__DIR__ . '/../../../../Fixtures/timezones.json', 5))
->fetch();

foreach ($rows as $row) {
Expand All @@ -42,7 +42,7 @@ public function test_extracting_json_from_local_file_stream() : void
public function test_extracting_json_from_local_file_stream_using_pointer() : void
{
$rows = (new Flow())
->read(Json::from(__DIR__ . '/../Fixtures/nested_timezones.json', 5, pointer: '/timezones'))
->read(Json::from(__DIR__ . '/../../../../Fixtures/nested_timezones.json', 5, pointer: '/timezones'))
->fetch();

foreach ($rows as $row) {
Expand All @@ -64,7 +64,7 @@ public function test_extracting_json_from_local_file_stream_using_pointer() : vo

public function test_extracting_json_from_local_file_string_uri() : void
{
$extractor = new JsonExtractor(Path::realpath(__DIR__ . '/../Fixtures/timezones.json'), 5);
$extractor = new JsonExtractor(Path::realpath(__DIR__ . '/../../../../Fixtures/timezones.json'), 5);

$total = 0;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php declare(strict_types=1);

namespace Flow\ETL\Adapter\Text\Tests\Benchmark;

use Flow\ETL\DSL\Parquet;
use PhpBench\Attributes\Iterations;
use PhpBench\Attributes\Revs;

#[Iterations(5)]
final class ParquetExtractorBench
{
#[Revs(1000)]
public function bench_extract() : void
{
Parquet::from(__DIR__ . '/../Fixtures/data.parquet');
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php declare(strict_types=1);

namespace Flow\ETL\Adapter\Text\Tests\Benchmark;

use Flow\ETL\DSL\Text;
use PhpBench\Attributes\Iterations;
use PhpBench\Attributes\Revs;

#[Iterations(5)]
final class TextExtractorBench
{
#[Revs(1000)]
public function bench_extract() : void
{
Text::from(__DIR__ . '/../Fixtures/annual-enterprise-survey-2019-financial-year-provisional-csv.csv');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php declare(strict_types=1);

namespace Flow\ETL\Adapter\Text\Tests\Benchmark;

use Flow\ETL\DSL\XML;
use PhpBench\Attributes\Iterations;
use PhpBench\Attributes\Revs;

#[Iterations(5)]
final class XmlExtractorBench
{
#[Revs(1000)]
public function bench_extract() : void
{
XML::from(__DIR__ . '/../Fixtures/simple_items.xml');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ final class XMLReaderExtractorTest extends TestCase
public function test_reading_xml() : void
{
$xml = new \DOMDocument();
$xml->load(__DIR__ . '/xml/simple_items.xml');
$xml->load(__DIR__ . '/../../../Fixtures/simple_items.xml');

$this->assertEquals(
(new Rows(Row::create(Entry::xml('node', $xml)))),
(new Flow())
->read(XML::from(__DIR__ . '/xml/simple_items.xml'))
->read(XML::from(__DIR__ . '/../../../Fixtures/simple_items.xml'))
->fetch()
);
}
Expand All @@ -37,7 +37,7 @@ public function test_reading_xml_each_collection_item() : void
Row::create(Entry::xml('node', '<item item_attribute_01="5"><id id_attribute_01="5">5</id></item>')),
),
(new Flow())
->read(XML::from(__DIR__ . '/xml/simple_items_flat.xml', 'root/items/item'))
->read(XML::from(__DIR__ . '/../../../Fixtures/simple_items_flat.xml', 'root/items/item'))
->fetch()
);
}
Expand Down Expand Up @@ -69,7 +69,7 @@ public function test_reading_xml_from_path() : void
$this->assertEquals(
new Rows(Row::create(Entry::xml('node', $xml))),
(new Flow())
->read(XML::from(__DIR__ . '/xml/simple_items.xml', 'root/items'))
->read(XML::from(__DIR__ . '/../../../Fixtures/simple_items.xml', 'root/items'))
->fetch()
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php declare(strict_types=1);

namespace Flow\ETL\Tests\Benchmark\Transformer;

use function Flow\ETL\DSL\ref;
use Flow\ETL\Config;
use Flow\ETL\DSL\Entry;
use Flow\ETL\FlowContext;
use Flow\ETL\Row;
use Flow\ETL\Rows;
use Flow\ETL\Transformer\EntryExpressionEvalTransformer;
use PhpBench\Attributes\Iterations;
use PhpBench\Attributes\Revs;

#[Iterations(5)]
final class EntryExpressionEvalTransformerBench
{
#[Revs(1000)]
public function bench_transform_json_row() : void
{
(new EntryExpressionEvalTransformer('decoded', ref('json')->jsonDecode()))
->transform(
new Rows(Row::create(Entry::json('json', ['some' => 'field', 'boolean' => false]))),
new FlowContext(Config::default())
);
}

#[Revs(1000)]
public function bench_transform_string_row() : void
{
(new EntryExpressionEvalTransformer('string', ref('string')->upper()))
->transform(
new Rows(Row::create(Entry::string('string', 'string'))),
new FlowContext(Config::default())
);
}

#[Revs(1000)]
public function bench_transform_xml_row() : void
{
(new EntryExpressionEvalTransformer('xpath', ref('xml')->xpath('/root/foo')))
->transform(
new Rows(Row::create(Entry::xml('xml', '<root><foo baz="buz">bar</foo><foo>baz</foo></root>'))),
new FlowContext(Config::default())
);
}
}
Loading

0 comments on commit 68916ab

Please sign in to comment.