action_cost 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.1
1
+ 0.1.0
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "action_cost"
8
- s.version = "0.0.1"
8
+ s.version = "0.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Philippe Le Rohellec"]
12
- s.date = "2013-03-25"
12
+ s.date = "2013-03-31"
13
13
  s.description = "ActionCost measures the performance of a Rails 3 app controller actions in terms of number of calls to the database and to RecordCache."
14
14
  s.email = "philippe@lerohellec.com"
15
15
  s.extra_rdoc_files = [
@@ -32,7 +32,9 @@ Gem::Specification.new do |s|
32
32
  "config/routes.rb",
33
33
  "lib/action_cost.rb",
34
34
  "lib/action_cost/engine.rb",
35
+ "lib/action_cost/extensions/mysql2_adapter.rb",
35
36
  "lib/action_cost/extensions/postgresql_adapter.rb",
37
+ "lib/action_cost/extensions/sqlite_adapter.rb",
36
38
  "lib/action_cost/middleware.rb",
37
39
  "lib/action_cost/record_cache/index_hook.rb",
38
40
  "lib/action_cost/record_cache_parser.rb",
@@ -2,7 +2,6 @@
2
2
  <html>
3
3
  <head>
4
4
  <title>ActionCost Dashboard</title>
5
- <%= javascript_include_tag :defaults %>
6
5
  <%= csrf_meta_tag %>
7
6
 
8
7
  <style>
@@ -23,5 +22,8 @@
23
22
 
24
23
  <%= yield %>
25
24
 
25
+ <br>
26
+ <p><%= link_to 'Back to the application', '/' %></p>
27
+
26
28
  </body>
27
29
  </html>
@@ -14,8 +14,11 @@ module ActionCost
14
14
  end
15
15
  end
16
16
 
17
- initializer "action_cost:instrument_postgresql_adapter" do |app|
18
- require "#{lib_base_dir}/action_cost/extensions/postgresql_adapter"
17
+ initializer "action_cost:instrument_adapters" do |app|
18
+ db_adapter = ActiveRecord::Base.configurations[Rails.env]['adapter']
19
+ db_adapter = 'sqlite' if db_adapter =='sqlite3'
20
+
21
+ require "#{lib_base_dir}/action_cost/extensions/#{db_adapter}_adapter"
19
22
  end
20
23
 
21
24
  initializer "action_cost.add_middleware" do |app|
@@ -0,0 +1,14 @@
1
+ module ActiveRecord
2
+ module ConnectionAdapters # :nodoc:
3
+ class Mysql2Adapter < AbstractMysqlAdapter
4
+
5
+ def execute_with_action_cost(sql, name='')
6
+ #Rails.logger.debug "execute_with_action_cost: #{sql}"
7
+ parser = ActionCost::SqlParser.new(sql)
8
+ ActionCost::Middleware.push_sql_parser(parser)
9
+ execute_without_action_cost(sql, name)
10
+ end
11
+ alias_method_chain :execute, :action_cost
12
+ end
13
+ end
14
+ end
@@ -3,7 +3,7 @@ module ActiveRecord
3
3
  class PostgreSQLAdapter
4
4
 
5
5
  def execute_with_action_cost(sql, name='')
6
- Rails.logger.debug "execute_with_action_cost: #{sql}"
6
+ #Rails.logger.debug "execute_with_action_cost: #{sql}"
7
7
  parser = ActionCost::SqlParser.new(sql)
8
8
  ActionCost::Middleware.push_sql_parser(parser)
9
9
  execute_without_action_cost(sql, name)
@@ -12,7 +12,7 @@ module ActiveRecord
12
12
 
13
13
  if Rails.version =~ /^3.[12]\./
14
14
  def exec_query_with_action_cost(sql, name='', binds = [])
15
- Rails.logger.debug "exec_query_with_action_cost: #{sql}"
15
+ #Rails.logger.debug "exec_query_with_action_cost: #{sql}"
16
16
  parser = ActionCost::SqlParser.new(sql)
17
17
  ActionCost::Middleware.push_sql_parser(parser)
18
18
  exec_query_without_action_cost(sql, name, binds)
@@ -0,0 +1,24 @@
1
+ module ActiveRecord
2
+ module ConnectionAdapters # :nodoc:
3
+ class SQLiteAdapter
4
+
5
+ def execute_with_action_cost(sql, name='')
6
+ #Rails.logger.debug "execute_with_action_cost: #{sql}"
7
+ parser = ActionCost::SqlParser.new(sql)
8
+ ActionCost::Middleware.push_sql_parser(parser)
9
+ execute_without_action_cost(sql, name)
10
+ end
11
+ alias_method_chain :execute, :action_cost
12
+
13
+ if Rails.version =~ /^3.[12]\./
14
+ def exec_query_with_action_cost(sql, name='', binds = [])
15
+ #Rails.logger.debug "exec_query_with_action_cost: #{sql}"
16
+ parser = ActionCost::SqlParser.new(sql)
17
+ ActionCost::Middleware.push_sql_parser(parser)
18
+ exec_query_without_action_cost(sql, name, binds)
19
+ end
20
+ alias_method_chain :exec_query, :action_cost
21
+ end
22
+ end
23
+ end
24
+ end
@@ -3,22 +3,28 @@ module ActionCost
3
3
 
4
4
  attr_reader :controller_name, :action_name
5
5
  attr_reader :operation_stats, :table_stats, :join_stats
6
-
6
+
7
7
  def initialize(env)
8
- request = Rails.application.routes.recognize_path(env['REQUEST_URI'])
9
-
10
- @controller_name = request[:controller]
11
- @action_name = request[:action]
12
-
8
+ begin
9
+ routes_env = { :method => env['REQUEST_METHOD'] }
10
+ request = Rails.application.routes.recognize_path(env['REQUEST_URI'], routes_env)
11
+
12
+ @controller_name = request[:controller]
13
+ @action_name = request[:action]
14
+ rescue
15
+ @controller_name = nil
16
+ @action_name = nil
17
+ end
18
+
13
19
  @operation_stats = { :sql => {}, :rc => {} }
14
20
  ActionCost::SqlParser::VALID_OPERATIONS.each do |op|
15
21
  @operation_stats[:sql][op] = 0
16
22
  @operation_stats[:rc][op] = 0
17
23
  end
18
-
24
+
19
25
  @table_stats = { :sql => {}, :rc => {} }
20
26
  @join_stats = { :sql => {}, :rc => {} }
21
-
27
+
22
28
  action_cost_logfile = File.open(Rails.root.join("log", 'action_cost.log'), 'a')
23
29
  action_cost_logfile.sync = true
24
30
  @logger = Logger.new(action_cost_logfile)
@@ -47,14 +47,14 @@ module ActionCost
47
47
  private
48
48
 
49
49
  def parse_select
50
- if @sql =~ /from "?(\w+)"?\b/i
50
+ if @sql =~ /from ["`']?(\w+)["`']?\b/i
51
51
  @table_name = $1.downcase
52
52
  else
53
53
  @invalid = true
54
54
  return
55
55
  end
56
56
 
57
- @sql.scan(/join "?(\w+)"?\b/i) do |arr|
57
+ @sql.scan(/join ["`']?(\w+)["`']?\b/i) do |arr|
58
58
  arr.each do |t|
