Is an instance variable recalculated every time it is used in the same controller?
Both methods will have almost the same performance, because Thing.all.where(boolean: true)
returns an instance of ActiveRecord::Relation
. An ActiveRecord::Relation
does not perform an database query immediately, but on the first method call that needs the data (reverse
in this examples).
Method 2 creates new (but similar) relation objects, whereas Method 1 reuses the same relation object. There performance would be almost the same, because creating that relation is very fast compared to running multiple database calls and there is the same number of database call on both examples.
But there is a way to speed this up: Both methods in your example will only perform one database call, because Rails caches database calls in the database connector. But Rails will re-generate the result from that cached database responds on each query and will return an new array with new instances every time.
It might be faster (depending on the size of the database response) to load all objects into an array and to reuse that array:
def show
# triggers the database call and returns an array (not a Relation)
@things = Thing.where(boolean: true).to_a
# re-use the result from the @things array, and not a Relation object
@things_decreasing = @things.reverse
@things_random = @things.shuffle
end
What is a reliable way to calculate actual (web) page loadtime
http://www.webpagetest.org/ is an excellent resource for measuring load time
Also google chorme dev tools has a Timeline panel where you can record events,
Here is a 2.5minute video showing you how Timeline in google chrome works http://www.youtube.com/watch?v=RhaWYQ44WEc
Calculating Page Load Time In JavaScript
Don't ever use the setInterval
or setTimeout
functions for time measuring! They are unreliable, and it is very likely that the JS execution scheduling during a documents parsing and displaying is delayed.
Instead, use the Date
object to create a timestamp when you page began loading, and calculate the difference to the time when the page has been fully loaded:
<doctype html>
<html>
<head>
<script type="text/javascript">
var timerStart = Date.now();
</script>
<!-- do all the stuff you need to do -->
</head>
<body>
<!-- put everything you need in here -->
<script type="text/javascript">
$(document).ready(function() {
console.log("Time until DOMready: ", Date.now()-timerStart);
});
$(window).load(function() {
console.log("Time until everything loaded: ", Date.now()-timerStart);
});
</script>
</body>
</html>
Ruby-Capybara Page load Wait [ To set up a method in Ruby-Capybara-Selenium-Cucumber embeded framework for a web Automation]
This works for me.
calculating with Ruby and Jscript - document.readyState, Create a generic method and call it.
SIGNIN_BTN = "//div/button[contains(.,'Sign In')]"
NEXT_BTN = "//div/button[contains(.,'NEXTBUTTON')]"
visit "https://gmail.com/"
timewait = pagetime_time
puts "LAUNCH GMAIL | PAGELOAD TIME :#{timewait}"
find(:xpath, SIGNIN_BTN).click
timewait = pagetime_time
puts "SIGN IN GMAIL | PAGELOAD TIME :#{timewait}"
find(:xpath, NEXT_BTN ).click
timewait = pagetime_time
puts "NEXT GMAIL | PAGELOAD TIME :#{timewait}"
def pagetime_time
time1 = Time.now
timeloop = 0
while timeloop < 500
if (Capybara.current_session.driver.execute_script('var browserState = document.readyState; return browserState;') == 'complete')
time2 = Time.now
break
end
timeloop + 1
end
timeWait = (time2 - time1) * 1000
return timeWait
end
Calculating time it takes for a webpage to fully load in a webview
private long startTime;
private long finishedTime;
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
startTime = System.nanoTime();
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
finishedTime = System.nanoTime();
long timeToFullLoad = finishedTime - startTime; // It will be in NANO, just convert it to seconds and enjoy
}
Website response time: Difference between `Load` and `Finish`
The Finish
time in Chrome Devtools includes the asynchronously loading (non blocking) objects/elements on the page which may continue downloading way after the onLoad
event for the page has fired.
The response time for a website generally means the Load
time, because that is user perceivable, and at this point user can see that the browser has finished loading and page is ready on their screen.
The Finish
time, although technically also a response time, doesn't have as much end-user implication.
In some cases, it seems that the Finish
timer never stops but continues increasing, so it may not be the best assessment of web page response time.
Is there a way to prevent the pretty-print gem from triggering every time a model is instantiate?
Coincidentally I do not use pretty-print for those reasons. You should check out snoop_dogg instead (Full disclosure, I created snoop_dogg).
The gem sorts the attributes alphabetically, and puts them to the console in a similar fashion as pretty-print, but it does not do so automatically. You instead call 'snoop' on the model instance when you want to inspect it in a more human readable way.
Also, 'snoop' takes arguments in the form of model relationships. For example, 'User.first.snoop(:address, :comments)' would print the @user instance as well as that user's associated address and any comments associated with that user.
I think this is what you are looking for. Hope that help! Feel free to contribute too!
Related Topics
Instance_Eval Block Not Supplied
Message Queues in Ruby on Rails
Automatic Code Quality Tool for Ruby
Generate Unique Random String with Letters and Numbers in Lower Case
Rails: Testing Named Scopes with Rspec
How to Write a Web Scraper in Ruby
Is It Idiomatic Ruby to Add an Assert( ) Method to Ruby's Kernel Class
Bcrypt Error: Devise Ruby 2.0 and Rails 4.0
Vagrant/Virtualbox Vm Provisioning: Rbenv Installs Successfully But Subsequent Uses in Script Fail
How Get All Routes in My Rails Application
How to List Included Modules in a Ruby Class
How Many Rails Apps on 1 Heroku Dyno
Find File Path of a Given Class
How to Remove All Non - Ascii Characters from a String in Ruby