rails_log_profiling 0.1.0.beta1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +22 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +75 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/gemfiles/Gemfile.rails.4.0 +6 -0
- data/gemfiles/Gemfile.rails.4.1 +6 -0
- data/gemfiles/Gemfile.rails.4.2 +6 -0
- data/gemfiles/Gemfile.rails.5.0 +6 -0
- data/lib/rails/log/profiling/action_controller.rb +20 -0
- data/lib/rails/log/profiling/logger.rb +22 -0
- data/lib/rails/log/profiling/query_log_subscriber.rb +70 -0
- data/lib/rails/log/profiling/query_profiling.rb +25 -0
- data/lib/rails/log/profiling/version.rb +7 -0
- data/lib/rails_log_profiling.rb +40 -0
- data/rails_log_profiling.gemspec +36 -0
- metadata +106 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7800e088ecfa2142aa4fad1f77da49da36df9600
|
4
|
+
data.tar.gz: abe57b18ab6d405b3a2a84d2bf867f041f0a5a4c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 14315a93ed0c74e6086edd7220e1fec2609a9841c7bc9bb8941d2187ad8be70dbac744a2e0adf4fe9ecdf29ea5b7f7618bdf12ded068d71add8fe894cfdeab2a
|
7
|
+
data.tar.gz: bb2ba48c71eb404926f8d1a3188b3f68c779bbbda564fe4fd2494529498dbb18727bfaa007eb8c885e36c8121611224ac6426fce39899e3b5b31d206041f7ae8
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
sudo: false
|
2
|
+
language: ruby
|
3
|
+
rvm:
|
4
|
+
- 2.1
|
5
|
+
- 2.2
|
6
|
+
- 2.3.0
|
7
|
+
- 2.3.1
|
8
|
+
before_install:
|
9
|
+
- gem install bundler
|
10
|
+
gemfile:
|
11
|
+
- gemfiles/Gemfile.rails.5.0
|
12
|
+
- gemfiles/Gemfile.rails.4.2
|
13
|
+
- gemfiles/Gemfile.rails.4.1
|
14
|
+
- gemfiles/Gemfile.rails.4.0
|
15
|
+
script: "bundle exec rake spec"
|
16
|
+
|
17
|
+
matrix:
|
18
|
+
exclude:
|
19
|
+
- rvm: 2.1
|
20
|
+
gemfile: gemfiles/Gemfile.rails.5.0
|
21
|
+
- rvm: 2.2
|
22
|
+
gemfile: gemfiles/Gemfile.rails.5.0
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 fukumone
|
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,75 @@
|
|
1
|
+
# Rails::Log::Profiling
|
2
|
+
|
3
|
+

