问题
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:
Create a Job
php artisan make:job ProcessAlarms
In the Job's Handle method you put the logic to save the Alarm
Then in your Controller's
store
method use thisdispatch(new ProcessAlarm($data_you_want_to_pass));
来源:https://stackoverflow.com/questions/40129065/laravel-api-speed-too-slow