TracePoint, benchmark and profilers

Benchmark.measure

Before a profiler, a stopwatch. Benchmark.measure runs a block and prints the user time, the system time, the total and the real elapsed time:

ruby -r benchmark -e 'puts Benchmark.measure { 1_000_000.times { |i| i * 2 } }'

0.097932 0.000021 0.097953 ( 0.098076)

The last column in parentheses is wall-clock time; the first is the CPU the process used. On a quiet machine they are close. The numbers move between runs, so compare two versions of the same code in the same session rather than remembering an absolute value.

Benchmark.bm prints a table for several blocks, which is the quick way to compare an implementation with its replacement:

require "benchmark" Benchmark.bm(10) do |x| x.report("map") { (1..100_000).map { _1 * 2 } } x.report("lazy") { (1..100_000).lazy.map { _1 * 2 }.to_a } end

ruby -r benchmark -e 'Benchmark.bm(10) do |x|; x.report("map") { (1..100_000).map { _1 * 2 } }; x.report("lazy") { (1..100_000).lazy.map { _1 * 2 }.to_a }; end'

user system total real map 0.014719 0.001144 0.015863 ( 0.015870) lazy 0.061941 0.000960 0.062901 ( 0.062912)

Lazy is four times slower here, which is the point of lesson 4.12: the chain of blocks costs more than the array it avoids building on a hundred thousand items. The table is worth the minute it takes, because the difference is not the one a reader guesses.

TracePoint

TracePoint is Ruby watching itself. A trace on :call fires for every method call while it is enabled, and the event object names the method:

ruby -e 'tp = TracePoint.new(:call) { |t| puts t.method_id }; def a; end; tp.enable { a }'

a

Only a is printed, because the trace covers the block and the block calls one method. The event also carries path, lineno, defined_class and self, which is enough to build a call log. The other useful events are :return, :raise and :line.

A trace on :line over a large program is slow: every line runs a block. Enable it around the one call that needs watching, as above, instead of for the whole program.

GC.stat

When a program gets slower over time, the allocator is a suspect. GC.stat is a hash of counters, and heap_live_slots is the number of objects alive right now:

ruby -e 'p GC.stat[:heap_live_slots]'

23875

A bare Ruby already holds a few thousand objects, so the absolute number says little. Read it twice, around the work: if the live slots climb by the size of the input on every iteration, something is kept that could be released. GC.stat[:total_allocated_objects] and GC.stat[:minor_gc_count] answer the related questions of how much was allocated and how often the collector ran.

A profiler

A stopwatch says a block is slow; a profiler says which line inside it. stackprof samples the stack and writes a dump that it can report on. The gem builds a native extension, so these commands ran on the host in the ruby:3.4 image:

gem install stackprof

1 gem installed

ruby -r stackprof -e 'StackProf.run(mode: :cpu, out: "/tmp/stackprof.dump") { 100_000.times { |i| i.to_s } }'

That command prints nothing and exits 0: the samples are in the dump, and the report reads them:

stackprof /tmp/stackprof.dump --text

================================== Mode: cpu(1000) Samples: 37 (0.00% miss rate) GC: 12 (32.43%) ================================== TOTAL (pct) SAMPLES (pct) FRAME 15 (40.5%) 15 (40.5%) Integer#to_s 9 (24.3%) 9 (24.3%) (sweeping) 21 (56.8%) 6 (16.2%) block (2 levels) in <main> 25 (67.6%) 4 (10.8%) Integer#times 3 (8.1%) 3 (8.1%) (marking) 25 (67.6%) 0 (0.0%) block in <main> 25 (67.6%) 0 (0.0%) StackProf.run 25 (67.6%) 0 (0.0%) <main> 12 (32.4%) 0 (0.0%) (garbage collection)

SAMPLES is the time spent in that frame itself; TOTAL adds the frames it called, so a method near the bottom of the list can still own most of the run. Here the top frame, Integer#to_s, is 40% and garbage collection is another third, which is the answer a stopwatch could not give: the loop spends its time converting integers and collecting them, not in times.

For a Rails application, rack-mini-profiler and the development log give the same information per request.

What to watch for

A benchmark of a method that does nothing is a benchmark of the optimizer. Measure the real input, or a smaller version of it, and keep the data in the same shape.

A profile from a development machine answers a different question from a profile in production. The slow path is often I/O or a database call, and neither appears in a CPU profile; measure the wall clock around the call before reaching for a sampling profiler.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does Benchmark.measure { ... } return?

    Answer

    A Benchmark::Tms object with the four times

    The object prints as user, system, total and real, and puts shows those columns.

  2. What does this program print? tp = TracePoint.new(:call) { |t| puts t.method_id } def a; end tp.enable { a }

    Answer

    a

    The trace covers the block, the block calls one method, and the event names it.

  3. What does this program print? p GC.stat.class

    Answer

    Hash

    GC.stat answers a hash of counters, keyed by names such as heap_live_slots.

  4. What does this program print? p GC.stat.key?(:heap_live_slots)

    Answer

    true

    heap_live_slots is one of the counters in the hash; the keys are symbols.

  5. Which column of Benchmark.measure is the wall-clock time?

    Answer

    The one in parentheses, real time

    User and system are CPU time; the parenthesised column is what a clock on the wall measured.

  6. Which event does TracePoint.new(:call) report?

    Answer

    Every method call while the trace is enabled

    :return, :raise and :line are the other useful events.

  7. In a stackprof report, what does the SAMPLES column count?

    Answer

    The time spent in that frame itself

    TOTAL adds the frames it called, so a low frame can still own most of the run.

  8. What does this program print? p GC.stat[:heap_live_slots] > 0

    Answer

    true

    A bare Ruby already holds thousands of live objects.

  9. Why compare two versions of the code in the same session instead of against a remembered number?

    Answer

    The times move between runs and between machines

    An absolute value from another day says nothing about this machine now.

  10. A CPU profile is flat while the slow path is a database call. Why?

    Answer

    Time waiting on I/O is not CPU time, so a CPU profile does not see it

    Measure the wall clock around the call before reaching for a sampling profiler.

Article author: Arthur Isaev