diff --git a/Rakefile b/Rakefile index 7225f13..8fad89c 100644 --- a/Rakefile +++ b/Rakefile @@ -24,6 +24,7 @@ spec = Gem::Specification.new do |s| s.author = AUTHOR s.email = EMAIL s.homepage = HOMEPAGE + s.add_dependency('fiveruns_tuneup_core') s.add_dependency('merb-slices', '>= 0.9.5') s.require_path = 'lib' s.files = %w(LICENSE README Rakefile TODO) + Dir.glob("{lib,spec,app,public,stubs}/**/*") diff --git a/lib/fiveruns_tuneup_core.rb b/lib/fiveruns_tuneup_core.rb deleted file mode 100644 index 20e6a16..0000000 --- a/lib/fiveruns_tuneup_core.rb +++ /dev/null @@ -1,393 +0,0 @@ -require 'erb' - -module Fiveruns - - module Tuneup - - class CalculationError < ::RuntimeError; end - - class << self - attr_accessor :javascripts_path - attr_accessor :stylesheets_path - end - - def self.record(&block) - Step.reset! - root = RootStep.new - root.record(&block) - root - end - - def self.step(name, layer, extras = {}, &block) - trace = format_caller(caller) - Step.new(name, layer, extras.merge('Caller' => trace), nil).record(&block) - end - - def self.insert_panel(body, run) - return body unless run - tag = body[/(
]*>)/i, 1] - return body unless tag - panel = Panel.new(run) - body.sub(/<\/head>/i, head << '').sub(tag, tag + panel.to_html) - end - - def self.head - %( - - - ) - end - - def self.format_caller(trace) - valid_lines = trace.reject { |line| line =~ /fiveruns_tuneup/ }[0,5] - linked_lines = valid_lines.map { |line| editor_link_line(line) } - '%s' % linked_lines.join("\n") - end - - def self.strip_root(text) - pattern = /^#{Regexp.quote Merb.root}\/?/o - if text =~ pattern - result = text.sub(pattern, '') - in_app = result !~ /^gems\// - [in_app, result] - else - [false, text] - end - end - - # TODO: Refactor - def self.editor_link_line(line) - filename, number, extra = line.match(/^(.+?):(\d+)(?::in\b(.*?))?/)[1, 2] - in_app, line = strip_root(line) - name = if line.size > 87 - "…#{CGI.escapeHTML line.sub(/^.*?(.{84})$/, '\1')}" - else - line - end - name = if in_app - if name =~ /`/ - name.sub(/^(.*?)\s+`(.*?)'$/, %q(\1 `\2')) - else - %(#{name}) - end - else - name.sub(/([^\/\\]+\.\S+:\d+:in)\s+`(.*?)'$/, %q(\1 `\2')) - end - %(%s%s) % [CGI.escapeHTML(line), filename, number, name, extra] - end - - module Templating - - def h(text) - CGI.escapeHTML(text) - end - - def to_html - ERB.new(template).result(binding) - end - - end - - class RootStep - include Templating - attr_reader :children, :bar - attr_accessor :time, :parent - def initialize(time = nil) - @time = time - @children = [] - @bar = Bar.new(self) - end - - def root - parent ? parent.root : self - end - - def record - start = Time.now - result = Step.inside(self) { yield } - @time = Time.now - start - result - end - def disparity - result = time - children.inject(0) { |sum, child| sum + child.time } - if result < 0 - raise CalculationError, "Child steps exceed parent step size" - end - result - end - def add_child(child) - child.parent = self - children << child - end - def format_time(time) - '%.1fms' % (time * 1000) - end - def layer_portions - children.first.layer_portions - end - def to_json - {:children => children, :time => time}.to_json - end - - def proportion - time / root.time - end - - def template - %( -
- Other is the amount of time spent executing - code that TuneUp doesn't wrap to extract more information. - To reduce overhead and make the listing more - manageable, we don't generate steps for every operation. -
-#{layer_description}
- ) - } - end - - def layer_description - case layer - when :model - "In the model layer, this is probably ORM overhead (out of your control)." - when :view - "In the view layer, this is probably framework overhead during render (out of your control)." - when :controller - %( - In the controller layer, this is probably framework overhead during action execution (out of your control), - or time spent executing your code in the action (calls to private methods, libraries, etc). - ) - end - end - - end - - class Bar - include Templating - - attr_reader :step - def initialize(step) - @step = step - end - - private - - def template - %( - - ) - end - - def component(layer) - width = width_of(layer) - %( -