Laravel API speed (too slow)

五迷三道 提交于 2020-01-25 20:27:25

问题


i'm trying to develop an API to store alarms coming from several applications into a database. I'm developing the api with Laravel.

I've made a C program which makes multiple post requests to the API in order to see how many request can the laravel api process.

I have the following route in api.php

Route::post('/alarm', 'Api\v1\AlarmController@store');

In my controller i have made the store function which stores the alarm values received in the post request into the database

function store(Request $request)
    {

        $content = $request->getContent();

       $json_content = json_decode($content);

        $id = $this->alarm_model->newAlarm($json_content);

        echo '{ "result": '.$id.', "msg":'.$content.' }';

    } 

Then i have a Alarm model which stores the json values in the database alarm table.

If i make 1000 requests it is not able to process all of them. I get HttpSendRequest Error : 12002 Internet Timeout

Am I doing something wrong? How many requests per second allows Laravel framework?


回答1:


There's no limit, as far as i know, it all depends on your environment, if it's a small server i doubt it would be able to handle 1000 requests per second.

What you could do instead is make use of Laravel Queues

What i think is happening on your end is that your server gets too busy when trying to complete the 1000 requests at once. This results in the server responding with the "I'm Busy" signal.

But with a queue you could limit the pressure on the server and make it happen over the course of a few minutes?

How To:

  1. Create a Job php artisan make:job ProcessAlarms

  2. In the Job's Handle method you put the logic to save the Alarm

  3. Then in your Controller's store method use this dispatch(new ProcessAlarm($data_you_want_to_pass));



来源:https://stackoverflow.com/questions/40129065/laravel-api-speed-too-slow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!