discrepancy in latency between rails request logs and chrome network tab - ruby-on-rails

A Rails API call shows a duration of 894m in the request logs locally, where the render looks like this: render status: :ok, json: { ... }
:status => 200,
:duration => 894.95,
:view => 7.87,
I'm rendering an array of objects (not ActiveRecord), but I didn't override as_json with crazy implementations or anything.
This is a SPA, an Angular frontend. When I open the network tab in Chrome console for the same API call, it shows that it's been waiting for 3s before the first byte:
I'm trying to speed up a page in my app and this API call is a big one. How can I figure what what is causing this discrepancy?

Related

Rails takes extremely slow time to process ajax json

It takes my rails app an insane amount time to server ajax request. This is my test code
def test
respond_to do |format|
format.json { render json: '{"text" : "simpleJson"}' }
end
end
base on server log, it take my apps only 1ms to execute request :
but in browser, it is always recieved response in about 20k+ ms
When i change the json return to nothing ( format.json { render json: {} } ) the request only takes ~18ms. So I guess rails has some trouble when sending json back to client (because server log say the method test execute very fast). But I'm not sure about that because I'm very new to ruby and rails. My localhost is run by puma 3.10.0.
Any help would be appreciated.
Change from puma server to webrick solve my issue, but after doing some google the root cause of my problem maybe because I run rails on Window Subsystem Linux. At the moment, puma is slow in WSL.

Different response time for post on RoR

I have a web service to which data is posted. What I do is parse the json and store the data.
And my response is
render json: { success: true, message: "ok" }, status: 200
Active record takes about 10ms to complete the inserts. There are no views so rails returns something like 0.4 ms to render them. What I find strange are the response time, that are always different (ok, 20 ms up or down is ok, but those are the response times): 130ms, 62ms, 149ms, 71ms, 77ms, 150ms, 48ms, 72ms
My app is on Heroku (I only have new relic enabled, no other request). Does anyone know why the difference or a good way to test it? Maybe because of new Relic?

Rails API: How do you standardize your JSON responses?

Right now, upon a successful call to my Rails API Backend, I might do one of the following...
render json: {user_id: #user.id, token: token}
or
render json: {status: :success}
And upon an unsuccessful result, I may do something like
render json: {status: :failure, error: "The email or password entered is not correct."}
My question is... what's the best way to standardize this.
If my iOS app goes searching for a json dictionary value of dict[#"error"] it's not always going to find it. Should every single render message I have regardless of success... have a 'status' key, or an 'error' key or both.
Or is it better for the front end to deal with this, and if dict[#"error"]/dict[#"status"] happen to not exist... then it knows the back end did not have an error occur otherwise it would have said so?
The way you are rendering the responses is fine. On your client side, it's should be all about handling properly HTTP status codes.
render status: :ok -> should return a HTTP status code 200, which means a successful request. Then your client (iphone app) does know that with a successfull request you get the user id and token.
render status: :bad_request -> should return a HTTP status code 400, which means unsuccessfull request, then your client would know that an error occured, and based on your standards he can look up the error field of the json response
I had the similar question in terms of how do we standardize the JSON response.
By default Rails generates this sort of JSON from a post model,
posts: [
{
id: 22198,
title: "Using GitHub Pages To Host Your Website",
date: "2013-08-16 09:30:20",
},
{
id: 22196,
title: "Running Tests in Ruby on Rails – Treehouse Quick Tip",
date: "2013-08-15 14:30:48",
}
]
But is there any standard to power the Rails API for an iPhone APP?
What are the standards or typical success/failure response format an iOS developer would expect to see?

Sporadic ActionController::UnknownFormat on ajax calls of a remote form

I'm currently using rails 4 remote forms with json responses:
respond_to do |format|
if read_only || #object.update_attributes(object_params)
format.json { render json: {}, status: :ok }
else
format.json { render json: #object.errors.messages, status: :unprocessable_entity }
end
end
Most of the time it works well, but sometimes, on chrome, it gives a strange ActionController::UnknownFormat error.
This is very hard to trace since it is sporadic...
Anyone experienced this issue or has a solution?
EDIT:
As requested, from logs
Started PATCH "/objects/727"
I, INFO -- : Processing by ObjectsController#update as JS
Started PATCH "/objects/727"
I, INFO -- : Processing by ObjectsController#update as HTML
it seams you are right, the failing request is not always JS, but I cannot think of a reason it doesn't always behave the same way and also why does it happen only on chrome?
I had this issue a few months back, where my error aggregator was showing me similar errors. The errors were only for users accessing the site using IE7 and earlier browsers. The existing jquery version - 2.2.0 that I was using did not have support for older browsers, so I had to downgrade jquery version to 1.9.1, which solved the problem for me.

how do you write json response destroy methods correctly in rails?

Here's the code for sending a json response associated with a working and non working status. Does anyone have recommendations for other resources? This isn't working. Thank you.
if #content.destroy
format.json { redirect_to #collection, status: :destroyed, notice 'Content was removed from collection.' }, :status => 200
else
format.json { render json #content.errors, status: :unprocessable_entity }, :status => 400
end
TL;DR: if you are using some JS framework, then look up how that framework wants success and failure response to look like, if you are rolling your own - then do whatever makes more sense for you.
Long version:
There are couple ways you can return response from rails to your web app. Rails by default returns status code 2xx for success and 4xx for failed requests. Some web frameworks such as Extjs, like to receive response with 200 code always and look at success key in the response to see if the request was successfult. If you are writing your own JS that utilizes XHR, then you can do whatever you want: its up to you how you architect your API. I find it useful to return json response from server with 200 code and set success key to true or false so in my callback JS i can write something like:
if(request.data.success){
successfulPost(response);
} else {
failed(response);
}
one of the reasons I prefer that approach is that, if there is an exception, then I can actually distinguish from record not being saved due to validation errors as opposed to bug in code that caused exception. But that is a personal preference and some may argue that error is error and it does not matter what caused it.

Resources