All Projects → shiroyasha → Factory_bot_instruments

shiroyasha / Factory_bot_instruments

Licence: mit
Instruments for benchmarking, tracing, and debugging Factory Girl models.

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to Factory bot instruments

Mailcatcher
Catches mail and serves it through a dream.
Stars: ✭ 5,512 (+5003.7%)
Mutual labels:  rails, rubygems
Rails Settings
Manage settings with Ruby on Rails
Stars: ✭ 807 (+647.22%)
Mutual labels:  rails, rubygems
Unread
Handle unread records and mark them as read with Ruby on Rails
Stars: ✭ 638 (+490.74%)
Mutual labels:  rails, rubygems
Pluck to hash
Extend ActiveRecord pluck to return array of hashes
Stars: ✭ 275 (+154.63%)
Mutual labels:  rails, rubygems
Tabler Rubygem
Rubygem for https://tabler.github.io
Stars: ✭ 77 (-28.7%)
Mutual labels:  rails, rubygems
Tapping device
TappingDevice makes objects tell you what they do, so you don't need to track them yourself.
Stars: ✭ 296 (+174.07%)
Mutual labels:  rails, tracing
Materialize Sass
Materializecss rubygem for Rails Asset Pipeline / Sprockets
Stars: ✭ 785 (+626.85%)
Mutual labels:  rails, rubygems
Agoo
A High Performance HTTP Server for Ruby
Stars: ✭ 679 (+528.7%)
Mutual labels:  rails, benchmark
Drafting
Ruby gem for saving drafts of ActiveRecord models
Stars: ✭ 41 (-62.04%)
Mutual labels:  rails, rubygems
Api schema
DSL for describing APIs and generate swagger json
Stars: ✭ 18 (-83.33%)
Mutual labels:  rails, rubygems
ruby-sensor
💎 Ruby Distributed Tracing & Metrics Sensor for Instana
Stars: ✭ 23 (-78.7%)
Mutual labels:  rubygems, tracing
Pluck all
A more efficient way to get data from database. Like #pluck method but return array of hashes instead.
Stars: ✭ 83 (-23.15%)
Mutual labels:  rails, rubygems
perforator
Record "perf" performance metrics for individual functions/regions of an ELF binary.
Stars: ✭ 33 (-69.44%)
Mutual labels:  benchmark, tracing
Deep pluck
Allow you to pluck attributes from nested associations without loading a bunch of records.
Stars: ✭ 385 (+256.48%)
Mutual labels:  rails, rubygems
Vueonrails
💎 Rails gem with the power of Vue.js components
Stars: ✭ 250 (+131.48%)
Mutual labels:  rails, rubygems
Leaky Gems
A list of Ruby gems that have known memory leaks (and issues)
Stars: ✭ 895 (+728.7%)
Mutual labels:  rails, rubygems
Karafka
Framework for Apache Kafka based Ruby and Rails applications development.
Stars: ✭ 1,223 (+1032.41%)
Mutual labels:  rails, rubygems
Rails or
Cleaner syntax for writing OR Query in Rails 5, 6. And also add #or support to Rails 3 and 4.
Stars: ✭ 86 (-20.37%)
Mutual labels:  rails, rubygems
Rails Letsencrypt
The Let's Encrypt certificate manager for rails
Stars: ✭ 104 (-3.7%)
Mutual labels:  rails
Rubocop Rails config
RuboCop configuration which has the same code style checking as official Ruby on Rails.
Stars: ✭ 106 (-1.85%)
Mutual labels:  rails

Factory Bot Instruments

Build Status Gem Version

Instruments for benchmarking, tracing, and debugging Factory Bot models.

Table of content:

Purpose of this gem

Factory Bot is probably the base of your Rails test suite, but how deeply you understand the models and the associations that are created in your tests?

Factory Bot Instruments help in these three aspects:

  1. Slow test suites: Factory Bot is used for the bulk of tests in Rails. Even a small performance improvement in one of your factories can dramatically improve the speed of your overall test suite.

    Hint: Run FactoryBot.benchmark_all.

  2. Deeper understanding of the database state: By tracing factory bot and SQL calls you can get a deeper understanding of what is actually created in your tests, helping you to debug the issues faster.

    Hint: Run FactoryBot.trace { FactoryBot.create(:user) }.

  3. Find issues with missconfigured factories: When a factory is properly set up it is a bliss to work with it. However, if there is a hidden deep in the association chain debugging the created model can be a hellish experience.

    Hint: Run FactoryBot.trace { FactoryBot.create(:user) } and observe the chain of calls.

Install

Add the following to your Gemfile:

gem 'factory_bot_instruments'

and run bundle install from your shell.

To install the gem manually from your shell, run:

gem install factory_bot_instruments

Documentation

Benchmarking one Factory Bot model

If you have a user factory, you can benchmark it with:

FactoryBot.benchmark(:user)

By default, the FactoryBot.create(<model>) is called. You can pass :method to override this:

FactoryBot.benchmark(:user, :method => :build_stubbed)

The above snippet will call FactoryBot.build_stubbed(:user).

Benchmarking all Factory Bot models

To collect benchmarking information from all Factory Bot models:

FactoryBot.benchmark_all

To skip a factory, pass the :except options:

FactoryBot.benchmark_all(:except => [:user])

By default, benchmarks for FactoryBot.create(<model>), FactoryBot.build(<model>), FactoryBot.build_stubbed(<model>) are collected. You can override this by passing an array of methods:

FactoryBot.benchmark_all(:methods => [:create]) # benchmark only :create

Tracing Factory Bot calls

To trace factory Bot actions, wrap your call in the FactoryBot.trace method:

FactoryBot.trace do
  FactoryBot.create(:comment)
end

The above snippet will output the following tree:

┌ (start) create :comment
|  ┌ (start) create :user
|  |  (0.1ms)  begin transaction
|  |  (0.4ms)  INSERT INTO "users" ("name", "username") VALUES (?, ?)  [["name", "Peter Parker"], ["username", "spiderman"]]
|  |  (2.3ms)  commit transaction
|  └ (finish) create :user [0.010s]
|  ┌ (start) create :article
|  |  ┌ (start) create :user
|  |  |  (0.1ms)  begin transaction
|  |  |  (0.3ms)  INSERT INTO "users" ("name", "username") VALUES (?, ?)  [["name", "Peter Parker"], ["username", "spiderman"]]
|  |  |  (1.8ms)  commit transaction
|  |  └ (finish) create :user [0.007s]
|  |  (0.1ms)  begin transaction
|  |  (0.2ms)  INSERT INTO "articles" ("title", "content", "user_id") VALUES (?, ?, ?)  [["title", "New Article"], ["content", "article content"], ["user_id", "121"]]
|  |  (1.5ms)  commit transaction
|  └ (finish) create :article [0.021s]
|  (0.1ms)  begin transaction
|  (0.2ms)  INSERT INTO "comments" ("content", "user_id", "article_id") VALUES (?, ?, ?)  [["content", "First!"], ["user_id", "120"], ["article_id", "61"]]
|  (1.5ms)  commit transaction
└ (finish) create :comment [0.046s]

To trace without SQL logs, use the following:

FactoryBot.trace(sql: false) do
  FactoryBot.create(:comment)
end

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/shiroyasha/factory_girl_instruments. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].