-
Notifications
You must be signed in to change notification settings - Fork 2k
[rails] Return plain json from route #9573
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
Conversation
b01d03c to
1de1af2
Compare
Return JSON from the routes is a lot faster: +------------+------------------------------+------+ | name| branch_name| json| +------------+------------------------------+------+ |rails-iodine| master|105369| |rails-iodine| rails/json-from-route|144006| +------------+------------------------------+------+
1de1af2 to
cec5f91
Compare
|
IMHO, the problem with such changes is that the resulting code is not production-grade. Nobody uses |
Thanks for looking at this PR @rsamoilov ! I can see the problem taking things too far. For example, we could implement the whole application in Also, Rails comes with a lot of functionality out-of-the-box, that the tests don't require and other frameworks don't implement, like CSRF-protection.
BTW this change removes usage of
I work on applications that have lambda handlers in routes. For example: get 'googleabcdef12345.html' => lambda { |env| [200, { }, ['google-site-verification: googleabcdef12345.html'] ] }For the
Some servers, for example The Rails documentation also describes setting the headers. |
|
@rsamoilov Btw, I think it would be totally acceptable to do this for Rage as well. Rage.routes.draw do
root to: ->(env) { [200, {}, "It works!"] }
get "json", to: "benchmarks#json"
get "plaintext", ->(env) { [200, {
'Server' => 'Rails',
'Content-Type' => 'text/plain',
},
['Hello, World!']]]
}
get "db", to: "benchmarks#db"
get "queries", to: "benchmarks#queries"
get "fortunes", to: "benchmarks#fortunes"
get "updates", to: "benchmarks#updates"
end |
Fair enough.
I don't think it would make it much faster. With JSON and plaintext routes, most overhead comes from scheduling fibers and parsing parameters - and this code is called for lambda handlers, too. |
|
Locally I get:
|
Quite unexpected.
I think I would prefer regular controllers in the Rage test - you almost won me over, but I still think it's not a production-grade code. You don't have access to parameters inside lambda handlers, no request/response objects, and no authentication. The strength of Ruby (and Rails) is its high level of abstraction. Rage proves that this high level of abstraction doesn't have to be slow. But with such low-level lambda-handler code, you might as well use Go - and it will also be faster. |
The things is that the
But these calls still add to the composite score. And if other frameworks have special paths for
For calls where we do need to access parameters we still use regular controllers. The plaintext test requirements state that the test "... is an exercise of the request-routing fundamentals only..".
Some of the Ruby frameworks are faster than some of the Go frameworks. |
|
Well, I think you have a point. |
All benchmarks will, by definition, test routing a request and response. The trouble is that you are fully bypassing Rails' request/response abstractions and benchmarking Rack instead. I think Metal was less problematic because it was still a Rails feature. You are right that using Rack apps in the router is a Rails feature but then it begs the question of where to draw the line: for example, should frameworks that offer the ability to hijack the socket, use such feature to directly read/write to a socket?
Both links are still using the framework request/response abstractions to write their responses instead of sending canned responses as this PR does. I'd say they are much closer to the Metal example that was here before. |

Return JSON from the routes is a lot faster: