=begin = apache/ruby-profile.rb Copyright (C) 2005 Shugo Maeda All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. == Overview Apache::RubyProfile profiles Ruby scripts. == Example of httpd.conf RubyRequire apache/ruby-profile SetHandler ruby-object RubyHandler Apache::RubyProfile.instance =end require "prof" require "apache/ruby-run" begin open("/proc/cpuinfo") do |f| f.each_line do |line| s = line.slice(/cpu MHz\s*:\s*(.*)/, 1) if s Prof.cpu_frequency = s.to_f * 1000000 break end end end rescue Errno::ENOENT end module Apache class RubyProfile < RubyRun def handler(r) status = check_request(r) return status if status != OK filename = setup(r) case ENV["RUBY_PROF_CLOCK_MODE"] when "gettimeofday" Prof.clock_mode = Prof::GETTIMEOFDAY when "cpu" Prof.clock_mode = Prof::CPU else Prof.clock_mode = Prof::CLOCK end Prof.start load(filename, true) result = Prof.stop append_profile_result(r, result) return OK end def append_profile_result(r, result) total = result.detect { |i| i.method_class.nil? && i.method_id == :"#toplevel" }.total_time if total == 0.0 total = 0.001 end sum = 0 buf = "\n" buf.concat("") buf.concat("") buf.concat("") buf.concat("") buf.concat("") buf.concat("") buf.concat("") buf.concat("") buf.concat("\n") for i in result sum += i.self_time if i.method_class.nil? name = i.method_id.to_s elsif i.method_class.is_a?(Class) name = i.method_class.to_s + "#" + i.method_id.to_s else name = i.method_class.to_s + "." + i.method_id.to_s end buf.concat("") buf.concat(format("", i.self_time / total * 100)) buf.concat(format("", sum)) buf.concat(format("", i.self_time)) buf.concat(format("", i.count)) buf.concat(format("", i.self_time * 1000 / i.count)) buf.concat(format("", i.total_time * 1000 / i.count)) buf.concat(format("", r.escape_html(name))) buf.concat("\n") end buf.concat("
% timecumulative secondsself secondscallsself ms/calltotal ms/callname
%.4f%.4f%.4f%d%.4f%.4f%s
\n") if !r.output_buffer.gsub!(/<\/body>/i, buf) r.output_buffer.concat(buf) end r.headers_out["Content-Length"] = r.output_buffer.length.to_s end end end