postrank-labs/goliath

require grape slow down app

yokas opened this issue · 5 comments

yokas commented

I've written a 'Hello World' app using Goliath, and I decided to mount Grape on top of it:

#!/usr/bin/env ruby

require 'rubygems'
require 'bundler/setup'
Bundler.setup :default
require 'goliath'
require 'grape'   # <-- Comment out this line will hugely increase performance, but why?

class Server < Goliath::API
  def response(env)
    [200, {}, 'Hello, world!']
  end
end

I benchmarked it:

ab -n 1000 -c 100 http://localhost:9000/

It shows that about 250 requests can be handled per second. But when I comment out the line require 'grape', this value suddenly increased to about 700. Can anybody answer why the simple require can bring such a huge difference?

P.S. I use MRI 2.2.2

This sounds like a grape-specific issue. Closing, but feel free to reopen if Goliath is at fault.

p.s. also, make sure you run with -e prod to remove code reloading!

It's not a grape-specific issue. It really seems like code reloading issue.

I generated 1000 empty ruby files:

1000.times do |i|
  File.write("./file#{i}.rb", '')
end

And required them all in your application (instead of require 'grape'):

1000.times do |i|
  require_relative "file#{i}"
end

Then I got a described slow down. However, when running with -e prod the difference is really negligible. You can try it yourself.

dj2 commented

When you say the difference is negligible, do you mean the difference of running with and without -e prod?

@dj2

Negligible with and without many loaded ruby files when running with -e prod since code reloading feature is not activated.

That said, almost any library with many dependencies will cause slow down when running in development mode. You just measured a wrong thing.