custom_benchmarks 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -2,13 +2,21 @@
2
2
 
3
3
  == About
4
4
 
5
- Custom Benchmarks allow you to easily add your own information to the
5
+ Custom Benchmarks allow you to easily add your own information to a
6
6
  benchmark line logged at the end of each request. e.g.,
7
7
 
8
- Completed in 0.40306 (2 reqs/sec) | Rendering: 0.05003 (12%) | DB: 0.04571 (11%) | Search: 0.16429,1 (40%) | PID: 22426 | 200 OK [http://www.zvents.com/welcome/index]
8
+ Finished JsonController#index in 0.40306 (2 reqs/sec) | Rendering: 0.05003 (12%) | DB: 0.04571 (11%) | Search: 0.16429,1 (40%) | Time: 1163542055 | PID: 22426 | 200 OK [http://www.zvents.com/welcome/index]
9
9
 
10
- Typically, the log line includes the latency associated with executing
11
- specific parts of a request. In the example above, we have added a
10
+ At present, this line appears in addition to the standard log line (described
11
+ below) that starts with the "Completed in". I was unable to replace the
12
+ existing log line without incurring other side effects. I believe the
13
+ problem is due to attempting to monkey patch a method (perform_action)
14
+ that has been aliased several times by the Rails core. Adding an extra
15
+ line to the log file is not optimal, but it seems to be the best choice
16
+ for now.
17
+
18
+ Typically, the standard log line includes the latency associated with
19
+ executing specific parts of a request. In the example above, we have added a
12
20
  measurement of search latency. But you can use Custom Benchmarks to add
13
21
  any information to the log line. The example above also shows the ID of
14
22
  the process (PID) that served this request. The PID is useful when parsing
@@ -75,24 +83,9 @@ ApplicationController:
75
83
 
76
84
  custom_benchmark {|runtime| MySearch.get_timing_summary(runtime) }
77
85
 
78
- == Bugs, Code and Contributing
79
-
80
- There's a RubyForge project set up at:
81
-
82
- http://rubyforge.org/projects/zventstools/
83
-
84
- Anonymous SVN access:
85
-
86
- $ svn checkout svn://rubyforge.org/var/svn/zventstools
87
-
88
- Author: Tyler Kovacs (tyler dot kovacs at gmail dot com)
86
+ == Default Integrations: Adapters
89
87
 
90
- == What's Next?
91
- * Default integrations: It would be great if the plugin shipped with a
92
- number of default integrations for logging things like acts_as_ferret
93
- and acts_as_solr, memcached latency and call count and activerecord SQL
94
- call count.
95
- * Log File Analyzer: One we have all this great data written to the log,
96
- how do we consume it? It makes sense to write an analyzer similar to
97
- the production_log_analyzer, but one that can adapt to arbitrary data
98
- written to the log (provided it fits a certain format).
88
+ custom_benchmarks now ships with default integrations with external
89
+ libraries. The first, and only, integration (as of 0.0.3 release) is
90
+ with MemCacheClient. Following the instructions in
91
+ vendor/plugins/custom_benchmarks/init.rb to enable adapters.
@@ -0,0 +1,33 @@
1
+ # Add this line to your ApplicationController (app/controllers/application.rb)
2
+ # to enable logging for memcache-client:
3
+ # custom_benchmark {|runtime| MemCache.cache_runtime(runtime) }
4
+
5
+ class MemCache
6
+ @@cache_latency = 0.0
7
+ @@cache_touches = 0
8
+
9
+ def self.reset_benchmarks
10
+ @@cache_latency = 0.0
11
+ @@cache_touches = 0
12
+ end
13
+
14
+ def self.get_benchmarks
15
+ [@@cache_latency, @@cache_touches]
16
+ end
17
+
18
+ def self.cache_runtime(runtime)
19
+ latency, touches = self.get_benchmarks
20
+ self.reset_benchmarks
21
+ " | memcache: #{sprintf("%.5f,%d",latency,touches)} (#{sprintf("%d", (latency * 100) / runtime)}%)"
22
+ end
23
+
24
+ def get_with_benchmark(key)
25
+ val = nil
26
+ @@cache_latency += Benchmark::measure{ val=get_without_benchmark(key) }.real
27
+ @@cache_touches += 1
28
+ val
29
+ end
30
+ alias_method :get_without_benchmark, :get
31
+ alias_method :get, :get_with_benchmark
32
+ end
33
+
@@ -92,15 +92,18 @@ module ActionController #:nodoc:
92
92
 
93
93
  def perform_action_with_custom_benchmark
94
94
  unless logger
95
- perform_action_without_benchmark
95
+ perform_action_without_custom_benchmark
96
96
  else
97
- runtime = [Benchmark::measure{ perform_action_without_benchmark }.real, 0.0001].max
98
- log_message = "Completed in #{sprintf("%.5f", runtime)} (#{(1 / runtime).floor} reqs/sec)"
97
+ runtime = [Benchmark::measure{ perform_action_without_custom_benchmark }.real, 0.0001].max
98
+ log_message = "Finished #{controller_class_name}\##{action_name} in #{sprintf("%.5f", runtime)} (#{(1 / runtime).floor} reqs/sec)"
99
+ if Object.const_defined?("ActiveRecord") && ActiveRecord::Base.connected?
100
+ log_message << active_record_runtime(runtime)
101
+ end
99
102
  log_message << rendering_runtime(runtime) if @rendering_runtime
100
- log_message << active_record_runtime(runtime) if Object.const_defined?("ActiveRecord") && ActiveRecord::Base.connected?
101
103
  self.class.custom_benchmarks.each do |benchmark|
102
104
  log_message << benchmark.call(runtime)
103
105
  end
106
+ log_message << " | Time: #{Time.now.to_i}"
104
107
  log_message << " | #{headers["Status"]}"
105
108
  log_message << " [#{complete_request_uri rescue "unknown"}]"
106
109
  logger.info(log_message)
@@ -108,3 +111,56 @@ module ActionController #:nodoc:
108
111
  end
109
112
  end
110
113
  end
114
+
115
+ module ActiveRecord
116
+ module ConnectionAdapters # :nodoc:
117
+ class AbstractAdapter
118
+ def initialize(connection, logger = nil) #:nodoc:
119
+ @connection, @logger = connection, logger
120
+ @runtime = 0
121
+ @total_runtime = 0
122
+ @last_verification = 0
123
+ end
124
+
125
+ def reset_runtime(reset=false) #:nodoc:
126
+ if reset
127
+ rt, @runtime, @total_runtime = @total_runtime, 0, 0
128
+ else
129
+ rt, @runtime = @runtime, 0, 0
130
+ end
131
+
132
+ rt
133
+ end
134
+
135
+ protected
136
+ def log(sql, name)
137
+ if block_given?
138
+ if @logger and @logger.level <= Logger::INFO
139
+ result = nil
140
+ seconds = Benchmark.realtime { result = yield }
141
+ @runtime += seconds
142
+ @total_runtime += seconds
143
+ log_info(sql, name, seconds)
144
+ result
145
+ else
146
+ seconds = Benchmark.realtime { result = yield }
147
+ @runtime += seconds
148
+ @total_runtime += seconds
149
+ result
150
+ end
151
+ else
152
+ log_info(sql, name, 0)
153
+ nil
154
+ end
155
+ rescue Exception => e
156
+ # Log message and raise exception.
157
+ # Set last_verfication to 0, so that connection gets verified
158
+ # upon reentering the request loop
159
+ @last_verification = 0
160
+ message = "#{e.class.name}: #{e.message}: #{sql}"
161
+ log_info(message, name, 0)
162
+ raise ActiveRecord::StatementInvalid, message
163
+ end
164
+ end
165
+ end
166
+ end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.10
3
3
  specification_version: 1
4
4
  name: custom_benchmarks
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.0.2
7
- date: 2006-11-07
6
+ version: 0.0.3
7
+ date: 2006-11-14
8
8
  summary: Easily allows custom information to be included in the benchmark log line at the end of each request.
9
9
  require_paths:
10
10
  - lib
@@ -27,6 +27,8 @@ authors:
27
27
  - Tyler Kovacs
28
28
  files:
29
29
  - lib/custom_benchmarks.rb
30
+ - lib/adapters
31
+ - lib/adapters/memcache-client.rb
30
32
  - README
31
33
  test_files: []
32
34