Skip to content

Commit

Permalink
add phpbench (#27)
Browse files Browse the repository at this point in the history
Signed-off-by: svrnm <[email protected]>
  • Loading branch information
svrnm authored Jul 11, 2024
1 parent 0bf52d8 commit ec6b020
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 25 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/common-setup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Common Setup

on:
workflow_call:
inputs:
php-version:
required: true
type: string
cache-key-suffix:
required: true
type: string

jobs:
setup:
runs-on: ubuntu-22.04

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

- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ inputs.php-version }}
extensions: mbstring, pdo, pdo_mysql
ini-values: |
memory_limit=512M
coverage: none

- name: Get Composer Cache
id: composer-cache
uses: actions/cache@v4
with:
path: ~/.composer/cache
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}-${{ inputs.cache-key-suffix }}
restore-keys: ${{ runner.os }}-composer-${{ inputs.cache-key-suffix }}

- name: Install dependencies
run: composer install --prefer-dist --no-progress --no-suggest
28 changes: 28 additions & 0 deletions .github/workflows/phpbench.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Run PhpBench

on:
push:
branches:
- master
pull_request:
branches:
- master
workflow_dispatch:

jobs:
phpbench:
runs-on: ubuntu-22.04

strategy:
matrix:
php-version: ['7.0', '7.4', '8.0', '8.3']

steps:
- name: Common Setup
uses: ./.github/workflows/common.yml
with:
php-version: ${{ matrix.php-version }}
cache-key-suffix: ${{ matrix.php-version }}

- name: Run PhpBench
run: vendor/bin/phpbench run
32 changes: 8 additions & 24 deletions .github/workflows/phpspec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,26 @@ name: Run PhpSpec
on:
push:
branches:
- main
- master
pull_request:
branches:
- main
- master
workflow_dispatch:

jobs:
phpspec:
runs-on: ubuntu-latest
runs-on: ubuntu-22.04

strategy:
matrix:
php-version: ['7.4', '8.0', '8.1']
php-version: ['7.0', '7.4', '8.0', '8.3']

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up PHP
uses: shivammathur/setup-php@v2
- name: Common Setup
uses: ./.github/workflows/common.yml
with:
php-version: ${{ matrix.php-version }}
ini-values: |
memory_limit=512M
coverage: none

- name: Get Composer Cache
id: composer-cache
uses: actions/cache@v2
with:
path: ~/.composer/cache
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}-${{ matrix.php-version }}
restore-keys: ${{ runner.os }}-composer-${{ matrix.php-version }}

- name: Install dependencies
run: composer install --prefer-dist --no-progress --no-suggest
cache-key-suffix: ${{ matrix.php-version }}

- name: Run PhpSpec
run: vendor/bin/phpspec run
run: vendor/bin/phpspec run
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
examples/test.xlsx
/vendor
composer.lock
*.swp
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
},
"require-dev": {
"phpspec/phpspec": "~7.5",
"mikey179/vfsstream": "~1.6"
"mikey179/vfsstream": "~1.6",
"phpbench/phpbench": "^1.3"
},
"autoload": {
"psr-0": {
Expand Down
Binary file removed examples/test.xlsx
Binary file not shown.
4 changes: 4 additions & 0 deletions phpbench.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"$schema":"./vendor/phpbench/phpbench/phpbench.schema.json",
"runner.bootstrap": "vendor/autoload.php"
}
58 changes: 58 additions & 0 deletions tests/Svrnm/ExcelDataTables/Benchmark/AttachToFileBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
namespace Svrnm\ExcelDataTables\Benchmark;

use Svrnm\ExcelDataTables\ExcelDataTable;

class AttachToFileBench
{
/**
* @Revs(10)
* @Iterations(2)
* @ParamProviders("provideAttachToFile")
*/
public function benchAttachToFile(array $params)
{
$dataTable = new ExcelDataTable();
$in = __DIR__ . '/../../../../examples/spec.xlsx';

// output file need to be recreated => delete if exists
$out = __DIR__ . '/../../../../examples/test.xlsx';
if( file_exists($out) ) {
if( !@unlink ( $out ) )
{
echo "CRITIC! - destination file: $out - has to be deleted, and I can't<br>";
echo "CRITIC! - check directory and file permissions<br>";
die();
}
}
$dataTable->showHeaders()->addRows($params["data"])->attachToFile($in, $out, false);
}

private static function generate($rows, $cols) {
$data = array();
for($i = 0; $i < $rows; $i++) {
$row = array();
for($j = 0; $j < $cols; $j++) {
switch($j%3) {
case 0:
$row[] = $i;
break;
case 1:
$row[] = '('.$i.','.$j.')' ;
break;
case 2:
$row[] = new \DateTime('2024-01-01');
break;
}
}
$data[] = $row;
}
return $data;
}

public function provideAttachToFile() {
yield '100x100' => ['data' => self::generate(100, 100)];
yield '200x200' => ['data' => self::generate(200, 200)];
yield '400x400' => ['data' => self::generate(400, 400)];
}
}

0 comments on commit ec6b020

Please sign in to comment.