Monitor execution time and other metrics directly in rails console
, similar to what you see after each request.
[METRICS] Completed in 908.3ms | Allocations: 2894 | ActiveRecord: 0.9ms (queries: 13)
Just add this gem to the Gemfile and start rails c
. After this try to do something like User.first
.
If you want to measure few lines of code just wrap it with begin/end
:
[4] pry(main)> begin
[4] pry(main)* User.first.first_name.size
[4] pry(main)* a = User.count + 1
[4] pry(main)* b = User.second.last_name.size
[4] pry(main)* end
User Load (0.4ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT $1 [["LIMIT", 1]]
(3.6ms) SELECT COUNT(*) FROM "users"
User Load (0.3ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT $1 OFFSET $2 [["LIMIT", 1], ["OFFSET", 1]]
[METRICS] Completed in 6.8ms | Allocations: 839 | ActiveRecord: 4.3ms (queries: 3)
=> 5
Add this line to your application's Gemfile:
gem 'execution_time'
Sometime you can see that there are more SQL queries or allocated objects because Ruby is just loading objects in memory or verifying connection to the DB.
If you need to disable gem in the console you can do it by ExecutionTime.disable!
. And later enable with ExecutionTime.enable!
.
By default gem is enabled.
You are welcome to contribute.
The gem is available as open source under the terms of the MIT License.