Skip to content

Commit c870ac6

Browse files
Add rspec for kernel extension. Update README to reflect label usage
1 parent 9914a4c commit c870ac6

File tree

3 files changed

+64
-4
lines changed

3 files changed

+64
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Singed.output_directory = "tmp/slowness-exploration"
3232
If you are calling it in a loop, or with different variations, you can include a label on the filename:
3333

3434
```ruby
35-
flamegraph(label: "rspec") {
35+
flamegraph("rspec") {
3636
# your code here
3737
}
3838
```

lib/singed/kernel_ext.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
module Kernel
2-
def flamegraph(label = nil, open: true, ignore_gc: false, interval: 1000, &block)
2+
def flamegraph(label = nil, open: true, ignore_gc: false, interval: 1000, io: $stdout, &block)
33
fg = Singed::Flamegraph.new(label: label, ignore_gc: ignore_gc, interval: interval)
44
result = fg.record(&block)
55
fg.save
66

77
if open
88
# use npx, so we don't have to add it as a dependency
9-
puts "🔥📈 #{'Captured flamegraph, opening with'.colorize(:bold).colorize(:red)}: #{fg.open_command}"
9+
io.puts "🔥📈 #{'Captured flamegraph, opening with'.colorize(:bold).colorize(:red)}: #{fg.open_command}"
1010
fg.open
1111
else
12-
puts "🔥📈 #{'Captured flamegraph to file'.colorize(:bold).colorize(:red)}: #{fg.filename}"
12+
io.puts "🔥📈 #{'Captured flamegraph to file'.colorize(:bold).colorize(:red)}: #{fg.filename}"
1313
end
1414

1515
result

spec/singed/kernel_ext_spec.rb

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
describe Kernel, "extension" do
2+
let(:flamegraph) {
3+
instance_double(Singed::Flamegraph)
4+
}
5+
6+
before do
7+
allow(Singed::Flamegraph).to receive(:new).and_return(flamegraph)
8+
allow(flamegraph).to receive(:record)
9+
allow(flamegraph).to receive(:save)
10+
allow(flamegraph).to receive(:open)
11+
allow(flamegraph).to receive(:open_command)
12+
allow(flamegraph).to receive(:filename)
13+
end
14+
15+
let(:io) { StringIO.new }
16+
17+
it "works without any arguments" do
18+
# * except what's needed to test
19+
# note: use Object.new to get the actual flamegraph kernel extension, instead of the rspec-specific flamegraph
20+
Object.new.flamegraph io: io do
21+
end
22+
23+
expect(Singed::Flamegraph).to have_received(:new).with(label: nil, ignore_gc: false, interval: 1000)
24+
end
25+
26+
it "works with explicit arguments" do
27+
# note: use Object.new to get the actual flamegraph kernel extension, instead of the rspec-specific flamegraph
28+
Object.new.flamegraph 'yellowjackets', ignore_gc: true, interval: 2000, io: io do
29+
end
30+
31+
expect(Singed::Flamegraph).to have_received(:new).with(label: 'yellowjackets', ignore_gc: true, interval: 2000)
32+
end
33+
34+
context "default" do
35+
it "opens" do
36+
Object.new.flamegraph open: true, io: io do
37+
end
38+
39+
expect(flamegraph).to have_received(:open)
40+
end
41+
end
42+
43+
context "open: true" do
44+
it "opens" do
45+
Object.new.flamegraph open: true, io: io do
46+
end
47+
48+
expect(flamegraph).to have_received(:open)
49+
end
50+
end
51+
52+
context "open: false" do
53+
it "doesn't open" do
54+
Object.new.flamegraph open: false, io: io do
55+
end
56+
57+
expect(flamegraph).to_not have_received(:open)
58+
end
59+
end
60+
end

0 commit comments

Comments
 (0)