1

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:

  1. In WSL terminal (I use Ubuntu) curl -s "https://laravel.build/example-app?with=mysql,redis" | bash
  2. 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! enter image description here

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:

enter image description here

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:

enter image description here

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:

enter image description here

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:

enter image description here

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(

Aleksej_Shherbak
  • 2,757
  • 5
  • 34
  • 71
  • 3
    How do you run your php server? What version? is xDebug on? can you try it without ssl? Most probably it's not the laravel setup, but the webserver / php setup that is causing it to be slow. – Frnak Jul 27 '21 at 14:07
  • Everything was according to documentation. It means, I used Sail https://laravel.com/docs/8.x/sail Just go to this url https://laravel.build/example-app and u will see what Sail runs. – Aleksej_Shherbak Jul 27 '21 at 14:13
  • I just want to say that I did not have installed PHP/Server or something from the stack. I used Sail. – Aleksej_Shherbak Jul 27 '21 at 14:15
  • As a framework, there _is_ some overhead too, i.e. if you did a basic `index.php` file, without laravel, and served it via `localhost/index.php` (direct reference, no routing, etc.), it would be faster than Laravel. You could try caching (config/route caching) locally so help speed things up too. All that being said, not sure what you expect from this question; what is your expected outcome? Advice on how to speed up Laravel? Recommendations on a different framework to use? Something else? – Tim Lewis Jul 27 '21 at 14:15
  • a little overhead is normal - yes, but from my experience 1 sec for nothing is way too long. I never worked with sail so I cannot help here any further - sorry. – Frnak Jul 27 '21 at 14:18
  • @TimLewis I just want to understand why I just installed the framework and already have some performance problem. It should be easy to run something simple, like in my example. – Aleksej_Shherbak Jul 27 '21 at 14:19
  • 2
    So I got curious and did a fresh sail setup. Default page loaded in 76ms. The json string in your test function loaded in 49ms. So, something in your setup must be the culprit. – user3532758 Jul 27 '21 at 14:41
  • @user3532758 thank you! And what operating system do you use? Did you set up something additionally (I mean PHP/web server)? Just all according to Laravel documentation? – Aleksej_Shherbak Jul 27 '21 at 14:47
  • 1
    This is windows 10. Using WSL2. Hardware is inferior to yours; i7 6500U 2.50GHz 16GB RAM SSD and, yes, sail with mysql setup all according to laravel documentation. – user3532758 Jul 27 '21 at 14:53
  • @user3532758 thank you. Something is interfering to work the PHP stack on my machine. Strange. – Aleksej_Shherbak Jul 27 '21 at 14:54
  • 1
    Yeah definitely. I just tested one of the huge websites I have on my machine, which calls external resources as well without any optimisations or caching. It also took only 200ms to load everything. – user3532758 Jul 27 '21 at 14:56
  • It seems I'm not the only one who faced with this problem https://stackoverflow.com/questions/63036490/docker-is-extremely-slow-when-running-laravel-on-nginx-container-wsl2 – Aleksej_Shherbak Jul 27 '21 at 15:12
  • 1
    So the problem is not in Laravel and not in PHP. WSL2 is the reason – Aleksej_Shherbak Jul 27 '21 at 15:31
  • Ah I thought so! Were you running docker from the windows filesystem? – user3532758 Jul 27 '21 at 15:42
  • @user3532758 yes) Did you read https://stackoverflow.com/questions/63036490/docker-is-extremely-slow-when-running-laravel-on-nginx-container-wsl2 ? My question is duplicate. The answer what is going on is over there. So, the conclusion here is to use Linux for Linux friendly things (like PHP for example). – Aleksej_Shherbak Jul 27 '21 at 15:51
  • 1
    Does this answer your question? [Docker is extremely slow when running Laravel on Nginx container wsl2](https://stackoverflow.com/questions/63036490/docker-is-extremely-slow-when-running-laravel-on-nginx-container-wsl2) – Mark Rotteveel Apr 25 '22 at 09:48

0 Answers0