Sha256: 333861e9e64aa6b8865bc2b90222b38fd92cc61e9d81f98e0a72371eb4990aa8

Contents?: true

Size: 954 Bytes

Versions: 3

Compression:

Stored size: 954 Bytes

Contents

module FactoryBotInstruments
  module Benchmarking
    def benchmark_all(
      except: [],
      methods: [:create, :build, :build_stubbed],
      progress: false)

      factories = FactoryBot.factories.map(&:name) - except

      report = factories.map do |factory|
        puts "Processing #{factory}" if progress

        methods.map do |method|
          benchmark(factory, :method => method)
        end
      end

      report.flatten.sort_by(&:duration)
    end

    def benchmark(factory, method: :create)
      start = Time.now

      ActiveRecord::Base.transaction do
        FactoryBot.public_send(method, factory)

        raise ActiveRecord::Rollback
      end

      Benchmark.new(factory, method, Time.now - start)
    end
  end

  Benchmark = Struct.new(:factory, :method, :duration) do
    def to_s
      formated_duration = format("%5.3fs", duration)

      "#{formated_duration}: FactoryBot.#{method}(:#{factory})"
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
factory_bot_instruments-1.2.0 lib/factory_bot_instruments/benchmarking.rb
factory_bot_instruments-1.1.0 lib/factory_bot_instruments/benchmarking.rb
factory_bot_instruments-1.0.1 lib/factory_bot_instruments/benchmarking.rb