Skip to content

Commit 36d22db

Browse files
committed
Add flamegraphs and change graph printer
For testing and easier visualization use flamegraph and the html printer for ruby-prof. I couldn't get the ruby-prof-flamegraph gem to work with these types of tests :(
1 parent b53d034 commit 36d22db

File tree

4 files changed

+62
-24
lines changed

4 files changed

+62
-24
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ end
4646

4747
group :test do
4848
gem 'ruby-prof'
49+
gem 'flamegraph'
4950
gem 'mocha'
5051
gem 'capybara'
5152
gem 'benchmark-ips'

Gemfile.lock

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@ GEM
6060
specs:
6161
arel (6.0.0)
6262
benchmark-ips (2.1.0)
63-
binding_of_caller (0.7.2)
64-
debug_inspector (>= 0.0.1)
6563
builder (3.2.2)
6664
byebug (3.5.1)
6765
columnize (~> 0.8)
@@ -81,10 +79,14 @@ GEM
8179
execjs
8280
coffee-script-source (1.8.0)
8381
columnize (0.9.0)
84-
debug_inspector (0.0.2)
8582
debugger-linecache (1.2.0)
8683
erubis (2.7.0)
8784
execjs (2.2.2)
85+
fast_stack (0.1.0)
86+
rake
87+
rake-compiler
88+
flamegraph (0.0.5)
89+
fast_stack
8890
globalid (0.3.0)
8991
activesupport (>= 4.1.0)
9092
hike (1.2.3)
@@ -122,6 +124,8 @@ GEM
122124
rails-html-sanitizer (1.0.1)
123125
loofah (~> 2.0)
124126
rake (10.4.2)
127+
rake-compiler (0.9.2)
128+
rake
125129
rdoc (4.2.0)
126130
json (~> 1.4)
127131
ruby-prof (0.15.2)
@@ -173,6 +177,7 @@ DEPENDENCIES
173177
byebug
174178
capybara
175179
coffee-rails (~> 4.1.0)
180+
flamegraph
176181
jbuilder (~> 2.0)
177182
jquery-rails
178183
mocha
Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,41 @@
11
require 'test_helper'
22

33
class DocumentsControllerTest < ActionController::TestCase
4-
test "creating a new document" do
4+
test "index" do
55
result = RubyProf.profile do
6-
post :create, document: { title: "New things", content: "Doing them" }
6+
get :index
7+
assert_equal 200, response.status
78
end
8-
printer = RubyProf::GraphPrinter.new(result)
9+
printer = RubyProf::GraphHtmlPrinter.new(result)
910
printer.print(STDOUT, {})
10-
#post :create, document: { title: "New things", content: "Doing them" }
11+
end
1112

12-
document = Document.last
13-
assert_equal 'New things', document.title
14-
assert_equal 'Doing them', document.content
13+
test "index flame" do
14+
Flamegraph.generate("controller_index.html") do
15+
get :index
16+
assert_equal 200, response.status
17+
end
1518
end
1619

17-
test "index" do
20+
test "create" do
1821
result = RubyProf.profile do
19-
get :index
22+
post :create, document: { title: "New things", content: "Doing them" }
23+
24+
document = Document.last
25+
assert_equal 'New things', document.title
26+
assert_equal 'Doing them', document.content
2027
end
21-
printer = RubyProf::GraphPrinter.new(result)
28+
printer = RubyProf::GraphHtmlPrinter.new(result)
2229
printer.print(STDOUT, {})
30+
end
2331

24-
assert_equal 200, response.status
32+
test "create flame" do
33+
Flamegraph.generate("controller_create.html") do
34+
post :create, document: { title: "New things", content: "Doing them" }
35+
36+
document = Document.last
37+
assert_equal 'New things', document.title
38+
assert_equal 'Doing them', document.content
39+
end
2540
end
2641
end
Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,42 @@
11
require 'test_helper'
2+
require 'flamegraph'
23

34
class DocumentsIntegrationTest < ActionDispatch::IntegrationTest
4-
test "creating a new document" do
5+
test "index" do
56
result = RubyProf.profile do
6-
post documents_url, document: { title: "New things", content: "Doing them" }
7+
get '/documents'
8+
assert_equal 200, response.status
79
end
8-
printer = RubyProf::GraphPrinter.new(result)
10+
printer = RubyProf::GraphHtmlPrinter.new(result)
911
printer.print(STDOUT, {})
12+
end
1013

11-
document = Document.last
12-
assert_equal 'New things', document.title
13-
assert_equal 'Doing them', document.content
14+
test "index flame" do
15+
Flamegraph.generate("integration_index.html") do
16+
get '/documents'
17+
assert_equal 200, response.status
18+
end
1419
end
1520

16-
test "index" do
21+
test "create" do
1722
result = RubyProf.profile do
18-
get documents_url
23+
post '/documents', document: { title: "New things", content: "Doing them" }
24+
25+
document = Document.last
26+
assert_equal 'New things', document.title
27+
assert_equal 'Doing them', document.content
1928
end
20-
printer = RubyProf::GraphPrinter.new(result)
29+
printer = RubyProf::GraphHtmlPrinter.new(result)
2130
printer.print(STDOUT, {})
31+
end
2232

23-
assert_equal 200, response.status
33+
test "create flame" do
34+
Flamegraph.generate("integration_create.html") do
35+
post '/documents', document: { title: "New things", content: "Doing them" }
36+
37+
document = Document.last
38+
assert_equal 'New things', document.title
39+
assert_equal 'Doing them', document.content
40+
end
2441
end
2542
end

0 commit comments

Comments
 (0)