diff --git a/lib/singed.rb b/lib/singed.rb index 34cdd2d..3886fa8 100644 --- a/lib/singed.rb +++ b/lib/singed.rb @@ -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 @@ -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 diff --git a/lib/singed/flamegraph.rb b/lib/singed/flamegraph.rb index ee90362..1488444 100644 --- a/lib/singed/flamegraph.rb +++ b/lib/singed/flamegraph.rb @@ -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, @@ -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 + + end class Vernier < Flamegraph @@ -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