Skip to content

Commit 4cdf757

Browse files
committed
Deprecate Benchmark.ms and add benchmark to the gemspec
Ruby plans to make `benchmark` a bundled gem: ruby/ruby#11492 It doesn't make much sense to continue this core extension since rails would need to add it to its gemspec to continue offering it for what basically amounts to `x * 1000`. Since requiring it could emit a warning, add it to the gemspec until it can later be removed again with Rails 8.1 The benchmark generator will continue to work as usual: It adds `benchmark-ips` to the users Gemfile.
1 parent 734b7f4 commit 4cdf757

File tree

12 files changed

+77
-23
lines changed

12 files changed

+77
-23
lines changed

Gemfile.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ PATH
8787
marcel (~> 1.0)
8888
activesupport (8.0.0.alpha)
8989
base64
90+
benchmark (>= 0.3)
9091
bigdecimal
9192
concurrent-ruby (~> 1.0, >= 1.3.1)
9293
connection_pool (>= 2.2.5)
@@ -162,6 +163,7 @@ GEM
162163
bcrypt (3.1.20)
163164
bcrypt_pbkdf (1.1.0)
164165
beaneater (1.1.3)
166+
benchmark (0.3.0)
165167
bigdecimal (3.1.8)
166168
bindex (0.8.1)
167169
bootsnap (1.17.0)

actionpack/lib/action_controller/metal/instrumentation.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
# :markup: markdown
44

5-
require "benchmark"
65
require "abstract_controller/logger"
76

87
module ActionController
@@ -29,7 +28,7 @@ def initialize(...) # :nodoc:
2928
def render(*)
3029
render_output = nil
3130
self.view_runtime = cleanup_view_runtime do
32-
Benchmark.ms { render_output = super }
31+
ActiveSupport::Benchmark.realtime(:float_millisecond) { render_output = super }
3332
end
3433
render_output
3534
end

activerecord/lib/active_record/migration.rb

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# frozen_string_literal: true
22

3-
require "benchmark"
43
require "set"
54
require "active_support/core_ext/array/access"
65
require "active_support/core_ext/enumerable"
@@ -975,16 +974,16 @@ def migrate(direction)
975974
when :down then announce "reverting"
976975
end
977976

978-
time = nil
977+
time_elapsed = nil
979978
ActiveRecord::Tasks::DatabaseTasks.migration_connection.pool.with_connection do |conn|
980-
time = Benchmark.measure do
979+
time_elapsed = ActiveSupport::Benchmark.realtime do
981980
exec_migration(conn, direction)
982981
end
983982
end
984983

985984
case direction
986-
when :up then announce "migrated (%.4fs)" % time.real; write
987-
when :down then announce "reverted (%.4fs)" % time.real; write
985+
when :up then announce "migrated (%.4fs)" % time_elapsed; write
986+
when :down then announce "reverted (%.4fs)" % time_elapsed; write
988987
end
989988
end
990989

@@ -1025,8 +1024,8 @@ def say(message, subitem = false)
10251024
def say_with_time(message)
10261025
say(message)
10271026
result = nil
1028-
time = Benchmark.measure { result = yield }
1029-
say "%.4fs" % time.real, :subitem
1027+
time_elapsed = ActiveSupport::Benchmark.realtime { result = yield }
1028+
say "%.4fs" % time_elapsed, :subitem
10301029
say("#{result} rows", :subitem) if result.is_a?(Integer)
10311030
result
10321031
end

activerecord/test/cases/secure_password_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,17 @@ class SecurePasswordTest < ActiveRecord::TestCase
2929
User.authenticate_by(token: @user.token, password: @user.password)
3030

3131
retry_flaky_test do
32-
# Benchmark.realtime returns fractional seconds. Thus, summing over 1000
32+
# ActiveSupport::Benchmark.realtime returns fractional seconds. Thus, summing over 1000
3333
# iterations is equivalent to averaging over 1000 iterations and then
3434
# multiplying by 1000 to convert to milliseconds.
3535
found_average_time_in_ms = 1000.times.sum do
36-
Benchmark.realtime do
36+
ActiveSupport::Benchmark.realtime do
3737
User.authenticate_by(token: @user.token, password: @user.password)
3838
end
3939
end
4040

