Permalink
Browse files

Only do benchmarking if log level matches and log caching

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2141 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
dhh committed Sep 6, 2005
1 parent 5e8e8d6 commit 291c667d6614ce28f85311f06ceb5f130b903be1
Showing with 31 additions and 20 deletions.
  1. +7 −3 actionpack/lib/action_controller/benchmarking.rb
  2. +24 −17 actionpack/lib/action_controller/caching.rb
@@ -19,11 +19,15 @@ def self.append_features(base)
module ClassMethods
# Log and benchmark the workings of a single block and silence whatever logging that may have happened inside it
# (unless <tt>use_silence</tt> is set to false).
- def benchmark(title, use_silence = true)
- if logger
+ #
+ # The benchmark is only recorded if the current level of the logger matches the <tt>log_level</tt>, which makes it
+ # easy to include benchmarking statements in production software that will remain inexpensive because the benchmark
+ # will only be conducted if the log level is low enough.
+ def benchmark(title, log_level = Logger::DEBUG, use_silence = true)
+ if logger && logger.level == log_level
result = nil
seconds = Benchmark.realtime { result = use_silence ? silence { yield } : yield }
- logger.info "#{title} (#{sprintf("%f", seconds)})"
+ logger.add(log_level, "#{title} (#{sprintf("%f", seconds)})")
result
else
yield
@@ -74,17 +74,21 @@ module ClassMethods
# expire_page "/lists/show"
def expire_page(path)
return unless perform_caching
- File.delete(page_cache_path(path)) if File.exists?(page_cache_path(path))
- logger.debug "Expired page: #{page_cache_file(path)}" unless logger.nil?
+
+ benchmark "Expired page: #{page_cache_file(path)}" do
+ File.delete(page_cache_path(path)) if File.exists?(page_cache_path(path))
+ end
end
# Manually cache the +content+ in the key determined by +path+. Example:
# cache_page "I'm the cached content", "/lists/show"
def cache_page(content, path)
return unless perform_caching
- FileUtils.makedirs(File.dirname(page_cache_path(path)))
- File.open(page_cache_path(path), "w+") { |f| f.write(content) }
- logger.debug "Cached page: #{page_cache_file(path)}" unless logger.nil?
+
+ benchmark "Cached page: #{page_cache_file(path)}" do
+ FileUtils.makedirs(File.dirname(page_cache_path(path)))
+ File.open(page_cache_path(path), "w+") { |f| f.write(content) }
+ end
end
# Caches the +actions+ using the page-caching approach that'll store the cache in a path within the page_cache_directory that
@@ -279,21 +283,22 @@ def write_fragment(name, content, options = {})
return unless perform_caching
key = fragment_cache_key(name)
- fragment_cache_store.write(key, content, options)
- logger.debug "Cached fragment: #{key}" unless logger.nil?
+ benchmark "Cached fragment: #{key}" do
+ fragment_cache_store.write(key, content, options)
+ end
+
content
end
def read_fragment(name, options = {})
return unless perform_caching
- key = fragment_cache_key(name)
- if cache = fragment_cache_store.read(key, options)
- logger.debug "Fragment hit: #{key}" unless logger.nil?
- cache
- else
- false
+ key, cache = fragment_cache_key(name), nil
+ benchmark "Fragment hit: #{key}" do
+ cache = fragment_cache_store.read(key, options)
end
+
+ cache || false
end
# Name can take one of three forms:
@@ -306,11 +311,13 @@ def expire_fragment(name, options = {})
key = fragment_cache_key(name)
if key.is_a?(Regexp)
- fragment_cache_store.delete_matched(key, options)
- logger.debug "Expired fragments matching: #{key.source}" unless logger.nil?
+ benchmark "Expired fragments matching: #{key.source}" do
+ fragment_cache_store.delete_matched(key, options)
+ end
else
- fragment_cache_store.delete(key, options)
- logger.debug "Expired fragment: #{key}" unless logger.nil?
+ benchmark "Expired fragment: #{key}" do
+ fragment_cache_store.delete(key, options)
+ end
end
end

0 comments on commit 291c667

Please sign in to comment.