bmpf's avatar
Level 3

Execution time issues on server. ( 150 ms average localhost vs 300 ms average on live server )

Hello,

Recently I made a eCommerce website using laravel as api. Everything is working fine but there is one thing bothering me. In my live server the average request is 300 ms , and in localhost is 150 ms [average] - Takes twice the time. I can't understand why that happens.

  • I downloaded the exact same project to my localhost ,
  • Set up the database to be connected to the live project ( Instead of the localhost in the env file, I put the IP address of the live server ) ,
  • The same thing happens.

Also I used an PHP BENCHMARK SCRIPT and the results are :

  • Live Server
Start : 2020-01-03 23:19:12
PHP version : 7.3.13
Platform : Linux
--------------------------------------
test_ifelse               : 0.160 sec.
test_loops                : 0.238 sec.
test_stringmanipulation   : 0.349 sec.
test_math                 : 0.252 sec.
--------------------------------------
Total time:               : 0.999 sec.
  • Localhost Server :
Start : 2020-01-04 00:19:48
Server : localhost@::1
PHP version : 7.3.12
Platform : WINNT
--------------------------------------
test_math                 : 0.275 sec.
test_stringmanipulation   : 1.686 sec.
test_loops                : 0.610 sec.
test_ifelse               : 0.369 sec.
--------------------------------------
Total time:               : 2.94 sec.

Example :

Localhost : Query with 855 results : 164 ms
Live Server : Query with 855 results : 338 ms

Version : 6.0.4

Running this piece of code :

Route::get('/testing', function() {
    return "Testing";
});
  • Live Server : 321 ms
  • Localhost : 114 ms
0 likes
13 replies
corbosman's avatar

Those response times are really high. If i add the above route to my current project using valet it takes about 15-20ms to respond. My first guess, do you have xdebug turned on? If so, turn it off and try again.

bugsysha's avatar

Maybe your internet path to that server is not fast enough. Try using some service to see response times.

bmpf's avatar
Level 3

xdebug is not enable :/

bmpf's avatar
Level 3

The internet is fast. As told the localhost project is getting the data in the server.

Tray2's avatar

I suggest you install laravel debugbar and check what takes time.

It might be that your local machine caches better than you live server.

bmpf's avatar
Level 3

Hello, I installed on my local machine :

Code :

Route::get('/testing', function() {
    return "Testing";
});

In the timeline I get this :

  • Current Project
1 x Booting (79.71%) 101.35ms
1 x Application (19.16%) 24.36ms
  • Fresh Laravel APP :
1 x Booting (72.2%) 73.91ms
1 x Application (25.79%) 26.40ms
bmpf's avatar
Level 3

This is very weird :

1 x Time for getting one product (48.73%) > 112.47ms

    public function show ($search_slug)
    {
        echo 'Testing';
        Debugbar::startMeasure('getProduct', 'Time for getting one product');
            $product = Product::where('product_id', 1)
            ->where('product_active', '=' , 1)
            ->firstOrFail( [ 'product_id'
            ] );
        Debugbar::stopMeasure('getProduct');
    }
corbosman's avatar

Does that table have a proper index? You can try to take the query from the debugbar and manually run it. You can then try to use the mysql EXPLAIN command to see if it uses the indices.

Cronix's avatar

Is your localhost hardware the exact same as the server? Likely not. Is your server dedicated, or sharing resources with others? When identical code behaves differently on different hardware, I wouldn't assume it's the software right off the bat.

You're benchmarking different machines using the same software, and getting different results...

bmpf's avatar
Level 3

Yes it is with proper index. I think its the domain. I have a vps with 2 domains. The same script runs faster in the main domain.

bmpf's avatar
Level 3

Yes, in theory the live server is better than my localhost. But its not giving me the best results as showed by the benchmark .

bmpf's avatar
Level 3

this piece of code

<?php die('testing'); ?>

In the same vps running on different domains, the main domain gets 3ms to 15 ms , the other gives me 80ms to 180 ms , problem is the waiting (TTFB) Image : https://i.ibb.co/LdHm6Sg/report.png

bmpf's avatar
bmpf
OP
Best Answer
Level 3

Resolved . Server error.

Please or to participate in this conversation.