4141
not_found_average_time_in_ms = 1000.times.sum do
42-
Benchmark.realtime do
42+
ActiveSupport::Benchmark.realtime do
4343
User.authenticate_by(token: "wrong", password: @user.password)
4444
end
4545
end

activesupport/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
* Deprecate `Benchmark.ms` core extension.
2+
3+
The `benchmark` gem will become bundled in Ruby 3.5
4+
5+
*Earlopain*
6+
17
* `ActiveSupport::TimeWithZone#inspect` now uses ISO 8601 style time like `Time#inspect`
28

39
*John Hawthorn*

activesupport/activesupport.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,5 @@ Gem::Specification.new do |s|
4545
s.add_dependency "logger", ">= 1.4.2"
4646
s.add_dependency "securerandom", ">= 0.3"
4747
s.add_dependency "uri", ">= 0.13.1"
48+
s.add_dependency "benchmark", ">= 0.3"
4849
end

activesupport/lib/active_support.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ module ActiveSupport
5858
eager_autoload do
5959
autoload :BacktraceCleaner
6060
autoload :ProxyObject
61+
autoload :Benchmark
6162
autoload :Benchmarkable
6263
autoload :Cache
6364
autoload :Callbacks
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# frozen_string_literal: true
2+
3+
module ActiveSupport
4+
module Benchmark # :nodoc:
5+
# Benchmark realtime in the specified time unit. By default,
6+
# the returned unit is in seconds.
7+
#
8+
# ActiveSupport::Benchmark.realtime { sleep 0.1 }
9+
# # => 0.10007
10+
#
11+
# ActiveSupport::Benchmark.realtime(:float_millisecond) { sleep 0.1 }
12+
# # => 100.07
13+
#
14+
# `unit` can be any of the values accepted by Ruby's `Process.clock_gettime`.
15+
def self.realtime(unit = :float_second, &block)
16+
time_start = Process.clock_gettime(Process::CLOCK_MONOTONIC, unit)
17+
yield
18+
Process.clock_gettime(Process::CLOCK_MONOTONIC, unit) - time_start
19+
end
20+
end
21+
end

activesupport/lib/active_support/benchmarkable.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# frozen_string_literal: true
22

3-
require "active_support/core_ext/benchmark"
43
require "active_support/core_ext/hash/keys"
54

65
module ActiveSupport
@@ -41,7 +40,9 @@ def benchmark(message = "Benchmarking", options = {}, &block)
4140
options[:level] ||= :info
4241

4342
result = nil
44-
ms = Benchmark.ms { result = options[:silence] ? logger.silence(&block) : yield }
43+
ms = ActiveSupport::Benchmark.realtime(:float_millisecond) do
44+
result = options[:silence] ? logger.silence(&block) : yield
45+
end
4546
logger.public_send(options[:level], "%s (%.1fms)" % [ message, ms ])
4647
result
4748
else

activesupport/lib/active_support/core_ext/benchmark.rb

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,11 @@
33
require "benchmark"
44

55
class << Benchmark
6-
# Benchmark realtime in milliseconds.
7-
#
8-
# Benchmark.realtime { User.all }
9-
# # => 8.0e-05
10-
#
11-
# Benchmark.ms { User.all }
12-
# # => 0.074
13-
def ms(&block)
14-
1000 * realtime(&block)
6+
def ms(&block) # :nodoc
7+
# NOTE: Please also remove the Active Support `benchmark` dependency when removing this
8+
ActiveSupport.deprecator.warn <<~TEXT
9+
`Benchmark.ms` is deprecated and will be removed in Rails 8.1 without replacement.
10+
TEXT
11+
ActiveSupport::Benchmark.realtime(:float_millisecond, &block)
1512
end
1613
end

0 commit comments

Comments
 (0)