I work with Windows 10 (WSL 2). My hardware is:
Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz 2.60 GHz RAM 8.00GB SSD
Actually, this is a game laptop (MSI GL 65 95CK) if you are interested.
I decided to install Laravel, went to documentation and implemented described steps:
- In WSL terminal (I use Ubuntu)
curl -s "https://laravel.build/example-app?with=mysql,redis" | bash
cd example-app && /vendor/bin/sail up
I went to browser and realized that the main page took almost 1 second to render! Sometimes even two seconds!
I thought, "ok, maybe the framework is in a not optimized mode. Debug and so on", and decided to turn APP_DEBUG
in .env
to false
. I also removed all routes and put this instead:
Route::get('/', [\App\Http\Controllers\TestController::class, 'test']);
Before this, I created the TestController
:
class TestController extends Controller
{
public function test() {
return response()->json([
'name' => 'Abigail',
'state' => 'CA',
]);
}
}
Then I run php artisan optimaze
, open in browser http://localhost/api
and the result is a big sorrow:
Why 800ms? I did not do anything.
Ok, I decided just to rename the index.php
file in the public
folder to index2
for example, put new one index.php
with the array printing just to test whether this is a Laravel problem or this is just an infrastructure issue.
New index.php
:
Much better! Then I thought, "let's compare with another framework, for example with .NET Core". And I made a very simple Web Api project. Controller:
namespace MockWebApi.Controllers
{
[ApiController]
[Route("")]
public class MainController : ControllerBase
{
[Route("test")]
public IActionResult Test()
{
return Ok(new
{
Test = "hello world!!!"
});
}
}
}
The result is:
Ok, you can argue that this is a compiled language. I decided to check with Node.js and Express:
Code:
router.get('/', function(req, res, next) {
res.json({"test": "123"})
});
Result:
As you can see, Node as fast as C# in this case.
So, what is wrong with Laravel? Did I miss something in installation?
UPDATE
I raised Laravel without Sail. My docker-compose file:
version: '3'
services:
php-fpm:
build:
context: docker/php-fpm
volumes:
- ./:/var/www
networks:
- internal
nginx:
build:
context: docker/nginx
volumes:
- ./:/var/www
ports:
- "80:80"
depends_on:
- php-fpm
networks:
- internal
networks:
internal:
driver: bridge
Nginx Dockerfile:
FROM nginx
ADD ./default.conf /etc/nginx/conf.d/default.conf
WORKDIR /var/www
Nginx config:
server {
listen 80;
index index.php;
server_name 127.0.0.1 localhost;
root /var/www/public;
location / {
try_files $uri /index.php?$args;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php-fpm:9000;
fastcgi_index index.php;
fastcgi_read_timeout 1000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
php-fpm Dockerfile:
FROM php:7.4-fpm
RUN apt-get update && apt-get install -y wget git unzip \
&& apt-get install libpq-dev -y
RUN wget https://getcomposer.org/installer -O - -q \
| php -- --install-dir=/bin --filename=composer --quiet
RUN groupadd -r -g 1000 developer && useradd -r -u 1000 -g developer developer
USER developer
WORKDIR /var/www
I did not get any performance improvement(