性能调优 CakePHP 应用程序 [英] Performance tuning CakePHP application

查看:53
本文介绍了性能调优 CakePHP 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚得到了这个相当大的 CakePHP 应用程序(大约 20k 行代码),它不是很干净,而且根本没有文档.该应用程序正在生产中运行,但它在性能方面确实存在重大问题.

I just got this quite large CakePHP app (about 20k lines of code), which isn't very clean and there is no documentation at all. The app is running in production, but it has really major problems with performance.

服务器是具有 8GB RAM 的四核,但该应用程序只能处理大约 3-4 个请求/秒,这是非常非常糟糕的.每个请求大约占用所有四个 CPU 的 20-30%.

Server is Quad core with 8GB RAM, but the app can serve only about 3-4 requests/s, which is very very bad. Each request takes about 20-30% of all four CPUs.

当我尝试像 ab -n 100 -c 10 ... 这样的小负载测试时,它的平均响应高达 7000 毫秒.然而,我从来没有让它超过 800MB 的 RAM,所以至少有 6GB 的可用 RAM 用于一些调整.

When I try even little load test like ab -n 100 -c 10 ..., it goes up to 7000ms average response. However, I never made it over 800MB RAM, so there is at least 6GB free RAM for some tweaking.

问题是,我还没有能够创建工作的开发实例,所以我必须在生产中对其进行调整......

The problem is, that I haven't been yet able to create working development instance, so I have to tune it in production ...

对于简单提高性能,无需过多挖掘源代码,您有什么建议?

What do you recommend for easy boosting the performance, without too much digging into source code?

推荐答案

第 1 步:确保是应用程序,而不是 Web 服务器

在 Cake 层次结构之外创建一个简单的 hello world 文件

Step 1: Make sure it's the application, and not the web server

Create a simple hello world file outside the Cake hierarchy

<?php
echo 'Hello World';

看看运行需要多长时间.有时很容易将服务器/网络级别上发生的事情归咎于应用程序.

And see how long that takes to run. Sometimes it's easy to blame the application for something that's going on on the server/network level.

假设 test.php 在合理的时间内呈现,继续第二步.

Assuming test.php renders in a reasonable amount of time, move on to step two.

摆弄生产代码总是危险的游戏.在你开始之前做一个完整的数据库备份,以防你损坏了无法修复的东西,并复制整个蛋糕目录树.每当您完成一天的工作时,将生产目录的内容和您的副本(使用 GUI 工具或命令行)进行比较

Fiddling with production code is always a dangerous game. Before you start do a full database backup in case you corrupt something beyond repair, and copy the entire cake directory tree. Whenever you're done for the day, diff the contents of the production directory and your copy (using a GUI tool or the command line)

diff -r production-cake copy-of-cake

第 3 步:数据库几乎总是 LAMP 堆栈的第一个瓶颈

PHP 应用程序会生成大量 SQL 查询,尤其是当人们使用 ActiveRecord 样式模型时,该模型隐藏了大量实际的 SQL 查询.您需要将 Cake 设置为将查询记录到文件和/或数据库表中.在这里有一些关于这样做的说明,尽管我建议注销到平面文件和/或系统日志而不是数据库.将 DB 请求记录到数据库将使每个页面加载的查询数量增加一倍.

Step 3: The Database is Almost Always your First Bottleneck With the LAMP Stack

PHP applications generate a lot of SQL queries, particularly when people are using an ActiveRecord style model which hides a lot of the actual SQL querying. You'll want to set Cake up to log queries to a file and/or to a database table. There's some instructions here on doing this, although I'd recommend logging out to a flat file and/or the syslog instead of the database. Logging DB requests to the database will double the number of queries per page load.

我还建议添加 IP 检查,以便它只记录来自您的 IP 地址的请求.这样您的日志记录就不会显着干扰应用程序的正常运行.

I'd also recommend adding in an IP check so it only logs requests coming from your IP address. That way your logging doesn't dramatically interfere with the regular running of the application.

一旦到位,发出一个请求,然后查看正在生成的 SQL.寻找一遍又一遍重复的相同查询,作为您可以放入一些缓存以获得性能提升的地方.还要寻找顺序查询

Once this is in place, make a single request and then look at the SQL that's being generated. Look for identical queries being repeated over and over again as a place where you can drop in some caching to get a performance boost. Also look for sequential queries

select * from foo where id = 5
select * from foo where id = 6
etc...

这表明有人在不了解幕后发生的事情的情况下循环加载模型.

Which indicate someone's loading up models in a loop without understanding what's going on behind the scenes.

如果数据库不是瓶颈并且 PHP/Apache 运行正常,那么接下来要查找的是 系统调用.脱壳是一种快速而肮脏的完成工作的方式,但它的操作非常昂贵.循环使用其中的一两个,您就完成了.

If the database isn't yoru bottleneck and PHP/Apache are functioning properly, the next thing to look for is system calls. Shelling out is a quick and dirty way to get things done, but its a hugely expensive operation. Get one or two of those in a loop and you're done for.

在您的生产服务器上运行 topps 并查找正在启动和停止的程序,然后在代码库中搜索这些命令.

Run top or ps on your production server and look for programs that are starting and stopping, then search through the code base for those commands.

您将拥有多个控制器

/app/controllers/posts_controller.php
/app/controllers/other_controller.php
etc...

将对应于 URL

http://www.example.com/posts/methodName
http://www.example.com/other/methodName
etc...

每当您需要调试特定请求以找出为什么速度如此之慢时,请复制控制器.

Whenever you need to debug a particular request to figure out why it's so slow, make a copy of the controller.

/app/controllers/debugposts_controller.php

并手动发出请求

http://www.example.com/debugposts/methodName

然后您可以将任意数量的调试/打印语句放入控制器文件中.如果你很幸运",最初的开发人员可能在控制器文件中塞了很多逻辑.如果是这种情况,您现在就可以玩注释掉一半代码"的游戏了.

Then you can throw as many debug/print statements as you want into the controller file. If you're "lucky", the original developers probably jammed a lot of logic in the controller files. If that's the situation, you're now in a position to play the "comment out half the code" game.

这篇关于性能调优 CakePHP 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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