rails_request_stats 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/LICENSE +21 -0
- data/README.md +77 -0
- data/Rakefile +6 -0
- data/lib/rails_request_stats/notification_subscribers.rb +58 -0
- data/lib/rails_request_stats/report.rb +66 -0
- data/lib/rails_request_stats/request_stats.rb +32 -0
- data/lib/rails_request_stats/version.rb +3 -0
- data/lib/rails_request_stats.rb +7 -0
- data/rails_request_stats.gemspec +26 -0
- metadata +128 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 52c12f7e624aff9589705f54ebcfad9d0c5fd15f
|
4
|
+
data.tar.gz: e6db8268f92ac4ac512ce65c1afc51313e258bdd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 13cb4c0e7dc4c68daeb10a9a3d72c6567c165f2bc993625c14e419a855edff4a26e0190ce315090401e8488d633b8ba8925dd08e9d9e092c9d3fa03b0fd1bfb3
|
7
|
+
data.tar.gz: c6b74a309ac574e97882025082ce093577ac80af69607ee76ab4ffd2a012fd3a61933f8e5c31d0d89269aba34c1d592a9888a20eec45ff1bd075dddf10a5de88
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Kevin Jalbert
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
# RailsRequestStats
|
2
|
+
|
3
|
+
During development have you ever:
|
4
|
+
|
5
|
+
* Wondered how many SQL queries occurred during a request?
|
6
|
+
* Been curious on average view and database runtime for a request?
|
7
|
+
* Wanted a report containing overall statistics of all unique requests?
|
8
|
+
* Wanted a better way to iteratively optimize requests?
|
9
|
+
|
10
|
+
`RailsRequestStats` provides a simple drop-in solution to expose more statistics on requests. New information is presented in your development logs, supplying you with the required information to iteratively optimize requests by noticing subtle changes in the number of queries and average runtimes.
|
11
|
+
|
12
|
+
## How this Works
|
13
|
+
|
14
|
+
`RailsRequestStats::NotificationSubscribers` when required will subscribe to the `sql.active_record`, and `process_action.action_controller` `ActionSupport::Notifications`.
|
15
|
+
|
16
|
+
The `sql.active_record` event allow us to count each SQL query that passes though ActiveRecord, which we count internally. The `process_action.action_controller` event provides us runtime information along with identifying controller action details. At this point we are able to synthesis the query information and runtime information and store them internally in running collection of `RailsRequestStats::RequestStats` objects.
|
17
|
+
|
18
|
+
**Note** the data collection is tracked and stored in class-level instance variables. Thus this is not threadsafe, as no concurrency mechanisms are used (i.e., mutex). For non-threaded and forking application servers this should be fine.
|
19
|
+
|
20
|
+
## Installation
|
21
|
+
|
22
|
+
Add this line to your application's Gemfile:
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
gem 'rails_request_stats', group: :development
|
26
|
+
```
|
27
|
+
|
28
|
+
## Example Outputs
|
29
|
+
|
30
|
+
Within the console ./log/development.log you should start seeing the following statement appearing at the end of processing a request:
|
31
|
+
|
32
|
+
```
|
33
|
+
[RailsRequestStats] (AVG view_runtime: 163.655ms | AVG db_runtime: 15.465ms | query_count: 9 | cached_query_count: 0)
|
34
|
+
```
|
35
|
+
|
36
|
+
Finally when you exit the application's server, you should see a report of all the data captured:
|
37
|
+
|
38
|
+
```
|
39
|
+
[RailsRequestStats] INDEX:html "/users" (AVG view_runtime: 128.492ms | AVG db_runtime: 9.186ms | MIN query_count: 8 | MAX query_count: 9) with 4 data points
|
40
|
+
[RailsRequestStats] SHOW:html "/users/2" (AVG view_runtime: 13.0429ms | AVG db_runtime: 1.69033ms | MIN query_count: 2 | MAX query_count: 2) with 3 data points
|
41
|
+
[RailsRequestStats] SHOW:html "/users/2?test=1&blah=2" (AVG view_runtime: 17.8252ms | AVG db_runtime: 1.621ms | MIN query_count: 2 | MAX query_count: 2) with 1 data points
|
42
|
+
```
|
43
|
+
|
44
|
+
## Customizing Outputs
|
45
|
+
|
46
|
+
You can manually override the output by monkey-patching in an initializer (`./config/initializers/rails_request_stats.rb`):
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
module RailsRequestStats
|
50
|
+
class Report
|
51
|
+
# Called after every request
|
52
|
+
def report_text
|
53
|
+
# Access to @request_stats (instance of RequestStats)
|
54
|
+
end
|
55
|
+
# Called after the application server is closed (via #at_exit_handler)
|
56
|
+
def exit_report_text
|
57
|
+
# Access to @request_stats (instance of RequestStats)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
class NotificationSubscribers
|
62
|
+
# Called when the application server is closed
|
63
|
+
def self.at_exit_handler
|
64
|
+
# Access to @requests (hash of { <paths> => RequestStats })
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
```
|
69
|
+
|
70
|
+
## Contributing
|
71
|
+
|
72
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/kevinjalbert/rails_request_stats. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
|
73
|
+
|
74
|
+
## License
|
75
|
+
|
76
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
77
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
module RailsRequestStats
|
2
|
+
class NotificationSubscribers
|
3
|
+
SCHEMA_NAME = 'SCHEMA'.freeze
|
4
|
+
CACHE_NAME = 'CACHE'.freeze
|
5
|
+
|
6
|
+
attr_reader :query_count,
|
7
|
+
:cached_query_count,
|
8
|
+
:requests
|
9
|
+
|
10
|
+
def self.reset_query_counts
|
11
|
+
@query_count = 0
|
12
|
+
@cached_query_count = 0
|
13
|
+
end
|
14
|
+
reset_query_counts
|
15
|
+
|
16
|
+
def self.reset_requests
|
17
|
+
@requests = {}
|
18
|
+
end
|
19
|
+
reset_requests
|
20
|
+
|
21
|
+
at_exit do
|
22
|
+
at_exit_handler
|
23
|
+
end
|
24
|
+
|
25
|
+
ActiveSupport::Notifications.subscribe('sql.active_record') do |*args|
|
26
|
+
handle_sql_event(args.extract_options!)
|
27
|
+
end
|
28
|
+
|
29
|
+
ActiveSupport::Notifications.subscribe('process_action.action_controller') do |*args|
|
30
|
+
handle_process_action_event(args.extract_options!)
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.at_exit_handler
|
34
|
+
@requests.each_value do |request_stats|
|
35
|
+
Rails.logger.info { Report.new(request_stats).exit_report_text }
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.handle_sql_event(event)
|
40
|
+
return if event[:name] == SCHEMA_NAME
|
41
|
+
|
42
|
+
@cached_query_count += 1 if event[:name] == CACHE_NAME
|
43
|
+
@query_count += 1
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.handle_process_action_event(event)
|
47
|
+
request_key = { action: event[:action], format: event[:format], method: event[:method], path: event[:path] }
|
48
|
+
|
49
|
+
request_stats = @requests[request_key] || RequestStats.new(request_key)
|
50
|
+
request_stats.add_stats(event[:view_runtime], event[:db_runtime], @query_count, @cached_query_count)
|
51
|
+
|
52
|
+
@requests[request_key] = request_stats
|
53
|
+
reset_query_counts
|
54
|
+
|
55
|
+
Rails.logger.info { Report.new(request_stats).report_text }
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
module RailsRequestStats
|
2
|
+
class Report
|
3
|
+
FORMAT_FLAG = '%g'.freeze
|
4
|
+
|
5
|
+
attr_reader :request_stats
|
6
|
+
|
7
|
+
def initialize(request_stats)
|
8
|
+
@request_stats = request_stats
|
9
|
+
end
|
10
|
+
|
11
|
+
def report_text
|
12
|
+
avg_view_runtime = "AVG view_runtime: #{format_number(avg_stat(:view_runtime))}ms"
|
13
|
+
avg_db_runtime = "AVG db_runtime: #{format_number(avg_stat(:db_runtime))}ms"
|
14
|
+
query_count = "query_count: #{format_number(last_stat(:query_count))}"
|
15
|
+
cached_query_count = "cached_query_count: #{format_number(last_stat(:cached_query_count))}"
|
16
|
+
|
17
|
+
"[RailsRequestStats] (#{[avg_view_runtime, avg_db_runtime, query_count, cached_query_count].join(' | ')})"
|
18
|
+
end
|
19
|
+
|
20
|
+
def exit_report_text
|
21
|
+
controller_information = "[RailsRequestStats] #{@request_stats.action.upcase}:#{@request_stats.format} \"#{@request_stats.path}\""
|
22
|
+
avg_view_runtime = "AVG view_runtime: #{format_number(avg_stat(:view_runtime))}ms"
|
23
|
+
avg_db_runtime = "AVG db_runtime: #{format_number(avg_stat(:db_runtime))}ms"
|
24
|
+
min_query_count = "MIN query_count: #{format_number(min_stat(:query_count))}"
|
25
|
+
max_query_count = "MAX query_count: #{format_number(max_stat(:query_count))}"
|
26
|
+
request_count = "from #{format_number(count_stat(:view_runtime))} requests"
|
27
|
+
|
28
|
+
"#{controller_information} (#{[avg_view_runtime, avg_db_runtime, min_query_count, max_query_count].join(' | ')}) #{request_count}"
|
29
|
+
end
|
30
|
+
|
31
|
+
def min_stat(category)
|
32
|
+
category_collection(category).min
|
33
|
+
end
|
34
|
+
|
35
|
+
def max_stat(category)
|
36
|
+
category_collection(category).max
|
37
|
+
end
|
38
|
+
|
39
|
+
def total_stat(category)
|
40
|
+
category_collection(category).reduce(:+)
|
41
|
+
end
|
42
|
+
|
43
|
+
def count_stat(category)
|
44
|
+
category_collection(category).size
|
45
|
+
end
|
46
|
+
|
47
|
+
def last_stat(category)
|
48
|
+
category_collection(category).last
|
49
|
+
end
|
50
|
+
|
51
|
+
def avg_stat(category)
|
52
|
+
total_stat(category).to_f / count_stat(category)
|
53
|
+
end
|
54
|
+
|
55
|
+
private
|
56
|
+
|
57
|
+
def format_number(number)
|
58
|
+
format(FORMAT_FLAG, number)
|
59
|
+
end
|
60
|
+
|
61
|
+
def category_collection(category)
|
62
|
+
collection_variable_name = "#{category}_collection"
|
63
|
+
@request_stats.public_send(collection_variable_name) if @request_stats.respond_to?(collection_variable_name)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module RailsRequestStats
|
2
|
+
class RequestStats
|
3
|
+
attr_reader :action,
|
4
|
+
:format,
|
5
|
+
:method,
|
6
|
+
:path,
|
7
|
+
|
8
|
+
:view_runtime_collection,
|
9
|
+
:db_runtime_collection,
|
10
|
+
:query_count_collection,
|
11
|
+
:cached_query_count_collection
|
12
|
+
|
13
|
+
def initialize(key)
|
14
|
+
@action = key[:action]
|
15
|
+
@format = key[:format]
|
16
|
+
@method = key[:method]
|
17
|
+
@path = key[:path]
|
18
|
+
|
19
|
+
@view_runtime_collection = []
|
20
|
+
@db_runtime_collection = []
|
21
|
+
@query_count_collection = []
|
22
|
+
@cached_query_count_collection = []
|
23
|
+
end
|
24
|
+
|
25
|
+
def add_stats(view_runtime, db_runtime, query_count, cached_query_count)
|
26
|
+
@view_runtime_collection << view_runtime.to_f
|
27
|
+
@db_runtime_collection << db_runtime.to_f
|
28
|
+
@query_count_collection << query_count
|
29
|
+
@cached_query_count_collection << cached_query_count
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'rails_request_stats/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'rails_request_stats'
|
8
|
+
spec.version = RailsRequestStats::VERSION
|
9
|
+
spec.authors = ['Kevin Jalbert']
|
10
|
+
spec.email = ['kevin.j.jalbert@gmail.com']
|
11
|
+
|
12
|
+
spec.summary = 'Provides additional development statistics on Rails requests in logfile'
|
13
|
+
spec.description = 'Provides additional development statistics on Rails requests in logfile'
|
14
|
+
spec.homepage = 'https://github.com/kevinjalbert/rails_request_stats/'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.require_paths = ['lib']
|
19
|
+
|
20
|
+
spec.add_development_dependency 'bundler', '~> 1.10'
|
21
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
22
|
+
spec.add_development_dependency 'rspec', '~> 3.4.0'
|
23
|
+
spec.add_development_dependency 'pry', '~> 0.10.0'
|
24
|
+
|
25
|
+
spec.add_runtime_dependency 'rails', '>= 3.0.0'
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails_request_stats
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kevin Jalbert
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-12-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 3.4.0
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 3.4.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.10.0
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.10.0
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rails
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 3.0.0
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 3.0.0
|
83
|
+
description: Provides additional development statistics on Rails requests in logfile
|
84
|
+
email:
|
85
|
+
- kevin.j.jalbert@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- ".travis.yml"
|
93
|
+
- CODE_OF_CONDUCT.md
|
94
|
+
- Gemfile
|
95
|
+
- LICENSE
|
96
|
+
- README.md
|
97
|
+
- Rakefile
|
98
|
+
- lib/rails_request_stats.rb
|
99
|
+
- lib/rails_request_stats/notification_subscribers.rb
|
100
|
+
- lib/rails_request_stats/report.rb
|
101
|
+
- lib/rails_request_stats/request_stats.rb
|
102
|
+
- lib/rails_request_stats/version.rb
|
103
|
+
- rails_request_stats.gemspec
|
104
|
+
homepage: https://github.com/kevinjalbert/rails_request_stats/
|
105
|
+
licenses:
|
106
|
+
- MIT
|
107
|
+
metadata: {}
|
108
|
+
post_install_message:
|
109
|
+
rdoc_options: []
|
110
|
+
require_paths:
|
111
|
+
- lib
|
112
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - ">="
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
117
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
118
|
+
requirements:
|
119
|
+
- - ">="
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '0'
|
122
|
+
requirements: []
|
123
|
+
rubyforge_project:
|
124
|
+
rubygems_version: 2.4.8
|
125
|
+
signing_key:
|
126
|
+
specification_version: 4
|
127
|
+
summary: Provides additional development statistics on Rails requests in logfile
|
128
|
+
test_files: []
|