model_observer 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4cefc88cdbb1e00c4b1550cb9f45edcc6b0fd078
4
- data.tar.gz: a0798211619936ad40912908f92ba157423c0695
3
+ metadata.gz: 1b2f28ed4ce8f3ca08a0174cffe2efa8f204cc8f
4
+ data.tar.gz: b7645d137b2c89cf2c62bc402f29d927f53edc95
5
5
  SHA512:
6
- metadata.gz: 96c0cf861d126fb6be493c7ee17cc4dc9090775187cb7d49c01bbd34ff77a310b8a81b6038b3af8453b78075ad66685c4a6c4994b6df25d53f6fa99d3e69a6f0
7
- data.tar.gz: 3182a0ed8c4cb77aadddeaafb5a971eb2e22f6a39123c6439ea083446de315dee9f5ae44e967e06af73880d63e53a01b8f4888ad63e8add7c3dc54d06312f3da
6
+ metadata.gz: 0971e467136f4ca7d050f78f52b27abe59462a34cf56c347a869ba6d8a99c2c9449ea9daad424ed16f74d244af35bede5fbb6f9fc0bf003ac7d2929fb60c121e
7
+ data.tar.gz: b0ad80b30466713bd869148efaf71cf039856ccee8e0384f6303647fec5ff9c405e20f2917e87e077010611f50b28d56ab23299ff247ff1f6ef9a2e57a336e67
data/README.md CHANGED
@@ -16,18 +16,18 @@ and bundle
16
16
 
17
17
  bundle
18
18
 
19
- ## Query Log
19
+ ## Configuration
20
20
 
21
- The time include instantiate models will be logged after each sql log like this:
21
+ Initialize ModelObserver at `config/environments/development.rb` with the following code:
22
22
 
23
- Author Load (0.5ms) SELECT `authors`.* FROM `authors` WHERE `authors`.`id` = 1 LIMIT 1
24
- Author Instantiate (1.5ms) SELECT `authors`.* FROM `authors` WHERE `authors`.`id` = 1 LIMIT 1
25
-
26
- 1.5ms == 0.5ms(DB query) + 1.0ms(model instantiation)
23
+ config.after_initialize do
24
+ ModelObserver.summary = true
25
+ ModelObserver.instantiation_log = true
26
+ end
27
27
 
28
28
  ## Summary Log
29
29
 
30
- The summary of each request will be added to the end like this:
30
+ The summary of each request will be appended to the end like this:
31
31
 
32
32
  ===== Model Observer Start =====
33
33
  Author: 1 sum(10.6ms) avg(10.6ms)
@@ -42,3 +42,14 @@ The summary of each request will be added to the end like this:
42
42
  id(507): 3
43
43
  id(536): 3
44
44
  ===== Model Observer End =======
45
+
46
+ `id(319): 3` means the models with same class(Book) and same id(319) have been instantiated three times.
47
+
48
+ ## Instantiation Log
49
+
50
+ The duration include query and instantiate models will be logged after each sql log like this:
51
+
52
+ Author Load (0.5ms) SELECT `authors`.* FROM `authors` WHERE `authors`.`id` = 1 LIMIT 1
53
+ Author Instantiate (1.5ms) SELECT `authors`.* FROM `authors` WHERE `authors`.`id` = 1 LIMIT 1
54
+
55
+ 1.5ms == 0.5ms(DB query) + 1.0ms(model instantiation)
@@ -9,20 +9,43 @@ module ModelObserver
9
9
  autoload :Analyser, 'model_observer/analyser'
10
10
  autoload :Rack, 'model_observer/rack'
11
11
 
12
- if active_record?
13
- if active_record4?
14
- autoload :ActiveRecord, 'model_observer/active_record4'
15
- ModelObserver::ActiveRecord.enable
16
- elsif active_record3?
17
- autoload :ActiveRecord, 'model_observer/active_record3'
18
- ModelObserver::ActiveRecord.enable
19
- else
20
- raise "Current version of active_record is not supported."
21
- end
22
- require 'model_observer/log_subscriber'
23
- end
12
+ attr_writer :summary, :instantiation_log
24
13
 
25
14
  class << self
15
+ def summary?
16
+ @summary
17
+ end
18
+
19
+ def instantiation_log?
20
+ @instantiation_log
21
+ end
22
+
23
+ def summary=(bool)
24
+ @summary = bool
25
+ if summary?
26
+ if active_record?
27
+ if active_record4?
28
+ autoload :ActiveRecord, 'model_observer/active_record4'
29
+ ModelObserver::ActiveRecord.enable
30
+ elsif active_record3?
31
+ autoload :ActiveRecord, 'model_observer/active_record3'
32
+ ModelObserver::ActiveRecord.enable
33
+ else
34
+ raise "Current version of active_record is not supported."
35
+ end
36
+ end
37
+ end
38
+ end
39
+
40
+ def instantiation_log=(bool)
41
+ @instantiation_log = bool
42
+ if instantiation_log?
43
+ if active_record?
44
+ require 'model_observer/log_subscriber'
45
+ end
46
+ end
47
+ end
48
+
26
49
  def start_request
27
50
  end
28
51
 
@@ -1,27 +1,22 @@
1
1
  module ModelObserver
2
2
  module ActiveRecord
3
- def self.enable
4
- require 'active_record'
5
-
6
- ::ActiveRecord::Inheritance::ClassMethods.class_eval do
7
- alias_method :origin_instantiate, :instantiate
8
-
9
- def instantiate(record)
10
- started_at = Time.now
11
- instance = origin_instantiate(record)
12
- ModelObserver::Collector.add_metric(ModelObserver::Metric.new(instance, started_at, Time.now))
13
- instance
14
- end
3
+ class << self
4
+ def enable
5
+ require 'active_record'
6
+ require 'model_observer/querying'
7
+ overwrite_instantiate
15
8
  end
16
9
 
17
- ::ActiveRecord::Querying.class_eval do
18
- alias_method :origin_find_by_sql, :find_by_sql
10
+ def overwrite_instantiate
11
+ ::ActiveRecord::Inheritance::ClassMethods.class_eval do
12
+ alias_method :origin_instantiate, :instantiate
19
13
 
20
- def find_by_sql(sql, binds = [])
21
- ::ActiveSupport::Notifications.instrumenter.instrument(
22
- "instantiate.model_observer",
23
- :sql => connection.to_sql(sanitize_sql(sql), binds),
24
- :name => "#{name} Instantiate") { origin_find_by_sql(sql, binds) }
14
+ def instantiate(record)
15
+ started_at = Time.now
16
+ instance = origin_instantiate(record)
17
+ ModelObserver::Collector.add_metric(ModelObserver::Metric.new(instance, started_at, Time.now))
18
+ instance
19
+ end
25
20
  end
26
21
  end
27
22
  end
@@ -1,27 +1,22 @@
1
1
  module ModelObserver
2
2
  module ActiveRecord
3
- def self.enable
4
- require 'active_record'
5
-
6
- ::ActiveRecord::Persistence::ClassMethods.class_eval do
7
- alias_method :origin_instantiate, :instantiate
8
-
9
- def instantiate(record, column_types = {})
10
- started_at = Time.now
11
- instance = origin_instantiate(record, column_types)
12
- ModelObserver::Collector.add_metric(ModelObserver::Metric.new(instance, started_at, Time.now))
13
- instance
14
- end
3
+ class << self
4
+ def enable
5
+ require 'active_record'
6
+ require 'model_observer/querying'
7
+ overwrite_instantiate
15
8
  end
16
9
 
17
- ::ActiveRecord::Querying.class_eval do
18
- alias_method :origin_find_by_sql, :find_by_sql
10
+ def overwrite_instantiate
11
+ ::ActiveRecord::Persistence::ClassMethods.class_eval do
12
+ alias_method :origin_instantiate, :instantiate
19
13
 
20
- def find_by_sql(sql, binds = [])
21
- ::ActiveSupport::Notifications.instrumenter.instrument(
22
- "instantiate.model_observer",
23
- :sql => connection.to_sql(sanitize_sql(sql), binds),
24
- :name => "#{name} Instantiate") { origin_find_by_sql(sql, binds) }
14
+ def instantiate(record, column_types = {})
15
+ started_at = Time.now
16
+ instance = origin_instantiate(record, column_types)
17
+ ModelObserver::Collector.add_metric(ModelObserver::Metric.new(instance, started_at, Time.now))
18
+ instance
19
+ end
25
20
  end
26
21
  end
27
22
  end
@@ -6,13 +6,21 @@ module ModelObserver
6
6
  Collector.metrics_hash.each do |key, value|
7
7
  sum_duration = value.map(&:duration).sum
8
8
  average_duration = sum_duration / value.count
9
- text << "#{key}: #{value.count} sum(#{sum_duration.round(1)}ms) avg(#{average_duration.round(1)}ms)\n"
9
+ text << "#{key}: #{value.count} #{sum_text(sum_duration)} #{average_text(average_duration)}\n"
10
10
  text << analyse_single_class(value)
11
11
  end
12
12
  text.sub!(/\n$/, '')
13
13
  text
14
14
  end
15
15
 
16
+ def sum_text(sum_duration)
17
+ "sum(#{sum_duration.round(1)}ms)"
18
+ end
19
+
20
+ def average_text(average_duration)
21
+ "avg(#{average_duration.round(1)}ms)"
22
+ end
23
+
16
24
  def analyse_single_class(metrics)
17
25
  return if metrics.empty?
18
26
  primary_key = metrics.first.model_class.primary_key
@@ -4,7 +4,7 @@ module ModelObserver
4
4
 
5
5
  def initialize(model, started_at, ended_at)
6
6
  @model_class = model.class
7
- @model_id = model.__send__(model.class.primary_key)
7
+ @model_id = model.__send__(model.class.primary_key) if model.class.primary_key
8
8
  @started_at = started_at
9
9
  @ended_at = ended_at
10
10
  end
@@ -1,14 +1,10 @@
1
- module ModelObserver
2
- module Querying
3
- ::ActiveRecord::Querying.class_eval do
4
- alias_method :origin_find_by_sql, :find_by_sql
1
+ ::ActiveRecord::Querying.class_eval do
2
+ alias_method :origin_find_by_sql, :find_by_sql
5
3
 
6
- def find_by_sql(sql, binds = [])
7
- ::ActiveSupport::Notifications.instrumenter.instrument(
8
- "instantiate.active_record",
9
- :sql => sql,
10
- :name => "#{name} Load") { origin_find_by_sql }
11
- end
12
- end
4
+ def find_by_sql(sql, binds = [])
5
+ ActiveSupport::Notifications.instrumenter.instrument(
6
+ "instantiate.model_observer",
7
+ :sql => connection.to_sql(sanitize_sql(sql), binds),
8
+ :name => "#{name} Instantiate") { origin_find_by_sql(sql, binds) }
13
9
  end
14
10
  end
@@ -1,3 +1,3 @@
1
1
  module ModelObserver
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: model_observer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Weihu Chen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-11 00:00:00.000000000 Z
11
+ date: 2014-01-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sqlite3
@@ -81,7 +81,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
81
81
  version: '0'
82
82
  requirements: []
83
83
  rubyforge_project:
84
- rubygems_version: 2.1.9
84
+ rubygems_version: 2.0.14
85
85
  signing_key:
86
86
  specification_version: 4
87
87
  summary: Help to find out duplicated model object creations.