Skip to content

Commit

Permalink
Merge pull request #144 from tbela99/threads
Browse files Browse the repository at this point in the history
Added multithreading #142
  • Loading branch information
tbela99 committed Sep 19, 2022
2 parents d21c984 + ba887b9 commit 56ae9e4
Show file tree
Hide file tree
Showing 155 changed files with 2,873 additions and 1,155 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/php.yml
@@ -1,4 +1,4 @@
name: CI
name: PHP8.0-CI

on:
push:
Expand Down
34 changes: 34 additions & 0 deletions .github/workflows/php5.6.yml
@@ -0,0 +1,34 @@
name: PHP5.6-CI

on:
push:
branches: [ php56-backport ]
pull_request:
branches: [ php56-backport ]

jobs:
build:

runs-on: ubuntu-20.04

steps:
- uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '5.6'
extensions: mbstring, xml, curl
tools: phpunit, composer

- name: Validate composer.json and composer.lock
run: composer validate --strict

- name: Install dependencies
run: composer install --prefer-dist --no-progress

# Add a test script to composer.json, for instance: "test": "vendor/bin/phpunit"
# Docs: https://getcomposer.org/doc/articles/scripts.md

- name: Run test suite
run: composer run-script test
34 changes: 34 additions & 0 deletions .github/workflows/php7.4.yml
@@ -0,0 +1,34 @@
name: PHP7.4-CI

on:
push:
branches: [ php56-backport ]
pull_request:
branches: [ php56-backport ]

jobs:
build:

runs-on: ubuntu-20.04

steps:
- uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '7.4'
extensions: mbstring, xml, curl
tools: phpunit, composer

- name: Validate composer.json and composer.lock
run: composer validate --strict

- name: Install dependencies
run: composer install --prefer-dist --no-progress

# Add a test script to composer.json, for instance: "test": "vendor/bin/phpunit"
# Docs: https://getcomposer.org/doc/articles/scripts.md

- name: Run test suite
run: composer run-script test
34 changes: 34 additions & 0 deletions .github/workflows/php8.1.yml
@@ -0,0 +1,34 @@
name: PHP8.1-CI

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:

runs-on: ubuntu-20.04

steps:
- uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
extensions: mbstring, xml, curl
tools: phpunit, composer

- name: Validate composer.json and composer.lock
run: composer validate --strict

- name: Install dependencies
run: composer install --prefer-dist --no-progress

# Add a test script to composer.json, for instance: "test": "vendor/bin/phpunit"
# Docs: https://getcomposer.org/doc/articles/scripts.md

- name: Run test suite
run: composer run-script test
34 changes: 34 additions & 0 deletions .github/workflows/php8.2.yml
@@ -0,0 +1,34 @@
name: PHP8.2-CI

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:

runs-on: ubuntu-20.04

steps:
- uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.2'
extensions: mbstring, xml, curl
tools: phpunit, composer

- name: Validate composer.json and composer.lock
run: composer validate --strict

- name: Install dependencies
run: composer install --prefer-dist --no-progress

# Add a test script to composer.json, for instance: "test": "vendor/bin/phpunit"
# Docs: https://getcomposer.org/doc/articles/scripts.md

- name: Run test suite
run: composer run-script test
26 changes: 24 additions & 2 deletions README.md
Expand Up @@ -31,10 +31,16 @@ A CSS parser, beautifier and minifier written in PHP. It supports the following

