Skip to content

Commit

Permalink
WIP validation
Browse files Browse the repository at this point in the history
  • Loading branch information
technicalpickles committed May 9, 2024
1 parent b97237d commit 82a410e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/singed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def filter_line(line)
line
end

def profiler_klass(profiler)
def profiler_class_for(profiler)
case profiler
when :stackprof, nil then Singed::Flamegraph::Stackprof
when :vernier then Singed::Flamegraph::Vernier
Expand All @@ -61,8 +61,9 @@ def profiler_klass(profiler)
end

def profile(label = "flamegraph", profiler: nil, open: true, announce_io: $stdout, **profiler_options, &)
profiler_klass = profiler_klass(profiler)
fg = profiler_klass.new(
profiler_class = profiler_class_for(profiler)

fg = profiler_class.new(
label: label,
announce_io: announce_io,
**profiler_options
Expand Down
12 changes: 12 additions & 0 deletions lib/singed/flamegraph.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ def self.generate_filename(label: nil, time: Time.now) # rubocop:disable Rails/T
file
end

def self.validate_options(klass, method_name, options)
method = klass.instance_method(:method_name)
options.each do |key, value|
if method.parameters.none? { |type, name| type == :key && name == key }
raise ArgumentError, "Unknown option #{key} for #{klass}.#{method_name}"
end
end
end

class Stackprof < Flamegraph
DEFAULT_OPTIONS = {
mode: :wall,
Expand Down Expand Up @@ -80,6 +89,8 @@ def open_command
# use npx, so we don't have to add it as a dependency
@open_command ||= "npx speedscope #{@filename}"
end


Check failure on line 93 in lib/singed/flamegraph.rb

View workflow job for this annotation

GitHub Actions / StandardRB

lib/singed/flamegraph.rb#L92-L93

Extra empty line detected at class body end.
end

Check failure on line 94 in lib/singed/flamegraph.rb

View workflow job for this annotation

GitHub Actions / StandardRB

lib/singed/flamegraph.rb#L93-L94

Extra blank line detected.

Check failure on line 94 in lib/singed/flamegraph.rb

View workflow job for this annotation

GitHub Actions / StandardRB

lib/singed/flamegraph.rb#L93-L94

Extra empty line detected at class body end.

class Vernier < Flamegraph
Expand All @@ -91,6 +102,7 @@ def initialize(label: nil, announce_io: $stdout, **vernier_options)

def record
vernier_options = {out: filename.to_s}.merge(@vernier_options)
validate_options(::Vernier, :run, vernier_options)
::Vernier.run(**vernier_options) do
yield
end
Expand Down

0 comments on commit 82a410e

Please sign in to comment.