over 9 years ago
Rails has benchmark helper to quickly test the time of execution of a given piece of code. Active Record, Action Controller and Action View libraries provides a benchmark() method to measure the performance of code.
Example: Model(Active Record):
While execution of the above piece of code something like this Creating user (100.3ms) can be seen in the log file.Here benchmark() is a class method of the model User.
Controller(Action Controller):
- def process_stories
- self.class.benchmark("Processing stories") do
- @stories = current_user.stories
- Story.cache_story_feeds(@stories, "myfeed-#{current_user.id}")
- end
- end
def process_stories self.class.benchmark("Processing stories") do @stories = current_user.stories Story.cache_story_feeds(@stories, "myfeed-#{current_user.id}") end end
Views(Action View):
In the log file output will be Show story feeds (191.5ms).
This benchmark() method accepts logger level(:debug, :info, :warn, :error) as optional parameter. Default is :info. Thus we can modify the above code as:
If we pass silence: true as the third argument all logging activity except the time information for the block will be silenced.
0 Comment(s)