少し早起きしてRails send_fileとlighttpd sendfile(内部的にはsendfile(2)のはず)を比べてみました。

class ImagesController < ApplicationController
  def show
     send_file("/home/komamitsu/images/#{params[:id]}.png")
  end
end
class ImagesController < ApplicationController
  def show
    filename = params[:id] + '.png'
    response.headers["X-LIGHTTPD-send-file"] = "/home/komamitsu/images/#{filename}"
    render :nothing => true
  end
end
  • 一応クライアント側のコード
require 'open-uri'

5000.times do
  id = rand(100000)
  url = "http://192.168.2.32:3010/images/show/%08d" % id
  open(url) do
    nil
  end
end

ざっくりとした結果としては, 速度とCPU使用率はほぼ一緒。ちょっと期待はずれだけど、File I/Oの頻度に影響するものではない(今回は全てページキャッシュに乗っていたみたいだし)からこんなものなのかも。