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 Jenkins+k8s部署slave节点offline
  • ¥15 微信小游戏反编译后,出现找不到分包的情况
  • ¥15 如何实现从tello无人机上获取实时传输的视频流,然后将获取的视频通过yolov5进行检测
  • ¥15 WPF使用Canvas绘制矢量图问题
  • ¥15 用三极管设计一个单管共射放大电路
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'
  • ¥15 nirs_kit中打码怎么看(打码文件是csv格式)
  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题