Skip to content

Commit

Permalink
Merge pull request #43 from myaaghubi/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
myaaghubi authored Aug 26, 2023
2 parents 44f6aa7 + 49d94a7 commit c584dbb
Show file tree
Hide file tree
Showing 111 changed files with 8,840 additions and 150 deletions.
16 changes: 16 additions & 0 deletions .docker/apache.dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FROM php:8.2-apache

RUN apt-get update \
&& apt-get install -y libicu-dev

RUN mkdir /var/www/html/PHP-Frameworks-Bench

# Need it in a lot of frameworks
RUN docker-php-ext-install intl

# Optional opcache (recommended)
#RUN docker-php-ext-install opcache

ENV PORT 80
ENTRYPOINT []
CMD sed -i "s/80/$PORT/g" /etc/apache2/sites-available/000-default.conf /etc/apache2/ports.conf && docker-php-entrypoint apache2-foreground
101 changes: 69 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# PHP Frameworks Bench
[![Test PHP benchmark](https://github.com/myaaghubi/PHP-Frameworks-Bench/actions/workflows/test.yml/badge.svg)](https://github.com/myaaghubi/PHP-Frameworks-Bench/actions/workflows/test.yml) ![GitHub release (latest by date)](https://img.shields.io/github/v/release/myaaghubi/PHP-Frameworks-Bench?color=purpol) ![GitHub](https://img.shields.io/github/license/myaaghubi/PHP-Frameworks-Bench?color=green)


This project attempts to measure the minimum overhead (minimum bootstrap cost) of PHP frameworks in the real world.

So I think the minimum should not include:
Expand All @@ -16,9 +17,11 @@ Benchmarking on components like template engines or ORM/Database libraries is ou
- [Benchmarks](#benchmarks)
- [Latest](#latest)
- [OPCache On/Off](#opcache-on)
- [Benchmarking Policy](#benchmarking-policy)
- [How to Benchmark](#how-to-benchmark)
- [Docker](#docker)
- [Commands](#commands)
- [Benchmarking Policy](#benchmarking-policy)
- [Add Your Framework](#add-your-framework)
- [Donate](#-donate)
- [References](#references)
- [License](#license)
Expand All @@ -41,12 +44,12 @@ Benchmarking on components like template engines or ORM/Database libraries is ou

#### Results (2023/5/15)

These are my benchmarks, not yours. **I encourage you to run on your (production equivalent) environments.**
These are my benchmarks, not yours. **I encourage you to run on your -production equivalent- environments.**

![Benchmark Results Graph Throughput](screenshots/php-frameworks-bench-throughput.png)
![Benchmark Results Graph Memory](screenshots/php-frameworks-bench-memory.png)
![Benchmark Results Graph Execution Time](screenshots/php-frameworks-bench-exectime.png)
![Benchmark Results Graph Included Files](screenshots/php-frameworks-bench-includedfiles.png)
![Frameworks Benchmark Results Graph Throughput](screenshots/php-frameworks-bench-throughput.png)
![Frameworks Benchmark Results Graph Memory](screenshots/php-frameworks-bench-memory.png)
![Frameworks Benchmark Results Graph Execution Time](screenshots/php-frameworks-bench-exectime.png)
![Frameworks Benchmark Results Graph Included Files](screenshots/php-frameworks-bench-includedfiles.png)

|framework |requests per second (rps)|relative (rps)|peak memory|relative (mem)|
|-------------------|------------------------:|-------------:|----------:|-------------:|
Expand All @@ -71,29 +74,13 @@ These are my benchmarks, not yours. **I encourage you to run on your (production


#### OPCache On
Check out the video for more information.
[![Results with OPCache On/Off & How to add your framework](http://img.youtube.com/vi/Dk8YHQZ6jfY/0.jpg)](http://www.youtube.com/watch?v=Dk8YHQZ6jfY)


## Benchmarking Policy

This is for the `main` branch.

* Frameworks installed via `composer` according to their official documentation.
* Used the default configuration.
* Considering the minimum changes on frameworks to run the benchmark.
* Didn't remove any components/configurations even if there is no use for them.
* We have a `controller` class to get the `Hello World` for each, based on the default template of each framework.
* Turned off the `debug` mode and set the environment to `production` mode.
* Considered general optimization for the production environment, like `--optimize-autoloader` for the composer.

Some frameworks are optimized more than others, so some people may think using default configuration is not fair. The dept of optimizing a framework depends on the experiences of the developer too, so it's the rabbit hole and there is no point in it. I think the default configuration of frameworks is a good starting point to get ranking.

If you find something wrong in my code, feel free to send a PR. But please note optimizing for the "Hello World" is not acceptable! Building the fastest `Hello World` application is not the goal of this project.


## How to Benchmark

If you want to have benchmarks on `PHP extension frameworks` like `Phalcon`, you need to install the extension first based on its own documentation.
If you want to have benchmarks on `PHP extension frameworks` like `Phalcon`, you need to install the extension first, based on its own documentation.

1- Download & Setup:

Expand All @@ -104,7 +91,7 @@ $ git clone https://github.com/myaaghubi/PHP-Frameworks-Bench.git
$ cd PHP-Frameworks-Bench

# optional
$ nano benchmark.config
$ nano config

# run the setup & follow the progress
$ yes | bash setup.sh
Expand All @@ -115,29 +102,59 @@ $ yes | bash setup.sh
```bash
$ bash check.sh
# bash check.sh -t pure-php
# /------- pure-php: checking... done.
# pure-php
```

3- Run benchmarks:

```bash
# bash benchmark.sh --help
$ bash benchmark.sh
```

4- Check the results:
- web:

<http://localhost/PHP-Frameworks-Bench/>
<http://127.0.0.1/PHP-Frameworks-Bench/>

- terminal:
```bash
# bash results.sh --help
bash results.sh
```
bash show-table.sh
```


## Docker

Results with docker may not be reliable but in a situation you can use it like:

1- Change the `base` in `config` on the right port(`8080` considered):

```ini
base="http://127.0.0.1:8080/PHP-Frameworks-Bench"
...
```

2- Run/download the docker:

```bash
$ bash docker-apache.sh
```

3- Run the benchmark:

```bash
# run it in another terminal
$ bash benchmark.sh
```


## Commands

```bash
# use bash benchmark.sh --help
$ bash benchmark.sh -f -rapache -t pure-php

# run composer update for frameworks
$ bash update.sh

Expand All @@ -149,27 +166,47 @@ $ bash clean.sh
# clear the cache of frameworks
$ bash clear-cache.sh

# show the results of the last benchmark
$ bash show-table.sh
# show the results table
$ bash results.sh
```

To specify frameworks, put them with `-t ...` after each command:

```bash
# supported for `setup.sh`, `benchmark.sh`, `update.sh`, `clean.sh`, and `clear-cache.sh`
# bash benchmarks.sh --help -h
# bash benchmarks.sh --help
$ bash benchmark.sh -t laravel-10.0/ slim-4.11/ ...
...
```


## Add Your Framework
Check out the [Benchmarking Policy](#benchmarking-policy), to get more information watch the video [OPCache On/Off](#opcache-on).


## Benchmarking Policy

* Use `composer` to install frameworks according to their official documentation.
* Use the default configuration.
* Minimum changes on frameworks to have benchmarks.
* Don't remove any components/configurations even if there is no use for them.
* Include a `controller` class to get the `Hello World!` for each, based on the default template of each framework for `controllers`.
* Turn off the `debug` mode and set the environment to `production` mode.
* General optimization for the production environment, like `--optimize-autoloader` for the `composer`.

Some frameworks are optimized more than others, so some developers may think using default configuration is not fair. The dept of optimizing a framework depends on the skills/experiences of the developer too, so it's the rabbit hole and nonsense for ranking. Please **note** optimizing for the `Hello World` is not acceptable! Building the fastest `Hello World` application is not the goal of this project. I think the default configuration of frameworks is a good starting point to have a ranking.

If you find something wrong in my code, feel free to send a `PR`.


## 🍔 Donate
Don't forget to donate if you find it useful ☕ 🍺 🍸 🍔

ETH: 0x0ADd51D6855d2DF11BB5F331A3fa345c67a863b2

![Ethereum](screenshots/ethereum.jpg?raw=true "Ethereum")


## References
Note: This project is based on
[php-framework-benchmark](https://github.com/kenjis/php-framework-benchmark), thanks to [Kenjis](https://github.com/kenjis). It is very old and abandoned, so I decided to split it from the origin and update it separately.
Expand Down
7 changes: 3 additions & 4 deletions base/_functions.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
benchmark () {
fw="$1"
url="$2"
output_wrk="output/$fw.wrk.log"
output="output/$fw.output"
output_wrk="output/$dir_datetime/$fw.wrk.log"
output="output/$dir_datetime/$fw.output"

# check out the appropriate response is reachable
url_status=$(bash check.sh -t "$fw")
Expand Down Expand Up @@ -40,8 +40,7 @@ benchmark () {

rps=`grep "Requests/sec:" "$output_wrk" | tr -cd '0-9.'`

echo "rps: "
numfmt --g "$rps"
echo "rps: $rps"

# to make a small gap between the WRK and CURL
sleep 1
Expand Down
35 changes: 10 additions & 25 deletions base/hello_world.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,21 @@

cd `dirname $0`
. ./_functions.sh
. ../benchmark.config
. ../config

bm_name=`basename $0 .sh`

results_file="output/results.$bm_name.log"
check_file="output/check.$bm_name.log"
error_file="output/error.$bm_name.log"
url_file="output/urls.log"
bn_name=`basename $0 .sh`

cd ..

if [ -f "$results_file" ]; then
echo "moving $results_file to $results_file.old"
mv "$results_file" "$results_file.old"
fi

if [ -f "$check_file" ]; then
echo "moving $check_file to $check_file.old"
mv "$check_file" "$check_file.old"
fi
mkdir -p output

if [ -f "$error_file" ]; then
echo "moving $error_file to $error_file.old"
mv "$error_file" "$error_file.old"
fi
export dir_datetime=`date +%y-%m-%dT%H-%M-%S`
mkdir output/$dir_datetime

if [ -f "$url_file" ]; then
echo "moving $url_file to $url_file.old"
mv "$url_file" "$url_file.old"
fi
results_file="output/$dir_datetime/results.log"
check_file="output/$dir_datetime/check.log"
error_file="output/$dir_datetime/error.log"
url_file="output/$dir_datetime/urls.log"

phpc=`curl -s "$base/libs/php_config.php"`
echo "/------- PHP Config -------/"
Expand All @@ -52,7 +37,7 @@ do
# continue
# fi

if [ "$param_clean" = true ]; then
if [ "$param_fresh" = true ]; then
bash clean.sh
bash setup.sh -t "$fw"
fi
Expand Down
11 changes: 9 additions & 2 deletions base/option_target.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function showHelp()
{
cat << HEREDOC
Usage: bash check.sh [-t pure-php/ slim-*]
Usage: bash check.sh [-t pure-php slim-*]
Optional Arguments:
-h, --help Show this help message and exit
Expand All @@ -30,6 +30,7 @@ IFS=';'
params=(`php ./libs/strreplace.php " -" ";-" " ${paramsin}"`)
IFS=$oldIFS

init_benchmark=true
for option in "${params[@]}"
do
case "$option" in
Expand All @@ -41,13 +42,19 @@ do
fi
;;
-h|--help)
init_benchmark=false
showHelp;
;;
""|" ")
;;
*)
init_benchmark=false
echo "\"${option}\" not available"
exit 1
;;
esac
done
done

if [ "$init_benchmark" = false ]; then
exit 1
fi
2 changes: 1 addition & 1 deletion base/show_fw_array.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
cd `dirname $0`
cd ..

. ../benchmark.config
. ../config

# include framework list
targets="$frameworks_list"
Expand Down
Loading

0 comments on commit c584dbb

Please sign in to comment.