← Home

Rails Generate benchmark

Reference & Command Builder

Published: 2023-07-11
Author: Harrison Broadbent
Type: Reference & Command Builder

Rails Generate Benchmark

Rails Generate Benchmark (rails g benchmark) is the generator for Rails Performance Tests. These are a special type of integration test, that let you profile the memory and CPU usage of your app.

Once you've written your benchmarks, you can run them in either benchmark or profiling mode —

  • rake test:benchmark will benchmark your code; it makes it easy to quickly gather some basic metrics for each test, and runs each test 4 times.
  • rake test:profile will profile your code; it performs a more in-depth analysis of your code, and by default, runs each test once.

Here's an example output from a benchmark test —

# example from rake test:benchmark
BrowsingTest#test_homepage (31 ms warmup)
  wall_time: 6 ms
  memory: 437.27 KB
  objects: 5,514
  gc_runs: 0
  gc_time: 19 ms

Command Builder for rails g benchmark

rails g benchmark 

Benchmark name

Benchmark reports

Generator Options

Runtime Options

# Command Options
❯ rails g benchmark

  rails generate benchmark NAME [one two three] [options]

  [--skip-namespace], [--no-skip-namespace]              # Skip namespace (affects only isolated engines)
  [--skip-collision-check], [--no-skip-collision-check]  # Skip collision check

Runtime options:
  -f, [--force]                    # Overwrite files that already exist
  -p, [--pretend], [--no-pretend]  # Run but do not make any changes
  -q, [--quiet], [--no-quiet]      # Suppress status output
  -s, [--skip], [--no-skip]        # Skip files that already exist

# Command Description
    Generate benchmarks to compare performance optimizations.

    Makes use of the `benchmark-ips` gem as it provides a number of benefits like:
    - Simple significance test
    - Automatic warmup
    - No need to specify the number of iterations

    `bin/rails generate benchmark opt_compare`

    This will create:

    You can run the generated benchmark file using:
        `ruby script/benchmarks/opt_compare.rb`

    You can specify different reports:
        `bin/rails generate benchmark opt_compare patch1 patch2 patch3`