Professional Web Applications Themes

code that has been reached - Ruby

Does there exists a tool for Ruby which records which parts of code (preferable the nodes in the AST) there has been reached? Eventually with some statistics on how many times each part has been executed? -- Simon Strandgaard...

  1. #1

    Default code that has been reached

    Does there exists a tool for Ruby which records which parts
    of code (preferable the nodes in the AST) there has been reached?

    Eventually with some statistics on how many times each part
    has been executed?

    --
    Simon Strandgaard
    Simon Guest

  2. #2

    Default Re: code that has been reached


    "Simon Strandgaard" <dk> schrieb im Newsbeitrag
    news:dk... 

    I don't know such a tool but two things come to mind:

    - You can use the profiler for timings (alternatively use "benchmark",
    which has less performance overhead).

    - You can use set_trace_function() to build the statistics.

    Regards

    robert

    Robert Guest

  3. #3

    Default Re: code that has been reached

    Simon Strandgaard wrote: 

    There is coverage.rb in RAA. It's very basic, but it works fine
    "out of the box" and has been useful to me.

    It doesn't measure stats, but it does show what code has been
    hit and what has not, and dumps it in ANSI.

    Hal



    Hal Guest

  4. #4

    Default Re: code that has been reached

    On Tue, 03 Feb 2004 09:14:01 +0900, Hal Fulton wrote: 
    >
    > There is coverage.rb in RAA. It's very basic, but it works fine
    > "out of the box" and has been useful to me.
    >
    > It doesn't measure stats, but it does show what code has been
    > hit and what has not, and dumps it in ANSI.[/ref]

    Wew, this is really a short piece of code.. amazing that it can be done in
    only 45 lines of ruby code!

    If I do "cat report.txt" then my terminal just scrolls for 10 seconds,
    which is much larger than what my terminal history can remember.
    If I pipe it into "less" then the coloring disappears. How do you view
    ANSI files?

    I think I will improve it (because I need it in conjunction with unittest)
    * Add counters to each line.
    * Fancy HTML output.

    Has this been done before?
    Other ideas?
    Eventually make a rubyforge project out of it?

    --
    Simon Strandgaard


    Simon Guest

  5. #5

    Default Re: code that has been reached

    Hi,

    At Wed, 4 Feb 2004 00:30:04 +0900,
    Simon Strandgaard wrote: 

    less -r

    --
    Nobu Nakada


    nobu.nokada@softhome.net Guest

  6. #6

    Default Re: code that has been reached

    On Wed, 04 Feb 2004 00:46:20 +0900, nobu.nokad wrote: 
    >
    > less -r[/ref]

    Thanks..

    I have done some improvements to the original 'coverage.rb', so that
    HTML is now generated. For an example have a look at:
    http://neoneye.dk/pr.rb.coverage.html

    As you may notice comments/arrays appears as if they are not executed!
    How can I fix this?

    --
    Simon Strandgaard

    module PrettyCoverage
    class HTML
    def initialize
    files = {}
    end
    def execute
    puts "execute"
    files.each{|file, line_marked|
    create_file(file, line_marked)
    }
    create_file_index
    end
    def mk_filename(name)
    name+".coverage.html"
    end
    def create_file_index
    output_filename = "index.html"
    rows = []
    files.each{|file, line_marked|
    filename = mk_filename(file)
    rows << "<tr><td><a href=\"#{filename}\">#{file}</a></td></tr>"
    }
    result = rows.join("\n")
    body = "<table>#{result}</table>"
    title = "coverage"
    css = <<EOCSS
    body {
    background-color: rgb(180, 180, 180);
    }
    div.marked {
    background-color: rgb(185, 200, 200);
    }
    div.overview {
    border-bottom: 8px solid black;
    }
    EOCSS
    html = <<EOHTML
    <html><head><title>#{title}</title>
    <style type="text/css">#{css}</style></head>
    <body>#{body}</body></html>
    EOHTML
    File.open(output_filename, "w+") do |f|
    f.puts html
    end
    end
    def add_file(file, line_marked)
    percent = calc_coverage(line_marked)
    printf("file=#{file} coverage=%02.1f%\n", percent)
    files[file] = line_marked
    end
    def calc_coverage(line_marked)
    marked = line_marked.transpose[1]
    n = marked.inject(0) {|r, i| (i) ? (r+1) : r }
    percent = n.to_f * 100 / marked.size
    end
    def format_overview(file, line_marked)
    percent = "%02.1f" % calc_coverage(line_marked)
    html = <<EOHTML
    <div class="overview">
    <table>
    <tr><td>filename</td><td><tt>#{file}</tt></td></tr>
    <tr><td>coverage</td><td>#{percent}</td></tr>
    </table>
    </div>
    EOHTML
    html
    end
    def format_lines(line_marked)
    result = ""
    last = nil
    end_of_div = ""
    format_line = "%#{line_marked.size.to_s.size}d"
    line_no = 1
    line_marked.each {|(line, marked)|
    if marked != last
    result += end_of_div
    case marked
    when true
    result += "<div class=\"marked\">"
    end_of_div = "</div>"
    when false
    end_of_div = ""
    end
    end
    result += (format_line % line_no) + " " + line + "\n"
    last = marked
    line_no += 1
    }
    result += end_of_div
    "<pre>#{result}</pre>"
    end
    def create_file(file, line_marked)
    output_filename = mk_filename(file)
    puts "outputting #{output_filename.inspect}"
    body =
    format_overview(file, line_marked) +
    format_lines(line_marked)
    title = file + " - coverage"
    css = <<EOCSS
    body {
    background-color: rgb(180, 180, 180);
    }
    div.marked {
    background-color: rgb(185, 200, 200);
    }
    div.overview {
    border-bottom: 8px solid black;
    }
    EOCSS
    html = <<EOHTML
    <html><head><title>#{title}</title>
    <style type="text/css">#{css}</style></head>
    <body>#{body}</body></html>
    EOHTML
    File.open(output_filename, "w+") do |f|
    f.puts html
    end
    end
    end
    end

    SCRIPT_LINES__ = {} unless defined? SCRIPT_LINES__

    module COVERAGE__
    COVER = {}
    def self.trace_func(event, file, line, id, binding, klass)
    case event
    when 'c-call', 'c-return', 'class'
    return
    end
    COVER[file] ||= []
    COVER[file][line] ||= 0
    COVER[file][line] += 1
    end

    END {
    set_trace_func(nil)
    printer = PrettyCoverage::HTML.new
    COVER.each do |file, lines|
    next if SCRIPT_LINES__.has_key?(file) == false
    lines = SCRIPT_LINES__[file]
    covers = COVER[file]
    line_status = []
    0.upto(lines.size - 1) do |c|
    line = lines[c].chomp
    marked = false
    if covers[c + 1]
    marked = true
    elsif /^\s*(?:begin\s*(?:#.*)?|ensure\s*(?:#.*)?|else\s*( ?:#.*)?)$/ =~ line and covers[c + 1 + 1]
    covers[c + 1] = covers[c + 1 + 1]
    marked = true
    elsif /^\s*(?:end|})\s*$/ =~ line && covers[c + 1 - 1]
    covers[c + 1] = covers[c + 1 - 1]
    marked = true
    end
    line_status << [line, marked]
    end
    printer.add_file(file, line_status)
    end
    printer.execute
    }

    set_trace_func(COVERAGE__.method(:trace_func).to_p roc)
    end
    Simon Guest

  7. #7

    Default Re: code that has been reached


    "Simon Strandgaard" <dk> schrieb im Newsbeitrag
    news:dk... [/ref][/ref]
    seconds, [/ref][/ref]
    view 
    > >
    > > less -r[/ref]
    >
    > Thanks..
    >
    > I have done some improvements to the original 'coverage.rb', so that
    > HTML is now generated. For an example have a look at:
    > http://neoneye.dk/pr.rb.coverage.html
    >
    > As you may notice comments/arrays appears as if they are not executed!
    > How can I fix this?[/ref]

    Dunno whether it's worth the effort. I guess, comments could be fixed by
    identifying regions of comment lines and marking them as reached if the
    line above and below is reached. For arrays you could try to find the
    matching "[" for a closing "]" and mark all lines in between if the line
    of the closing "]" is marked.

    Apart from that: nice output!

    Regards

    robert
     
    /^\s*(?:begin\s*(?:#.*)?|ensure\s*(?:#.*)?|else\s*( ?:#.*)?)$/ =~ line and
    covers[c + 1 + 1] 

    Robert Guest

  8. #8

    Default Re: code that has been reached

    On Wed, 04 Feb 2004 11:22:05 +0100, Robert Klemme wrote:
     [/ref]
    > seconds, [/ref]
    > view 
    >>
    >> Thanks..
    >>
    >> I have done some improvements to the original 'coverage.rb', so that
    >> HTML is now generated. For an example have a look at:
    >> http://neoneye.dk/pr.rb.coverage.html
    >>
    >> As you may notice comments/arrays appears as if they are not executed!
    >> How can I fix this?[/ref]
    >
    > Dunno whether it's worth the effort. I guess, comments could be fixed by
    > identifying regions of comment lines and marking them as reached if the
    > line above and below is reached. For arrays you could try to find the
    > matching "[" for a closing "]" and mark all lines in between if the line
    > of the closing "]" is marked.
    >
    > Apart from that: nice output![/ref]

    Ok.. I now propagate the marked status to comments.. output is much better
    now. Still Arrays/Hashes/=begin=end needs to be fixed.

    Try compare the new output agains the old:
    http://neoneye.dk/pr.rb.coverage.ver2.html

    Suggestions for improvements is welcome.. also patches is welcome.

    For the courius; try to execute the attached code by
    requiring it on the commandline.
    ruby -rcoverage helloworld.rb
    That should output a 'helloworld.rb.coverage.html' file.

    --
    Simon Strandgaard

    module PrettyCoverage
    class HTML
    def initialize
    files = {}
    end
    def execute
    puts "execute"
    create_file_index
    files.each{|file, line_marked|
    create_file(file, line_marked)
    }
    end
    def mk_filename(name)
    name+".coverage.html"
    end
    def create_file_index
    output_filename = "index.html"
    rows = []
    files.each{|file, line_marked|
    filename = mk_filename(file)
    rows << "<tr><td><a href=\"#{filename}\">#{file}</a></td></tr>"
    }
    result = rows.join("\n")
    body = "<table>#{result}</table>"
    title = "coverage"
    css = <<EOCSS
    body {
    background-color: rgb(180, 180, 180);
    }
    div.marked {
    background-color: rgb(185, 200, 200);
    }
    div.overview {
    border-bottom: 8px solid black;
    }
    EOCSS
    html = <<EOHTML
    <html><head><title>#{title}</title>
    <style type="text/css">#{css}</style></head>
    <body>#{body}</body></html>
    EOHTML
    File.open(output_filename, "w+") do |f|
    f.puts html
    end
    end
    def add_file(file, line_marked)
    percent = calc_coverage(line_marked)
    printf("file=#{file} coverage=%02.1f%\n", percent)

    # comments and empty lines.. we must
    # propagate marked-value backwards
    line_marked << ["", false]
    (line_marked.size).downto(1) do |index|
    line, marked = line_marked[index-1]
    next_line, next_marked = line_marked[index]
    if line =~ /^\s*(#|$)/ and marked == false
    marked = next_marked
    #line = "hest" + line
    line_marked[index-1] = [line, marked]
    end
    end
    line_marked.pop

    files[file] = line_marked
    end
    def calc_coverage(line_marked)
    marked = line_marked.transpose[1]
    n = marked.inject(0) {|r, i| (i) ? (r+1) : r }
    percent = n.to_f * 100 / marked.size
    end
    def format_overview(file, line_marked)
    percent = "%02.1f" % calc_coverage(line_marked)
    html = <<EOHTML
    <div class="overview">
    <table>
    <tr><td>filename</td><td><tt>#{file}</tt></td></tr>
    <tr><td>coverage</td><td>#{percent}</td></tr>
    </table>
    </div>
    EOHTML
    html
    end
    def format_lines(line_marked)
    result = ""
    last = nil
    end_of_div = ""
    format_line = "%#{line_marked.size.to_s.size}d"
    line_no = 1
    line_marked.each {|(line, marked)|
    if marked != last
    result += end_of_div
    case marked
    when true
    result += "<div class=\"marked\">"
    end_of_div = "</div>"
    when false
    end_of_div = ""
    end
    end
    result += (format_line % line_no) + " " + line + "\n"
    last = marked
    line_no += 1
    }
    result += end_of_div
    "<pre>#{result}</pre>"
    end
    def create_file(file, line_marked)
    output_filename = mk_filename(file)
    puts "outputting #{output_filename.inspect}"
    body =
    format_overview(file, line_marked) +
    format_lines(line_marked)
    title = file + " - coverage"
    css = <<EOCSS
    body {
    background-color: rgb(180, 180, 180);
    }
    div.marked {
    background-color: rgb(185, 200, 200);
    }
    div.overview {
    border-bottom: 8px solid black;
    }
    EOCSS
    html = <<EOHTML
    <html><head><title>#{title}</title>
    <style type="text/css">#{css}</style></head>
    <body>#{body}</body></html>
    EOHTML
    File.open(output_filename, "w+") do |f|
    f.puts html
    end
    end
    end
    end

    SCRIPT_LINES__ = {} unless defined? SCRIPT_LINES__

    module COVERAGE__
    COVER = {}
    def self.trace_func(event, file, line, id, binding, klass)
    case event
    when 'c-call', 'c-return', 'class'
    return
    end
    COVER[file] ||= []
    COVER[file][line] ||= 0
    COVER[file][line] += 1
    end

    END {
    set_trace_func(nil)
    printer = PrettyCoverage::HTML.new
    COVER.each do |file, lines|
    next if SCRIPT_LINES__.has_key?(file) == false
    lines = SCRIPT_LINES__[file]
    covers = COVER[file]
    line_status = []
    0.upto(lines.size - 1) do |c|
    line = lines[c].chomp
    marked = false
    if covers[c + 1]
    marked = true
    elsif /^\s*(?:begin\s*(?:#.*)?|ensure\s*(?:#.*)?|else\s*( ?:#.*)?)$/ =~ line and covers[c + 1 + 1]
    covers[c + 1] = covers[c + 1 + 1]
    marked = true
    elsif /^\s*(?:end|})\s*$/ =~ line && covers[c + 1 - 1]
    covers[c + 1] = covers[c + 1 - 1]
    marked = true
    end
    line_status << [line, marked]
    end
    printer.add_file(file, line_status)
    end
    printer.execute
    }

    set_trace_func(COVERAGE__.method(:trace_func).to_p roc)
    end




    Simon Guest

  9. #9

    Default Re: code that has been reached


    "Simon Strandgaard" <dk> schrieb im Newsbeitrag
    news:dk... 
    > > seconds, 
    > > view [/ref][/ref]
    executed! 
    > >
    > > Dunno whether it's worth the effort. I guess, comments could be fixed[/ref][/ref]
    by [/ref]
    the [/ref]
    line 
    >
    > Ok.. I now propagate the marked status to comments.. output is much[/ref]
    better 

    Apparently "end" lines are not reported so the comment is marked "not
    reached" in line 72. Maybe it's better to check the reached status of the
    last line before a comment region. This should also be easier to do.

    Regards

    robert
     
    /^\s*(?:begin\s*(?:#.*)?|ensure\s*(?:#.*)?|else\s*( ?:#.*)?)$/ =~ line and
    covers[c + 1 + 1] 

    Robert Guest

Similar Threads

  1. expanding adg tree until depth reached
    By jo_sh in forum Macromedia Flex General Discussion
    Replies: 0
    Last Post: April 29th, 09:54 PM
  2. Replies: 7
    Last Post: May 12th, 03:42 AM
  3. how to know print as reached to the client..
    By vinod roy via .NET 247 in forum ASP.NET Data Grid Control
    Replies: 1
    Last Post: June 12th, 09:38 PM
  4. Reached Fireworks Limit??
    By Deaf Web Designer webforumsuser@macromedia.com in forum Macromedia Fireworks
    Replies: 8
    Last Post: September 25th, 08:17 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139