dongmu3187 2016-01-26 15:04 采纳率: 0%
浏览 55
已采纳

使用Symfony进行Ajax响应时间

Ajax is taking 1sec to load in dev and 500ms in prod with Symfony2 with a really simple call :

Javascript :

$.ajax({
            url: "{{ path('my_path') }}",
            type: 'GET',
            data: {x: x},
            dataType: 'json',
        }).done(function(res) {

        });

Controller :

/**
 * @Route("/my_path", name="my_path", condition="request.isXmlHttpRequest()")
 * @Method("GET")
 */
public function myPathAction(Request $request)
{
    return new JsonResponse('');
}

If I change the url in my ajax call by

url: "test.php"

With test.php :

return json_encode('');

In both case we are doing the same thing but accessing to a controller take 500ms in prod and accessing to "test.php" takes 20ms.

Why does Symfony takes that much time to access to the route and how can I make it faster ?

  • 写回答

2条回答 默认 最新

  • doutuo1939 2016-01-26 15:27
    关注

    Please not that the Symfony action is also checking if it's a GET request and a XmlHttpRequest, etc. etc. So even if you can make it faster, you'll never get it as fast as 'vanilla' PHP. A framework like Symfony is a great toolbox, but will add some overhead in return.

    Performance is really important, but very complex. There are 1000 things you can do to get a better performance. Just some thoughts:

    • First of all: read the documentation about Performance in the Symfony Book. Opcache/APC really helps.
    • Use the Web Debug Toolbar to see what's happening during a request. Is it the router part? Security? Or the templating?
    • Use your browser's console (F12 for the Chrome Console) to see what's the difference between test.php and /my_path. Check the request/response headers to see what's the difference.
    • I just switched to PHP 7 and the performance increase is impressive.

    Somebody downvoted your question and I can imagine why. When I started using Symfony, performance was a big concern for me as well. So is it for many other developers, so there are many other articles, questions, etc. you can find. Please read the documentation, search on Google/StackOverflow, etc.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么