Skip to content

Commit b730244

Browse files
committed
Add Benchmark.ms method and enhance realtime with unit parameter
Rails previously included a monkeypatch that added a `ms` method to the Benchmark module to return timing results in milliseconds instead of seconds. This monkeypatch has been deprecated in Rails and will be removed in future versions. ref: rails/rails@4cdf757 This commit adds native support for millisecond timing measurements by: 1. **Enhanced Benchmark.realtime method**: Now accepts an optional `unit` parameter (defaulting to `:float_second` for backward compatibility) that leverages `Process.clock_gettime`'s built-in unit support. 2. **New Benchmark.ms method**: A convenience method that returns elapsed time in milliseconds, equivalent to `realtime(:float_millisecond)`. For web applications, measuring performance in milliseconds is much more helpful since the majority of operations happen in less than a second. While it's acceptable to display times >1s as "1.2s", showing "0.125s" instead of "125ms" is significantly less readable and intuitive for developers analyzing performance metrics. ```ruby Benchmark.realtime { sleep 0.1 } #=> 0.10023 Benchmark.realtime(:float_millisecond) { sleep 0.1 } #=> 100.23 Benchmark.ms { sleep 0.1 } #=> 100.23 ``` This change provides a clean migration path for Rails applications currently relying on the deprecated monkeypatch while offering enhanced functionality for all benchmark users. As an alternative approach, the .realtime method now accepts an optional unit argument that gets passed directly to Process.clock_gettime, allowing developers to specify their preferred time unit (:float_second, :float_millisecond, :float_microsecond, :nanosecond) while maintaining backward compatibility.
1 parent 4e39de6 commit b730244

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

lib/benchmark.rb

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -314,18 +314,33 @@ def measure(label = "") # :yield:
314314

315315
#
316316
# Returns the elapsed real time used to execute the given block.
317-
# The unit of time is seconds.
317+
# The unit of time is seconds by default, or can be specified with the unit parameter.
318+
# See documentation for `Process.clock_gettime`` for supported units.
318319
#
319320
# Benchmark.realtime { "a" * 1_000_000_000 }
320321
# #=> 0.5098029999935534
321322
#
322-
def realtime # :yield:
323-
r0 = Process.clock_gettime(Process::CLOCK_MONOTONIC)
323+
# Benchmark.realtime(:float_millisecond) { "a" * 1_000_000_000 }
324+
# #=> 509.8029999935534
325+
#
326+
def realtime(unit = :float_second) # :yield:
327+
r0 = Process.clock_gettime(Process::CLOCK_MONOTONIC, unit)
324328
yield
325-
Process.clock_gettime(Process::CLOCK_MONOTONIC) - r0
329+
Process.clock_gettime(Process::CLOCK_MONOTONIC, unit) - r0
330+
end
331+
332+
#
333+
# Returns the elapsed real time used to execute the given block.
334+
# The unit of time is milliseconds.
335+
#
336+
# Benchmark.ms { "a" * 1_000_000_000 }
337+
# #=> 509.8029999935534
338+
#
339+
def ms # :yield:
340+
realtime(:float_millisecond) { yield }
326341
end
327342

328-
module_function :benchmark, :measure, :realtime, :bm, :bmbm
343+
module_function :benchmark, :measure, :realtime, :ms, :bm, :bmbm
329344

330345
#
331346
# A Job is a sequence of labelled blocks to be processed by the

0 commit comments

Comments
 (0)