Laravel API 速度(太慢) [英] Laravel API speed (too slow)

查看:89
本文介绍了Laravel API 速度(太慢)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发一个 API,以将来自多个应用程序的警报存储到数据库中.我正在用 Laravel 开发 api.

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

我制作了一个 C 程序,它向 API 发出多个 post 请求,以便查看 laravel api 可以处理多少个请求.

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.

我在 api.php 中有以下路由

I have the following route in api.php

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

在我的控制器中,我创建了 store 函数,该函数将在 post 请求中收到的警报值存储到数据库中

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.' }';

    } 

然后我有一个警报模型,它将 json 值存储在数据库警报表中.

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

如果我发出 1000 个请求,它无法处理所有请求.我收到 HttpSendRequest 错误:12002 Internet 超时

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

我做错了吗?每秒多少个请求允许 Laravel 框架?

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

推荐答案

没有限制,据我所知,这完全取决于您的环境,如果是小型服务器我怀疑它会每秒能够处理1000个请求.

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.

您可以做的是使用 Laravel 队列

我认为您那边发生的事情是您的服务器在尝试一次完成 1000 个请求时变得太忙.这导致服务器以我很忙"信号响应.

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?

操作方法:

  1. 创建作业 php artisan make:job ProcessAlarms

在 Job 的 Handle 方法中,您放置了保存警报的逻辑

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

然后在你的控制器的 store 方法中使用这个 dispatch(new ProcessAlarm($data_you_want_to_pass));

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

这篇关于Laravel API 速度(太慢)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