59
59
  @join_tables << t.downcase
60
60
  end
@@ -62,7 +62,7 @@ module ActionCost
62
62
  end
63
63
 
64
64
  def parse_insert
65
- if @sql =~ /insert into "?(\w+)"?\b/i
65
+ if @sql =~ /insert into ["`']?(\w+)["`']?\b/i
66
66
  @table_name = $1.downcase
67
67
  else
68
68
  @invalid = true
@@ -70,7 +70,7 @@ module ActionCost
70
70
  end
71
71
 
72
72
  def parse_update
73
- if @sql =~ /^update "?(\w+)"?\b/i
73
+ if @sql =~ /^update ["`']?(\w+)["`']?\b/i
74
74
  @table_name = $1.downcase
75
75
  else
76
76
  @invalid = true
@@ -78,7 +78,7 @@ module ActionCost
78
78
  end
79
79
 
80
80
  def parse_delete
81
- if @sql =~ /^delete from "?(\w+)"?\b/
81
+ if @sql =~ /^delete from ["`']?(\w+)["`']?\b/
82
82
  @table_name = $1.downcase
83
83
  else
84
84
  @invalid = true
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: action_cost
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-25 00:00:00.000000000 Z
12
+ date: 2013-03-31 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: shoulda
@@ -99,7 +99,9 @@ files:
99
99
  - config/routes.rb
100
100
  - lib/action_cost.rb
101
101
  - lib/action_cost/engine.rb
102
+ - lib/action_cost/extensions/mysql2_adapter.rb
102
103
  - lib/action_cost/extensions/postgresql_adapter.rb
104
+ - lib/action_cost/extensions/sqlite_adapter.rb
103
105
  - lib/action_cost/middleware.rb
104
106
  - lib/action_cost/record_cache/index_hook.rb
105
107
  - lib/action_cost/record_cache_parser.rb
@@ -123,7 +125,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
123
125
  version: '0'
124
126
  segments:
125
127
  - 0
126
- hash: 2458627961303877027
128
+ hash: 2346854853274766952
127
129
  required_rubygems_version: !ruby/object:Gem::Requirement
128
130
  none: false
129
131
  requirements: