Skip to content

Commit db04e03

Browse files
Merge branch 'main' of github.com:Gusto/singed
2 parents 8a626de + deeba71 commit db04e03

File tree

7 files changed

+211
-7
lines changed

7 files changed

+211
-7
lines changed

Gemfile.lock

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,38 @@
11
PATH
22
remote: .
33
specs:
4-
singed (0.0.1)
5-
colorize (~> 0.8.1)
4+
singed (0.1.1)
5+
colorize
66
stackprof
77

88
GEM
99
remote: https://rubygems.org/
1010
specs:
1111
colorize (0.8.1)
12+
diff-lcs (1.5.0)
1213
rake (13.0.6)
14+
rspec (3.12.0)
15+
rspec-core (~> 3.12.0)
16+
rspec-expectations (~> 3.12.0)
17+
rspec-mocks (~> 3.12.0)
18+
rspec-core (3.12.1)
19+
rspec-support (~> 3.12.0)
20+
rspec-expectations (3.12.2)
21+
diff-lcs (>= 1.2.0, < 2.0)
22+
rspec-support (~> 3.12.0)
23+
rspec-mocks (3.12.4)
24+
diff-lcs (>= 1.2.0, < 2.0)
25+
rspec-support (~> 3.12.0)
26+
rspec-support (3.12.0)
1327
stackprof (0.2.17)
1428

1529
PLATFORMS
1630
ruby
1731

1832
DEPENDENCIES
1933
rake (~> 13.0)
34+
rspec
2035
singed!
2136

2237
BUNDLED WITH
23-
2.2.30
38+
2.4.4

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
```

bin/rspec

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
#
5+
# This file was generated by Bundler.
6+
#
7+
# The application 'rspec' is installed as part of a gem, and
8+
# this file is here to facilitate running it.
9+
#
10+
11+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)
12+
13+
bundle_binstub = File.expand_path("bundle", __dir__)
14+
15+
if File.file?(bundle_binstub)
16+
if File.read(bundle_binstub, 300).include?("This file was generated by Bundler")
17+
load(bundle_binstub)
18+
else
19+
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
20+
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
21+
end
22+
end
23+
24+
require "rubygems"
25+
require "bundler/setup"
26+
27+
load Gem.bin_path("rspec-core", "rspec")

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

singed.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Gem::Specification.new do |spec|
2222
spec.add_dependency 'stackprof'
2323

2424
spec.add_development_dependency 'rake', '~> 13.0'
25+
spec.add_development_dependency 'rspec'
2526

2627
# For more information and examples about making a new gem, checkout our
2728
# guide at: https://bundler.io/guides/creating_gem.html

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

spec/spec_helper.rb

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# This file was generated by the `rspec --init` command. Conventionally, all
2+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3+
# The generated `.rspec` file contains `--require spec_helper` which will cause
4+
# this file to always be loaded, without a need to explicitly require it in any
5+
# files.
6+
#
7+
# Given that it is always loaded, you are encouraged to keep this file as
8+
# light-weight as possible. Requiring heavyweight dependencies from this file
9+
# will add to the boot time of your test suite on EVERY test run, even for an
10+
# individual file that may not need all of that loaded. Instead, consider making
11+
# a separate helper file that requires the additional dependencies and performs
12+
# the additional setup, and require it from the spec files that actually need
13+
# it.
14+
#
15+
# See https://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
16+
17+
require 'singed'
18+
19+
RSpec.configure do |config|
20+
# rspec-expectations config goes here. You can use an alternate
21+
# assertion/expectation library such as wrong or the stdlib/minitest
22+
# assertions if you prefer.
23+
config.expect_with :rspec do |expectations|
24+
# This option will default to `true` in RSpec 4. It makes the `description`
25+
# and `failure_message` of custom matchers include text for helper methods
26+
# defined using `chain`, e.g.:
27+
# be_bigger_than(2).and_smaller_than(4).description
28+
# # => "be bigger than 2 and smaller than 4"
29+
# ...rather than:
30+
# # => "be bigger than 2"
31+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
32+
end
33+
34+
# rspec-mocks config goes here. You can use an alternate test double
35+
# library (such as bogus or mocha) by changing the `mock_with` option here.
36+
config.mock_with :rspec do |mocks|
37+
# Prevents you from mocking or stubbing a method that does not exist on
38+
# a real object. This is generally recommended, and will default to
39+
# `true` in RSpec 4.
40+
mocks.verify_partial_doubles = true
41+
end
42+
43+
# This option will default to `:apply_to_host_groups` in RSpec 4 (and will
44+
# have no way to turn it off -- the option exists only for backwards
45+
# compatibility in RSpec 3). It causes shared context metadata to be
46+
# inherited by the metadata hash of host groups and examples, rather than
47+
# triggering implicit auto-inclusion in groups with matching metadata.
48+
config.shared_context_metadata_behavior = :apply_to_host_groups
49+
50+
# The settings below are suggested to provide a good initial experience
51+
# with RSpec, but feel free to customize to your heart's content.
52+
=begin
53+
# This allows you to limit a spec run to individual examples or groups
54+
# you care about by tagging them with `:focus` metadata. When nothing
55+
# is tagged with `:focus`, all examples get run. RSpec also provides
56+
# aliases for `it`, `describe`, and `context` that include `:focus`
57+
# metadata: `fit`, `fdescribe` and `fcontext`, respectively.
58+
config.filter_run_when_matching :focus
59+
60+
# Allows RSpec to persist some state between runs in order to support
61+
# the `--only-failures` and `--next-failure` CLI options. We recommend
62+
# you configure your source control system to ignore this file.
63+
config.example_status_persistence_file_path = "spec/examples.txt"
64+
65+
# Limits the available syntax to the non-monkey patched syntax that is
66+
# recommended. For more details, see:
67+
# https://relishapp.com/rspec/rspec-core/docs/configuration/zero-monkey-patching-mode
68+
config.disable_monkey_patching!
69+
70+
# This setting enables warnings. It's recommended, but in some cases may
71+
# be too noisy due to issues in dependencies.
72+
config.warnings = true
73+
74+
# Many RSpec users commonly either run the entire suite or an individual
75+
# file, and it's useful to allow more verbose output when running an
76+
# individual spec file.
77+
if config.files_to_run.one?
78+
# Use the documentation formatter for detailed output,
79+
# unless a formatter has already been configured
80+
# (e.g. via a command-line flag).
81+
config.default_formatter = "doc"
82+
end
83+
84+
# Print the 10 slowest examples and example groups at the
85+
# end of the spec run, to help surface which specs are running
86+
# particularly slow.
87+
config.profile_examples = 10
88+
89+
# Run specs in random order to surface order dependencies. If you find an
90+
# order dependency and want to debug it, you can fix the order by providing
91+
# the seed, which is printed after each run.
92+
# --seed 1234
93+
config.order = :random
94+
95+
# Seed global randomization in this process using the `--seed` CLI option.
96+
# Setting this allows you to use `--seed` to deterministically reproduce
97+
# test failures related to randomization by passing the same `--seed` value
98+
# as the one that triggered the failure.
99+
Kernel.srand config.seed
100+
=end
101+
end

0 commit comments

Comments
 (0)