install using [Composer](https://getcomposer.org/)

### PHP version >= 8.0
```bash
$ composer require tbela99/css
```

### PHP version >= 5.6
```bash
$ composer require "tbela99/css:dev-php56-backport"
```

## Requirements

- PHP version >= 8.0 on master branch.
Expand Down Expand Up @@ -578,6 +584,19 @@ $stylesheet->append('https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/

## Performance

### Utility methods

The renderer class provides utility methods to format css data

```php

$css = \TBela\CSS\Renderer::fromFile($url_or_file, $renderOptions = [], $parseOptions = []);
#
$css = \TBela\CSS\Renderer::fromString($css, $renderOptions = [], $parseOptions = []);
```

### Manual parsing and rendering

parsing and rendering ast is 3x faster than parsing an element.

```php
Expand All @@ -592,9 +611,12 @@ echo (string) $parser;

// or render minified css
$renderer = new Renderer(['compress' => true]);
echo $renderer->renderAst($parser->getAst());

// slower - will build an Element
echo $renderer->renderAst($parser);
# or
echo $renderer->renderAst($parser->getAst());
# or
// slower - will build a stylesheet object
echo $renderer->render($parser->parse());
```
## Parser Options
Expand Down
2 changes: 1 addition & 1 deletion benchmark/parseast.php
Expand Up @@ -13,4 +13,4 @@

$parser = (new Parser($css, $options));

//$ast = $parser->getAst();
$ast = $parser->getAst();
19 changes: 16 additions & 3 deletions bin/benchmark.sh
@@ -1,4 +1,16 @@
##!/bin/bash -x -v

# css files
if [ $# -gt 0 ]; then

files=()

while [ $# -gt 0 ]; do
files+=($(realpath "$1"))
shift
done
fi

cd $(dirname "$0")
cd ../benchmark

Expand All @@ -8,8 +20,9 @@ if [ ! -d ./vendor/sabberworm ]; then
exit 1
fi

# css files
files=$(ls -d ../test/perf_files/*.css)
if [ -z "$files" ]; then
files=$(ls -d ../test/perf_files/*.css)
fi
# files size
sizes=()
# measure test time
Expand All @@ -25,7 +38,7 @@ size() {
# shellcheck disable=SC2046
result=$($(echo "$prog" | cut -f1 -d ' ') $(echo "$prog" | cut -f2 -s -d ' ') "$@" 2>&1)
output=$(convert_file_size "${#result}")
# echo $(lpad "$output" 6)" ("$(lpad $(echo "scale=2; ${#result} * 100 / "$(stat -c%s $(echo "$@" | rev | cut -d ' ' -f1 | rev)) | bc) 5)"%)"
# echo $(lpad "$output" 6)" ("$(lpad $(echo "scale=2; ${#result} * 100 / "$(stat -c%s $(echo "$@" | rev | cut -d ' ' -f1 | rev)) | bc) 5)"%)"
echo $(lpad "$output" 9)
}
# left pad with '\ ', will pipe to sed 's/\\ //g' to display the actual space character
Expand Down
34 changes: 30 additions & 4 deletions bin/runtest.sh
Expand Up @@ -9,14 +9,25 @@
##
#set -x
DIR=$(cd -P -- "$(dirname $(readlink -f "$0"))" && pwd -P)
cd "$DIR"
cd "$DIR" || exit 1

if [ ! -f "../vendor/bin/phpunit" ]; then
echo "please go to "$(dirname "$DIR")" and run 'composer install'"
exit 1
fi

unset DIR
TEST_PCNTL=$(php -m | grep pcntl)

php=$(command -v php"$PHP_VER")

#test_timeout=60

if [ -z "$php" ]
then
echo "php$PHP_VER is not installed"
exit 1
fi
#
#
#../phpunit.phar --bootstrap autoload.php src/*.php
Expand All @@ -33,7 +44,22 @@ run() {

#
# set -x
php -dmemory_limit=512M ../vendor/bin/phpunit -v --enforce-time-limit --colors=always --bootstrap autoload.php --testdox --fail-on-skipped --fail-on-risky --fail-on-incomplete "$@"
result="0"
# timeout $test_timeout
PROCESS_ENGINE="process" "$php" ../vendor/bin/phpunit -v --colors=always --bootstrap autoload.php --testdox --fail-on-skipped --fail-on-risky --fail-on-incomplete "$@" || result="1"

if [ "$result" -gt 0 ]
then
return "$result"
fi

if [ -n "$TEST_PCNTL" ]; then
# timeout $test_timeout
PROCESS_ENGINE="thread" "$php" ../vendor/bin/phpunit -v --colors=always --bootstrap autoload.php --testdox --fail-on-skipped --fail-on-risky --fail-on-incomplete "$@" || result="1"
# unset $PROCESS_ENGINE
fi

return "$result"
# set +x
}

Expand All @@ -42,12 +68,12 @@ testName() {
fname=$(basename "$1" | awk -F . '{print $1}')

# strip the Test suffix
echo ""${fname%Test}
echo "${fname%Test}"
}

#
#
cd ../test
cd ../test || exit 1
#pwd
#
#
Expand Down

0 comments on commit 56ae9e4

Please sign in to comment.