问题
I want to make my website faster. I know I can make CSS sprites and compress my HTML and CSS.
Are there other ways I can optimize my page speed?
An example is this page: http://www.vinderhimlen.dk/konkurrencer/vind-elektronik
It takes several seconds to load the share buttons, rating stars and Facebook likes. I would really want to optimize it. I just don't think that minimizing the HTML and CSS is enough.
回答1:
No matter what you do to scale or increase performance of Rails you will want to take benchmarks before and after. It is easy to install the latest performance-enhancing plugin or copy someone else's technique, but without measuring execution times you will never know whether the things you are doing increase performance or degrade it.
Performance is relative and can vary based on many factors. There are two aspects of performance that are important for scaling Rails:
- Latency -- How fast can we answer a request? This is measured as the time elapsed between request and response.
- Throughput -- How many requests can we process over a given amount of time? This is measured as responses per second.
After we do our general performance testing we can easily see bottlenecks begin to appear. At that point we can further use packaged tools to narrow the field of focus to one or more of the following, which are in order of most common to least common:
- Database problems: Poor custom SQL, incorrect or non-optimal joins and associations, overly large database tables, etc.
- Model problems: Business rules are overly complex or incorrectly designed.
- Front-end view problems: Poor HTML or CSS choices, extremely large javascript libraries, un-optimized images, etc.
- Transactional problems: Too few Mongrel (or other app server) instances running, not enough hardware, poorly written or non-optimal controllers, incorrectly architected application, etc.
Often people think of transactional problems as the cause of poor performance, when more often it is caused by the other types of problems.
If you have poorly-designed custom SQL, extremely large database tables, or non-optimal joins or associations, no matter how much hardware you throw into the mix you may never increase performance. Your Rails application just won't scale. The same thing applies for some transactional problems. Most scalability problems occur when bad practices were followed during design and coding phases of development.
The following tools will be helpful in determining where your bottlenecks live...
- "query_analyzer" - Bob Silva's MySQL tool for automatically EXPLAINing each query.
- "ruby-prof" - A fast Ruby code profiler written in C.
- "pl_analyze" - Written by Eric Hodel, this analyzes logger output from SysLogLogger.
- "yslow" - A Firebug plugin to measure and determine why a particular page is slow.
- "YUI compressor" - A Javascript/CSS compression tool from the performance team at Yahoo.
Once you determine where the problems exist it is much easier to zero-in and address each issue.
回答2:
You can do little things as you suggest, however probably the biggest improvement would come by using selective caching or fragment caching.
Cache everything you can that does not change often. You can always invalidate parts of the cache when they change.
This was the only way I was able to make by RoR site even tolerably fast.
来源:https://stackoverflow.com/questions/6324335/rails-how-to-optimize-my-website