douluhaikao93943 2012-07-20 03:23
浏览 55
已采纳

PHP和MySQL:找出导致大量CPU负载的文件或查询[关闭]

Here is my top output :

6747 example.com 15 0 241m 19m 8188 R 29.9[CPU] 0.2[RAM] 3:24.89 php

31798 mysql 15 0 1952m 422m 5384 S 12.0[CPU] 5.3[RAM] 9:00.27 mysqld

Look at PHP's CPU usage. I don't know which file is causing this load. Is there any module for show which file is using CPU under tree? I mean like this :

6747 example.com 15 0 241m 19m 8188 R 29.9[CPU] 0.2[RAM] 3:24.89 php

                     5.49%                          index.php
                    15.39%                         videos.php
                        x%                              y.php

31798 mysql 15 0 1952m 422m 5384 S 12.0[CPU] 5.3[RAM] 9:00.27 mysqld

and I want same thing for mysql too. I want to now which query is executing now and how long is it take.

  • 写回答

3条回答 默认 最新

  • dongqiao9015 2012-07-20 03:44
    关注

    The simplest options (not saying the most accurate, but the simplest) will be:

    • For PHP, if you want to compare one PHP file against another, assume that the process that takes the longest is using most CPU. Use a network profiler (in-built into Chrome and IE, or Firebug) to find out which process takes longest. Failing that, user fiddler (on Windows) or Charles (Mac). Remove the data response times and average out the rest = time to generate response approx = CPU usage. Note that this will include external calls, memcached calls, mySQL calls etc.

    • for mySQL, the slow query log is invaluable. (Will need to be configure in my.cnf and restart the server - but it should be considered compulsory.)

    • For more diagnostics, Xdebug (as suggested by Xesued) will help you profile individual parts of scripts (as well as debug). Not recommended for production, though, as it will slow you down further.

    • Another crude way is to "echo microtime(true);" at various places in your script, or pump that info to a log file. (Open log file at start of script, and at various points record the microtime - look for the large gaps.)

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

报告相同问题?

悬赏问题

  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)