为什么我的 sinatra 网站这么慢?

问题描述:

在询问这个问题后, 我开始使用 Sinatra 作为提供网页的一种方式.

After asking this question, I started using Sinatra as a way to serve web pages.

今天晚上,我和一个朋友开始测试服务器的速度.

This evening, a friend of mine and I started to test the speed of the server.

登录文件如下:

require 'rubygems'
require 'sinatra'
require 'haml'

enable :sessions #for cookies!

get '/' do 
  haml :index 
end

index.haml 看起来像:

And the index.haml looks like:

%title
  First Page

%header 
  %h2 First Page

他和我一样坐在最近的笔记本电脑上,我们两人之间有一个 Apple 802.11n 路由器.我们都运行 Windows 7.我还在运行 Ubuntu 9.10 x64 和 Sinatra 的笔记本电脑上尝试了这些相同的文件,以及从 apt-get 安装的所有相关文件.

He's sitting on a recent laptop, as am I, with an Apple 802.11n router between the two of us. We're both running Windows 7. I've also tried these same files on a laptop running Ubuntu 9.10 x64 with Sinatra and all relevant files installed from apt-get.

无论服务器操作系统、Windows 还是 Linux,Sinatra 都需要 7 秒来处理单个页面请求.我看到 here 作者设法每秒处理超过 400 个请求.是什么赋予了?(或者这应该在 SuperUser 上还是类似的?)

Sinatra is taking 7 seconds to serve up a single page request, no matter the server OS, Windows or Linux. I see that here the author managed to get over 400 requests/second processed. What gives? (or should this be on SuperUser or the like?)

对于何时应该优化 Web 应用程序,我将搁置任何意见.

I'll set aside any opinions on when you should optimize your web application.

在您的 Sinatra 应用程序中为开发和生产设置不同的配置,因为其中一些建议,您不会总是想要使用.事实上,您可能应该继续设置和环境类似于在生产中部署的方式.您不会通过简单地运行 ruby app.rb 进行部署.你想把 apache 或 nginx 放在你的 Mongrel 前面.Mongrel 将提供您的静态文件,但这实际上仅适用于开发模式.在部署中,Web 服务器将为此做得更好.简而言之,您的部署环境将比您的独立开发环境更快.

Set up different configurations in your Sinatra app for development and production because some of these suggestions, you won't always want to use. In fact, you should probably go ahead and setup and environment similar to how you would deploy in production. You would not deploy by simply running ruby app.rb. You'd want to put apache or nginx in front of your Mongrel. Mongrel will serve up your static files, but that's really only advisable for development mode. In deployment, a web server is going to do a lot better job for that. In short, your deployed environment will be faster than your standalone development environment.

在这一点上,我不会担心 Mongrel 与 Thin.如果 Thin 快两倍——它不是——那么你的 7 秒变成了 3.5.这样就够了吗?

At this point, I wouldn't worry about Mongrel vs. Thin. If Thin is twice as fast - it isn't - then your 7 seconds becomes 3.5. Will that be good enough?

一些尝试......

我知道我刚刚告诉你设置部署环境,但可能不是服务器端.您是否尝试过运行 YSlowPageSpeed 在您的网页上?I/O 将比服务器占用更多的 7 秒(免责声明:我假设您的网络设置没有问题).YSlow - 实际上是 Firebug - 会告诉您页面的每个部分需要多长时间才能到达浏览器.

I know I just told you to set up a deployment environment, but maybe it's not the server side. Have you tried running YSlow or PageSpeed on your pages? I/O is going to take up more of those 7 seconds (Disclaimer: I'm assuming that there's nothing wrong with your network set up) than the server. YSlow - Firebug actually - will tell you how long each part of your page takes to get to the browser.

YSlow 告诉我要做的一件事是在我的静态资产上放置一个远期 Expires 标头,我知道这一点,但我将优化留到最后.那时我意识到至少有 3 个不同的地方我可以指定该标题.我说服自己在 nginx 中做这件事是正确的地方.

One of the things that YSlow told me to do was to put a far forward Expires header on my static assets, which I knew but I was leaving optimization until the end. That's when I realized that there were at least 3 different places that I could specify that header. I'm convincing myself that doing it in nginx is the right place to put it.

如果您对这些结果感到满意,那么您可以查看服务器.在我的头顶,所以不详尽

If you're happy with those results, then you can look at the server. Off the top of my head, so not exhaustive

  1. 开启 gzip 响应.
  2. 合并您的样式表,以便每个页面请求只有一个.如果您不手动执行此操作,可能会有一些机架中间件.
  3. 缓存.我正在尝试 Rack::Cache.
  4. 使用精灵来减少您使用的图片下载次数.
  5. 缩小您的 Javascript.同样,也许通过机架中间件.

机架中间件很整洁,但它使用 CPU.因此,手动缩小您的 Javascript 为您的工作流程添加了一个新步骤,但在服务器上,它比中间件更快.这是一种权衡.

Rack Middleware is neat, but it uses CPU. So, manually minifying your Javascript adds a new step to your workflow, but on the server, it's faster than Middleware. It's a tradeoff.

对不起,如果这有点乱.

Sorry if this was rambly.