crystal-lang / crystal

The Crystal Programming Language
https://crystal-lang.org
Apache License 2.0
19.2k stars 1.61k forks source link

`Benchmark.ips` does not accept `Time::Span` for calculation and warmup times #14758

Open HertzDevil opened 5 days ago

HertzDevil commented 5 days ago

The signatures of Benchmark.ips and Benchmark::IPS::Job#initialize are:

module Benchmark
  extend self

  def ips(calculation = 5, warmup = 2, interactive = STDOUT.tty?, &)
  end

  module IPS
    class Job
      def initialize(calculation = 5, warmup = 2, interactive = STDOUT.tty?)
        @warmup_time = warmup.seconds
        @calculation_time = calculation.seconds
        # ...
      end
    end
  end
end

Both calculation and warmup are durations in seconds, yet because of the Number#seconds call it is impossible to actually use a Time::Span here:

# Error: instance variable '@calculation_time' of Benchmark::IPS::Job must be Time::Span, not Int32
Benchmark.ips(5.seconds) do
end

I think those parameters should be restricted to Time::Spans, and Number arguments should be deprecated. (By the way, it seems the only other non-deprecated, public API that accepts a Number as a duration is ::sleep.)