|
4
|
+
|
5
|
+
Rails専用のパフォーマンスツール
|
6
|
+
- クエリプロファイリング:クエリを降順、昇順に整理してログに出力
|
7
|
+
- Viewプロファイリング:view、レンダリングにかかったページを降順、昇順に整理してログに出力
|
8
|
+
- メソッドの実行箇所を表示
|
9
|
+
|
10
|
+
TODO:
|
11
|
+
- Beta1
|
12
|
+
- クエリプロファイリング機能追加
|
13
|
+
- Beta2
|
14
|
+
- Viewプロファイル機能追加
|
15
|
+
- メソッドの実行箇所検知機能追加
|
16
|
+
- Beta3
|
17
|
+
- Rails4.0、Rails4.1、Rails4.2、Rails5.0正式サポート
|
18
|
+
|
19
|
+
## Installation
|
20
|
+
|
21
|
+
- gemとしてインストール
|
22
|
+
|
23
|
+
```
|
24
|
+
$ gem install rails_log_profiling --pre
|
25
|
+
```
|
26
|
+
|
27
|
+
- gemfileに追加
|
28
|
+
|
29
|
+
```
|
30
|
+
gem 'rails_log_profiling', '~> 0.1.0.beta1', :group => "development"
|
31
|
+
```
|
32
|
+
|
33
|
+
## Usage
|
34
|
+
Gemをインストールした状態でRails Serverを起動
|
35
|
+
log/以下にログファイルが作られ、記録されます
|
36
|
+
|
37
|
+
```
|
38
|
+
$ tail -f log/rails_log_profiling.log
|
39
|
+
|
40
|
+
# output
|
41
|
+
PostsController#index
|
42
|
+
|
43
|
+
2件のクエリの検知
|
44
|
+
1: Post Load (1.4ms) SELECT `posts`.* FROM `posts`
|
45
|
+
|
46
|
+
2: Article Load (0.8ms) SELECT `articles`.* FROM `articles` ORDER BY `articles`.`id` ASC LIMIT 1
|
47
|
+
```
|
48
|
+
|
49
|
+
## Configuration
|
50
|
+
Rails::Log::Profilingは初期の段階ではなにも行いません、動作させるには
|
51
|
+
config/environments/development.rbに下記の設定を追加してください
|
52
|
+
|
53
|
+
```:devlopment.rb
|
54
|
+
Rails::Log::Profiling.enable = true
|
55
|
+
```
|
56
|
+
|
57
|
+
Rails::Log::Profilingはオプションが用意されています
|
58
|
+
- `Rails::Log::Profiling.enable`: true => Rails::Log::Profilingを有効にする
|
59
|
+
- `Rails::Log::Profiling.sort_order`: クエリの並び順を指定する、降順はdesc、昇順はascと設定してください。初期設定はdescになっています
|
60
|
+
|
61
|
+
## Inspire
|
62
|
+
- [rack-mini-profiler](https://github.com/MiniProfiler/rack-mini-profiler)
|
63
|
+
- [Activerecord::Cause](https://github.com/joker1007/activerecord-cause)
|
64
|
+
- [Bullet](https://github.com/flyerhzm/bullet)
|
65
|
+
|
66
|
+
## Contributing
|
67
|
+
|
68
|
+
1. Fork it
|
69
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
70
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
71
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
72
|
+
5. Create new Pull Request
|
73
|
+
|
74
|
+
## License
|
75
|
+
MIT
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "rails/log/profiling"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'action_controller'
|
2
|
+
|
3
|
+
module ActionController
|
4
|
+
class Base
|
5
|
+
include Rails::Log::Profiling
|
6
|
+
before_action :controller_action_message
|
7
|
+
after_action :logger_execute
|
8
|
+
|
9
|
+
private
|
10
|
+
def controller_action_message
|
11
|
+
controller_name = params[:controller].split("_").map(&:capitalize).join
|
12
|
+
Rails::Log::Profiling.logger.info("\n \033[36m #{controller_name}Controller##{params[:action]}")
|
13
|
+
end
|
14
|
+
|
15
|
+
def logger_execute
|
16
|
+
Rails::Log::Profiling.logger.info("\n \033[36m" + Rails::Log::Profiling.sqls.count.to_s + "件のクエリの検知")
|
17
|
+
Rails::Log::Profiling::QueryProfiling.execute
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Rails
|
2
|
+
module Log
|
3
|
+
module Profiling
|
4
|
+
class Logger
|
5
|
+
def self.run
|
6
|
+
require 'fileutils'
|
7
|
+
root_path = Rails.root.to_s
|
8
|
+
FileUtils.mkdir_p(root_path + '/log')
|
9
|
+
log_file = root_path + '/log/rails_profiling.log'
|
10
|
+
if File.exist?(log_file)
|
11
|
+
f = File.open(log_file, 'a')
|
12
|
+
else
|
13
|
+
f = File.open("#{root_path}/log/rails_profiling.log", 'a+')
|
14
|
+
end
|
15
|
+
f.binmode
|
16
|
+
f.sync = true
|
17
|
+
Rails::Log::Profiling.logger = ActiveSupport::Logger.new f
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require "active_record"
|
2
|
+
require "active_record/log_subscriber"
|
3
|
+
|
4
|
+
module Rails::Log::Profiling
|
5
|
+
class QueryLogSubscriber < ActiveRecord::LogSubscriber
|
6
|
+
def sql(event)
|
7
|
+
payload = event.payload
|
8
|
+
|
9
|
+
return if IGNORE_PAYLOAD_NAMES.include?(payload[:name])
|
10
|
+
|
11
|
+
if ActiveRecord.version >= Gem::Version.new("5.0.0.beta")
|
12
|
+
ar_ver_5(event)
|
13
|
+
else
|
14
|
+
ar_ver_4(event)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def ar_ver_5(event)
|
21
|
+
payload = event.payload
|
22
|
+
name = "#{payload[:name]} (#{event.duration.round(1)}ms)"
|
23
|
+
sql = payload[:sql]
|
24
|
+
binds = nil
|
25
|
+
|
26
|
+
unless (payload[:binds] || []).empty?
|
27
|
+
binds = " " + payload[:binds].map { |attr| render_bind(attr) }.inspect
|
28
|
+
end
|
29
|
+
|
30
|
+
name = colorize_payload_name(name, payload[:name])
|
31
|
+
sql = color(sql, sql_color(sql), true)
|
32
|
+
|
33
|
+
if !name.match(/.*ActiveRecord::SchemaMigration.*/) && name.match(/.*Load.\(.*ms\).*/)
|
34
|
+
Rails::Log::Profiling.sqls << [ "#{event.duration.round(1)}".to_f, " #{name} #{sql}#{binds}"]
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def ar_ver_4(event)
|
39
|
+
payload = event.payload
|
40
|
+
name = '%s (%.1fms)' % [payload[:name], event.duration]
|
41
|
+
sql = payload[:sql].squeeze(' ')
|
42
|
+
binds = nil
|
43
|
+
|
44
|
+
unless (payload[:binds] || []).empty?
|
45
|
+
binds = " " + payload[:binds].map { |col,v|
|
46
|
+
if col
|
47
|
+
[col.name, v]
|
48
|
+
else
|
49
|
+
[nil, v]
|
50
|
+
end
|
51
|
+
}.inspect
|
52
|
+
end
|
53
|
+
|
54
|
+
if odd?
|
55
|
+
name = color(name, CYAN, true)
|
56
|
+
sql = color(sql, nil, true)
|
57
|
+
else
|
58
|
+
name = color(name, MAGENTA, true)
|
59
|
+
end
|
60
|
+
|
61
|
+
if !name.match(/.*ActiveRecord::SchemaMigration.*/) && name.match(/.*Load.\(.*ms\).*/)
|
62
|
+
Rails::Log::Profiling.sqls << [ "#{event.duration.round(1)}".to_f, " #{name} #{sql}#{binds}" ]
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
ActiveSupport.on_load(:active_record) do
|
69
|
+
Rails::Log::Profiling::QueryLogSubscriber.attach_to :active_record
|
70
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Rails
|
2
|
+
module Log
|
3
|
+
module Profiling
|
4
|
+
class QueryProfiling
|
5
|
+
def self.execute
|
6
|
+
return if Rails::Log::Profiling.sqls.empty?
|
7
|
+
self.query_sort
|
8
|
+
Rails::Log::Profiling.sqls.each.with_index(1) do |val, ind|
|
9
|
+
Rails::Log::Profiling.logger.debug("\n \033[36m #{ind}:" + val[1]) # colorで出力
|
10
|
+
end
|
11
|
+
Rails::Log::Profiling.sqls.clear
|
12
|
+
end
|
13
|
+
|
14
|
+
protected
|
15
|
+
def self.query_sort
|
16
|
+
if Rails::Log::Profiling.sort_order == "desc"
|
17
|
+
Rails::Log::Profiling.sqls.sort! { |a, b| b[0] <=> a[0] }
|
18
|
+
else # sort_order asc
|
19
|
+
Rails::Log::Profiling.sqls.sort!
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require "rails/log/profiling/version"
|
2
|
+
|
3
|
+
module Rails
|
4
|
+
module Log
|
5
|
+
module Profiling
|
6
|
+
include ActiveSupport::Configurable
|
7
|
+
|
8
|
+
config_accessor :logger, instance_accessor: false do
|
9
|
+
""
|
10
|
+
end
|
11
|
+
|
12
|
+
config_accessor :sqls, instance_accessor: false do
|
13
|
+
[]
|
14
|
+
end
|
15
|
+
|
16
|
+
config_accessor :enable, instance_accessor: false do
|
17
|
+
false
|
18
|
+
end
|
19
|
+
|
20
|
+
config_accessor :sort_order, instance_accessor: false do
|
21
|
+
"desc"
|
22
|
+
end
|
23
|
+
|
24
|
+
if defined?(Rails)
|
25
|
+
class Railtie < ::Rails::Railtie
|
26
|
+
initializer "rails_log_profiling.configure_rails_initialization" do
|
27
|
+
if Rails::Log::Profiling.enable
|
28
|
+
require 'rails/log/profiling/logger'
|
29
|
+
require 'rails/log/profiling/query_profiling'
|
30
|
+
require 'rails/log/profiling/query_log_subscriber'
|
31
|
+
::Rails::Log::Profiling::Logger.run
|
32
|
+
require 'rails/log/profiling/action_controller'
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'rails/log/profiling/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "rails_log_profiling"
|
8
|
+
spec.version = Rails::Log::Profiling::VERSION
|
9
|
+
spec.authors = ["fukumone"]
|
10
|
+
spec.email = ["fukuimone@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Rails専用のログプロファイリング}
|
13
|
+
spec.description = %q{Rails専用のログプロファイリング}
|
14
|
+
spec.homepage = "https://github.com/fukumone/rails_log_profiling.git"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
# if spec.respond_to?(:metadata)
|
20
|
+
# spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
|
21
|
+
# else
|
22
|
+
# raise "RubyGems 2.0 or newer is required to protect against " \
|
23
|
+
# "public gem pushes."
|
24
|
+
# end
|
25
|
+
|
26
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
27
|
+
f.match(%r{^(test|spec|features)/})
|
28
|
+
end
|
29
|
+
spec.bindir = "exe"
|
30
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
31
|
+
spec.require_paths = ["lib"]
|
32
|
+
|
33
|
+
spec.add_development_dependency "bundler", "~> 1.13"
|
34
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
35
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
36
|
+
end
|
metadata
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails_log_profiling
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0.beta1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- fukumone
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-10-23 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.13'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.13'
|
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.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.0'
|
55
|
+
description: Rails専用のログプロファイリング
|
56
|
+
email:
|
57
|
+
- fukuimone@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/console
|
70
|
+
- bin/setup
|
71
|
+
- gemfiles/Gemfile.rails.4.0
|
72
|
+
- gemfiles/Gemfile.rails.4.1
|
73
|
+
- gemfiles/Gemfile.rails.4.2
|
74
|
+
- gemfiles/Gemfile.rails.5.0
|
75
|
+
- lib/rails/log/profiling/action_controller.rb
|
76
|
+
- lib/rails/log/profiling/logger.rb
|
77
|
+
- lib/rails/log/profiling/query_log_subscriber.rb
|
78
|
+
- lib/rails/log/profiling/query_profiling.rb
|
79
|
+
- lib/rails/log/profiling/version.rb
|
80
|
+
- lib/rails_log_profiling.rb
|
81
|
+
- rails_log_profiling.gemspec
|
82
|
+
homepage: https://github.com/fukumone/rails_log_profiling.git
|
83
|
+
licenses:
|
84
|
+
- MIT
|
85
|
+
metadata: {}
|
86
|
+
post_install_message:
|
87
|
+
rdoc_options: []
|
88
|
+
require_paths:
|
89
|
+
- lib
|
90
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - ">"
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: 1.3.1
|
100
|
+
requirements: []
|
101
|
+
rubyforge_project:
|
102
|
+
rubygems_version: 2.5.1
|
103
|
+
signing_key:
|
104
|
+
specification_version: 4
|
105
|
+
summary: Rails専用のログプロファイリング
|
106
|
+
test_files: []
|