-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to run the compilation at startup rather than first request #1902
Comments
If you hold an API instance you can call Grape::API::Instance#compile. Is the issue finding the root of your API? What have you tried? I tried adding this to grape-on-rack api.rb and it worked. puts Acme::API.instance # => nil
Acme::API.compile
puts Acme::API.instance # => #<#<Class:0x00007f9bc56ae448>:0x00007f9bc4954658> I'd like a PR to this effect in the documentation once you've figured this out? Thanks. |
I've generated a very small test, but it seems just compiling doesn't actually reduce the time of the first call. require 'benchmark'
puts Benchmark.measure {
puts `curl localhost:9292/ping`
} And the results with or without compiling on rackup are 0.015 for the first call, and ~0.0075 for all subsequent calls this is my config.ru require 'rubygems'
require 'bundler/setup'
Bundler.require(:default)
require './small_api.rb'
SmallAPI.compile
run SmallAPI I've also attempted require 'rubygems'
require 'bundler/setup'
Bundler.require(:default)
require './small_api.rb'
warmup do |app|
app.compile
end
run SmallAPI |
Note that warming up using Rack mock to actually call the API does the trick: require 'rubygems'
require 'bundler/setup'
Bundler.require(:default)
require './small_api.rb'
warmup do |app|
client = Rack::MockRequest.new(app)
client.get('/ping')
end
run SmallAPI That's a workable work around but I'm not too thrilled about it |
I'm looking forward to someone attaching a profiler to this! :) |
After attaching a profiler I've figured the difference is that, even when pre-compiling, we seem to be requiring many files and / or libraries (possibly because of the autoloader?) on the first API call The profiler was attached to the require 'ruby-prof'
# profile the code
RubyProf.start
response = instance.call(env)
result = RubyProf.stop
printer = RubyProf::FlatPrinter.new(result)
printer.print(STDOUT)
response Here are both calls
2nd call
|
OK, having modified the Should we create a PR to add a |
Absolutely. I would think a bit harder about naming, maybe |
Opened the PR: #1904 , taking suggestions as to how best to test this |
I'd be curious to see whether there's some more work to be done. I'm sure there's still stuff that doesn't happen until runtime, but hopefully that is quite minimal now |
Given the lack of response I feel we should probably close this issue, and re open if a new problem arises |
I'm having an issue where the first request (or a number of first requests - one per worker) takes much longer than the rest. That extra time is spent running
Grape::API.compile
.I'd like to find a way to run that process during application startup instead (I'm running grape directly from rack). Doing it before I fork workers would also help a lot.
I've done some obvious attempts without success. Is there some snippet showing how to achieve manual initialisation?
The text was updated successfully, but these errors were encountered: