active-record-instance-count 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in active-record-instance-count.gemspec
4
+ gemspec
@@ -0,0 +1,29 @@
1
+ # ActiveRecordInstanceCount
2
+
3
+ Rack middleware to log the number of instantiated ActiveRecord models in a request. Helpful to see how many objects you're instantiating. First step to recognize you have a problem.
4
+
5
+ Logs a message like:
6
+
7
+ ```
8
+ Instantiation Breakdown: Total: 3 | UserSession: 1 | User: 1 | UserDevice: 1
9
+ ```
10
+
11
+ ## Installation
12
+
13
+ Add to your `Gemfile`:
14
+
15
+ ```
16
+ gem 'active-record-instance-count'
17
+ ```
18
+
19
+ Then provide an initializer, like `config/initializers/active_record_instance_counter.rb`:
20
+
21
+ ```
22
+ YourApp::Application.middleware.use( ActiveRecordInstanceCount::Middleware, :logger => Rails.logger )
23
+ ```
24
+
25
+ Provide a different `Logger` instance if you would like to redirect the output somewhere else. But keep in mind that no more context will be provided (e.g. the request details) so it might be fairly useless.
26
+
27
+ ## Credits
28
+
29
+ _Heavily_ inspired from [Oink](https://github.com/noahd1/oink)
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "active-record-instance-count/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "active-record-instance-count"
7
+ s.version = ActiveRecordInstanceCount::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Cody Caughlan"]
10
+ s.email = ["toolbag@gmail.com"]
11
+ s.homepage = ""
12
+ s.summary = "Rack middleware to write the number of instantiated ActiveRecord models to a log"
13
+ s.description = s.summary
14
+
15
+ #s.rubyforge_project = "active-record-instance-count"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ # specify any dependencies here; for example:
23
+ # s.add_development_dependency "rspec"
24
+ # s.add_runtime_dependency "rest-client"
25
+ end
@@ -0,0 +1,36 @@
1
+ require "active-record-instance-count/version"
2
+ require "active-record-instance-count/utils/hash_utils"
3
+ require "active-record-instance-count/instrumentation"
4
+
5
+ module ActiveRecordInstanceCount
6
+ class Middleware
7
+ def initialize(app, options = {})
8
+ @app = app
9
+ unless options[:logger]
10
+ raise 'Missing options[:logger] argument'
11
+ end
12
+ @logger = options[:logger]
13
+ ActiveRecord::Base.send(:include, ActiveRecordInstanceCount::Instrumentation::ActiveRecord)
14
+ end
15
+
16
+ def call(env)
17
+ status, headers, body = @app.call(env)
18
+ log_activerecord
19
+ [status, headers, body]
20
+ end
21
+
22
+ def log_activerecord
23
+ sorted_list = ActiveRecordInstanceCount::HashUtils.to_sorted_array(ActiveRecord::Base.instantiated_hash)
24
+ sorted_list.unshift("Total: #{ActiveRecord::Base.total_objects_instantiated}")
25
+ @logger.info("Instantiation Breakdown: #{sorted_list.join(' | ')}")
26
+ reset_objects_instantiated
27
+ end
28
+
29
+ private
30
+
31
+ def reset_objects_instantiated
32
+ ActiveRecord::Base.reset_instance_type_count
33
+ end
34
+
35
+ end
36
+ end
@@ -0,0 +1 @@
1
+ require "active-record-instance-count/instrumentation/active_record"
@@ -0,0 +1,63 @@
1
+ module ActiveRecordInstanceCount
2
+
3
+ def self.extended_active_record?
4
+ @_extended_active_record
5
+ end
6
+
7
+ def self.extended_active_record!
8
+ @_extended_active_record = true
9
+ end
10
+
11
+ module Instrumentation
12
+ module ActiveRecord
13
+
14
+ def self.included(klass)
15
+ klass.class_eval do
16
+
17
+ @@instantiated = {}
18
+ @@total = nil
19
+
20
+ def self.reset_instance_type_count
21
+ @@instantiated = {}
22
+ @@total = nil
23
+ end
24
+
25
+ def self.increment_instance_type_count
26
+ @@instantiated[base_class.name] ||= 0
27
+ @@instantiated[base_class.name] += 1
28
+ end
29
+
30
+ def self.instantiated_hash
31
+ @@instantiated
32
+ end
33
+
34
+ def self.total_objects_instantiated
35
+ @@total ||= @@instantiated.values.sum
36
+ end
37
+
38
+ unless ActiveRecordInstanceCount.extended_active_record?
39
+ class << self
40
+ alias_method :allocate_before_counter, :allocate
41
+
42
+ def allocate
43
+ value = allocate_before_counter
44
+ increment_instance_type_count
45
+ value
46
+ end
47
+ end
48
+
49
+ alias_method :initialize_before_counter, :initialize
50
+
51
+ def initialize(*args, &block)
52
+ value = initialize_before_counter(*args, &block)
53
+ self.class.increment_instance_type_count
54
+ value
55
+ end
56
+
57
+ ActiveRecordInstanceCount.extended_active_record!
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,9 @@
1
+ module ActiveRecordInstanceCount
2
+ module HashUtils
3
+
4
+ def self.to_sorted_array(hsh)
5
+ hsh.sort{ |a, b| b[1] <=>a [1] }.collect { |k,v| "#{k}: #{v}" }
6
+ end
7
+
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module ActiveRecordInstanceCount
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active-record-instance-count
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Cody Caughlan
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-01 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: Rack middleware to write the number of instantiated ActiveRecord models
15
+ to a log
16
+ email:
17
+ - toolbag@gmail.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - .gitignore
23
+ - Gemfile
24
+ - README.md
25
+ - Rakefile
26
+ - active-record-instance-count.gemspec
27
+ - lib/active-record-instance-count.rb
28
+ - lib/active-record-instance-count/instrumentation.rb
29
+ - lib/active-record-instance-count/instrumentation/active_record.rb
30
+ - lib/active-record-instance-count/utils/hash_utils.rb
31
+ - lib/active-record-instance-count/version.rb
32
+ homepage: ''
33
+ licenses: []
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements: []
51
+ rubyforge_project:
52
+ rubygems_version: 1.8.16
53
+ signing_key:
54
+ specification_version: 3
55
+ summary: Rack middleware to write the number of instantiated ActiveRecord models to
56
+ a log
57
+ test_files: []