quake-db_profiling 0.0.3
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.
- data/README +21 -0
- data/lib/db_profiling/db_profiling.rb +58 -0
- data/lib/db_profiling/db_profiling_helper.rb +21 -0
- data/lib/db_profiling.rb +4 -0
- metadata +56 -0
data/README
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
= DB Profiling
|
2
|
+
|
3
|
+
A simple database query profiling tool, basis of Aaron Patterson's blog:
|
4
|
+
http://tenderlovemaking.com/2008/03/13/profiling-database-queries-in-rails/
|
5
|
+
Honor to him, bug to me
|
6
|
+
|
7
|
+
== Installation
|
8
|
+
|
9
|
+
The recommended way is that you get the gem:
|
10
|
+
|
11
|
+
gem install quake-db_profiling --source http://gems.github.com/
|
12
|
+
|
13
|
+
== Example usage
|
14
|
+
|
15
|
+
add one line to environment.rb:
|
16
|
+
|
17
|
+
require 'db_profiling' if RAILS_ENV == "development"
|
18
|
+
|
19
|
+
add helper output to the view which you want to profiling, for example, layouts/application.rhtml:
|
20
|
+
|
21
|
+
<%= db_profiling_view if RAILS_ENV == "development" %>
|
@@ -0,0 +1,58 @@
|
|
1
|
+
ActiveRecord::ConnectionAdapters::QueryCache.module_eval do
|
2
|
+
@@stats_hits = []
|
3
|
+
@@stats_misses = []
|
4
|
+
@@stats_bytes = @@stats_rows = 0
|
5
|
+
|
6
|
+
def self.get_stats
|
7
|
+
{ :hits => @@stats_hits,
|
8
|
+
:misses => @@stats_misses,
|
9
|
+
:rows => @@stats_rows,
|
10
|
+
:bytes => @@stats_bytes,
|
11
|
+
:queries => @@stats_hits.size + @@stats_misses.size,
|
12
|
+
}
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.reset_stats
|
16
|
+
@@stats_hits = []
|
17
|
+
@@stats_misses = []
|
18
|
+
@@stats_bytes = @@stats_rows = 0
|
19
|
+
end
|
20
|
+
|
21
|
+
def cache_sql_with_stats(sql, &block)
|
22
|
+
if @query_cache.has_key?(sql)
|
23
|
+
@@stats_hits << [sql, extract_app_caller]
|
24
|
+
else
|
25
|
+
@@stats_misses << [sql, extract_app_caller]
|
26
|
+
end
|
27
|
+
bytes = 0
|
28
|
+
rows = cache_sql_without_stats(sql, &block)
|
29
|
+
rows.each do |row|
|
30
|
+
row.each do |key, value|
|
31
|
+
bytes += key.length
|
32
|
+
bytes += value.length if value
|
33
|
+
end
|
34
|
+
end
|
35
|
+
@@stats_rows += rows.length
|
36
|
+
@@stats_bytes += bytes
|
37
|
+
rows
|
38
|
+
end
|
39
|
+
alias_method_chain :cache_sql, :stats
|
40
|
+
|
41
|
+
def extract_app_caller
|
42
|
+
caller.select {|s| s.index("#{RAILS_ROOT}/app") == 0}
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
ActionController::Base.module_eval do
|
47
|
+
def perform_action_with_reset
|
48
|
+
ActiveRecord::ConnectionAdapters::QueryCache::reset_stats
|
49
|
+
perform_action_without_reset
|
50
|
+
end
|
51
|
+
|
52
|
+
alias_method_chain :perform_action, :reset
|
53
|
+
|
54
|
+
def active_record_runtime(runtime)
|
55
|
+
stats = ActiveRecord::ConnectionAdapters::QueryCache::get_stats
|
56
|
+
"#{super} #{sprintf("%.1fk", stats[:bytes].to_f / 1024)} queries: #{stats[:queries]}"
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module JavaEye
|
2
|
+
module DbProfiling
|
3
|
+
module Helper
|
4
|
+
def db_profiling_view
|
5
|
+
stats = ActiveRecord::ConnectionAdapters::QueryCache.get_stats
|
6
|
+
<<HTML
|
7
|
+
<div id="db_profiling" style="background:pink;color:black;position:absolute;top:16px;left:25%;padding:0.5em;border: 2px solid red;z-index:999;text-align:left;">
|
8
|
+
<strong>
|
9
|
+
Queries: <a href="#" onclick="$('cache_hits').toggle();return false;" title="Query cache hits">#{stats[:hits].size}</a> / <a href="#" onclick="$('cache_misses').toggle();return false;" title="DB queries">#{stats[:misses].size}</a> / #{number_to_percentage((stats[:hits].size.to_f / (stats[:queries])) * 100, :precision => 0)} |
|
10
|
+
Rows: #{stats[:rows]} |
|
11
|
+
Transfer: #{sprintf("%.1fk", stats[:bytes].to_f / 1024)} |
|
12
|
+
<a href="#" onclick="$('db_profiling').remove();return false;" title="Close">[X]</a>
|
13
|
+
</strong>
|
14
|
+
<ul id="cache_hits" style="text-align:left;display:none;">#{stats[:hits].inject("") {|output, s| output << "<li title='Click to display detail' onclick='$(this).next().toggle();return false;' style='cursor: pointer'>#{s[0]}</li><li style='display:none;list-style:none;'>#{s[1].join('<br/>')}</li>" }}</ul>
|
15
|
+
<ul id="cache_misses" style="text-align:left;display:none;">#{stats[:misses].inject("") {|output, s| output << "<li title='Click to display detail' onclick='$(this).next().toggle();return false;' style='cursor: pointer'>#{s[0]}</li><li style='display:none;list-style:none;'>#{s[1].join('<br/>')}</li>" }}</ul>
|
16
|
+
</div>
|
17
|
+
HTML
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/lib/db_profiling.rb
ADDED
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: quake-db_profiling
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Quake Wang
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-06-11 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: quake.wang@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README
|
24
|
+
files:
|
25
|
+
- lib/db_profiling.rb
|
26
|
+
- lib/db_profiling/db_profiling.rb
|
27
|
+
- lib/db_profiling/db_profiling_helper.rb
|
28
|
+
- README
|
29
|
+
has_rdoc: false
|
30
|
+
homepage: https://github.com/quake/db_profiling/wikis
|
31
|
+
post_install_message:
|
32
|
+
rdoc_options: []
|
33
|
+
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: "0"
|
41
|
+
version:
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: "0"
|
47
|
+
version:
|
48
|
+
requirements: []
|
49
|
+
|
50
|
+
rubyforge_project:
|
51
|
+
rubygems_version: 1.0.1
|
52
|
+
signing_key:
|
53
|
+
specification_version: 2
|
54
|
+
summary: simple database query profiling tool
|
55
|
+
test_files: []
|
56
|
+
|