Skip to content

cctiger36/model_observer

Repository files navigation

ModelObserver

A rails plugin to count the model object creations between each request, and help to find out the duplication.

Build Status Gem Version Code Climate Coverage Status

Installation

Add to your Gemfile

group :development do
  gem 'model_observer'
end

and bundle

bundle

Configuration

Initialize ModelObserver at config/environments/development.rb with the following code:

config.after_initialize do
  ModelObserver.summary = true
  ModelObserver.instantiation_log = true
end

Summary Log

The summary of each request will be appended to the end like this:

===== Model Observer Start =====
Author: 1 sum(10.6ms) avg(10.6ms)
Book: 27 sum(25.7ms) avg(1.0ms)
  id(319): 3
  id(377): 3
  id(487): 3
  id(489): 3
  id(493): 3
  id(496): 3
  id(499): 3
  id(507): 3
  id(536): 3
===== Model Observer End =======

id(319): 3 means the models with same class(Book) and same id(319) have been instantiated three times.

Instantiation Log

The duration include query and instantiate models will be logged after each sql log like this:

Author Load (0.5ms)  SELECT `authors`.* FROM `authors` WHERE `authors`.`id` = 1 LIMIT 1
Author Instantiate (1.5ms)  SELECT `authors`.* FROM `authors` WHERE `authors`.`id` = 1 LIMIT 1

1.5ms == 0.5ms(DB query) + 1.0ms(model instantiation)

About

Help to find out duplicated model object creations.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages