Skip to content

Commit 053d155

Browse files
committed
1 parent 1449c87 commit 053d155

File tree

14 files changed

+3148
-52
lines changed

14 files changed

+3148
-52
lines changed

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ <h1 id="ruby-prof">ruby-prof<a class="headerlink" href="#ruby-prof" title="Perma
628628
<ul>
629629
<li><a href="getting-started/">Getting Started</a> - Command line, convenience API, core API</li>
630630
<li><a href="advanced-usage/">Advanced Usage</a> - Measurement modes, allocation tracking, thread filtering, method exclusion</li>
631-
<li><a href="reports/">Reports</a> - Flat, graph, HTML, call stack, call tree, graphviz</li>
631+
<li><a href="reports/">Reports</a> - Flat, graph, HTML, call stack, call tree, flame graph, graphviz</li>
632632
<li><a href="profiling-rails/">Profiling Rails</a> - Using ruby-prof with Rails applications</li>
633633
<li><a href="architecture/">Architecture</a> - How ruby-prof works internally</li>
634634
</ul>

public/examples/example.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# A small synthetic workload for demonstrating ruby-prof reports.
2+
# word_freq.rb
3+
4+
def normalize(text)
5+
text.downcase.gsub(/[^a-z\s]/, "")
6+
end
7+
8+
def tokenize(text)
9+
text.split(/\s+/)
10+
end
11+
12+
def count_words(words)
13+
counts = Hash.new(0)
14+
words.each { |w| counts[w] += 1 }
15+
counts
16+
end
17+
18+
def top_words(counts, n = 10)
19+
counts.sort_by { |_, v| -v }.take(n)
20+
end
21+
22+
def run_example
23+
text = <<~EOS * 200
24+
Ruby is a dynamic, open source programming language with a focus on
25+
simplicity and productivity. It has an elegant syntax that is natural
26+
to read and easy to write.
27+
EOS
28+
29+
normalized = normalize(text)
30+
tokens = tokenize(normalized)
31+
counts = count_words(tokens)
32+
top = top_words(counts)
33+
end
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/usr/bin/env ruby
2+
# encoding: UTF-8
3+
4+
# Generates example reports for all ruby-prof printers.
5+
# Usage: ruby docs/public/examples/generate_reports.rb
6+
7+
# To make testing/debugging easier test within this source tree versus an installed gem
8+
require 'bundler/setup'
9+
10+
# Add ext directory to load path to make it easier to test locally built extensions
11+
ext_path = File.expand_path(File.join(__dir__, '..', '..', '..', 'ext', 'ruby_prof'))
12+
$LOAD_PATH.unshift(ext_path)
13+
14+
require 'fileutils'
15+
require 'stringio'
16+
require 'uri'
17+
require 'ruby-prof'
18+
require_relative 'example'
19+
20+
output_dir = File.join(File.dirname(__FILE__), "reports")
21+
FileUtils.mkdir_p(output_dir)
22+
23+
result = RubyProf::Profile.profile(measure_mode: RubyProf::WALL_TIME) do
24+
run_example
25+
end
26+
27+
# Flame Graph
28+
File.open(File.join(output_dir, "flame_graph.html"), "wb") do |f|
29+
RubyProf::FlameGraphPrinter.new(result).print(f)
30+
end
31+
32+
# Call Stack
33+
File.open(File.join(output_dir, "call_stack.html"), "wb") do |f|
34+
RubyProf::CallStackPrinter.new(result).print(f)
35+
end
36+
37+
# Graph HTML
38+
File.open(File.join(output_dir, "graph.html"), "wb") do |f|
39+
RubyProf::GraphHtmlPrinter.new(result).print(f)
40+
end
41+
42+
# Graph (text)
43+
File.open(File.join(output_dir, "graph.txt"), "wb") do |f|
44+
RubyProf::GraphPrinter.new(result).print(f)
45+
end
46+
47+
# Flat
48+
File.open(File.join(output_dir, "flat.txt"), "wb") do |f|
49+
RubyProf::FlatPrinter.new(result).print(f)
50+
end
51+
52+
# Call Info
53+
File.open(File.join(output_dir, "call_info.txt"), "wb") do |f|
54+
RubyProf::CallInfoPrinter.new(result).print(f)
55+
end
56+
57+
# Dot
58+
dot_io = StringIO.new
59+
RubyProf::DotPrinter.new(result).print(dot_io)
60+
dot_content = dot_io.string
61+
File.open(File.join(output_dir, "graph.dot"), "wb") do |f|
62+
f << dot_content
63+
end
64+
65+
# Graphviz Online viewer with dot content embedded in URL
66+
viewer_url = "https://dreampuf.github.io/GraphvizOnline/?engine=dot#" + URI.encode_uri_component(dot_content)
67+
File.open(File.join(output_dir, "graphviz_viewer.html"), "wb") do |f|
68+
f << %(<html><head><meta http-equiv="refresh" content="0;url=#{viewer_url}"></head></html>)
69+
end
70+
71+
# Call Tree (calltree format)
72+
RubyProf::CallTreePrinter.new(result).print(path: output_dir, profile: "profile")
73+
# Rename PID-based filename to a stable name
74+
Dir.glob(File.join(output_dir, "callgrind.out.*")).each do |f|
75+
FileUtils.mv(f, File.join(output_dir, "callgrind.out"))
76+
end
77+
78+
puts "Reports written to #{output_dir}/"
79+
Dir.glob(File.join(output_dir, "*")).sort.each do |f|
80+
puts " #{File.basename(f)}"
81+
end
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
----------------------------------------------------
2+
Thread ID: 464
3+
Fiber ID: 456
4+
Total Time: 0.008266999997431412
5+
Sort by:
6+
7+
[global]# (tt:0.01, st:0.00, wt:0.00, ct:0.01, call:1, )
8+
Object#run_example (tt:0.01, st:0.00, wt:0.00, ct:0.01, call:1, )
9+
String#* (tt:0.00, st:0.00, wt:0.00, ct:0.00, call:1, )
10+
Object#normalize (tt:0.00, st:0.00, wt:0.00, ct:0.00, call:1, )
11+
String#downcase (tt:0.00, st:0.00, wt:0.00, ct:0.00, call:1, )
12+
String#gsub (tt:0.00, st:0.00, wt:0.00, ct:0.00, call:1, )
13+
Object#tokenize (tt:0.00, st:0.00, wt:0.00, ct:0.00, call:1, )
14+
String#split (tt:0.00, st:0.00, wt:0.00, ct:0.00, call:1, )
15+
Object#count_words (tt:0.01, st:0.00, wt:0.00, ct:0.01, call:1, )
16+
Hash#initialize (tt:0.00, st:0.00, wt:0.00, ct:0.00, call:1, )
17+
Array#each (tt:0.01, st:0.00, wt:0.00, ct:0.00, call:1, )
18+
Hash#[] (tt:0.00, st:0.00, wt:0.00, ct:0.00, call:5800, )
19+
Integer#+ (tt:0.00, st:0.00, wt:0.00, ct:0.00, call:5800, )
20+
Hash#[]= (tt:0.00, st:0.00, wt:0.00, ct:0.00, call:5800, )
21+
Object#top_words (tt:0.00, st:0.00, wt:0.00, ct:0.00, call:1, )
22+
Enumerable#sort_by (tt:0.00, st:0.00, wt:0.00, ct:0.00, call:1, )
23+
Hash#each (tt:0.00, st:0.00, wt:0.00, ct:0.00, call:1, )
24+
Integer#-@ (tt:0.00, st:0.00, wt:0.00, ct:0.00, call:25, )
25+
Array#take (tt:0.00, st:0.00, wt:0.00, ct:0.00, call:1, )
26+
27+

0 commit comments

Comments
 (0)