Visualize with Ruby-Prof Printers

Ruby-prof can generate several types of human-readable reports. I find three of them to be the most useful: flat, call graph, and call stack reports. (You can see the full list in the ruby-prof documentation.)[10] Each serves a different purpose. But we’re learning by example here, and to show them to you, I’ll need a more sophisticated example to profile.

chp4/app.rb
 
require ​'date'
 
require ​'rubygems'
 
require ​'ruby-prof'
 
 
# This generates CSV like
 
# 1, John McTest, 1980-07-01
 
# 2, Peter McGregor, 1985-12-23
 
# 3, Sylvia McIntosh, 1989-06-13
 
def​ generate_test_data
 
50000.times.map ​do​ |i|
 
name = [​"John"​, ​"Peter"​, ​"Sylvia"​][rand(3)] + ​" "​ +
 
[​"McTest"​, ​"McGregor"​, ​"McIntosh" ...

Get Ruby Performance Optimization now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.