Class: Piggly::Profile

Inherits:
Object show all
Defined in:
lib/piggly/profile.rb

Overview

Collection of all Tags

Instance Method Summary collapse

Constructor Details

#initializeProfile

Returns a new instance of Profile.



8
9
10
11
12
# File 'lib/piggly/profile.rb', line 8

def initialize
  @by_id        = {}
  @by_cache     = {}
  @by_procedure = {}
end

Instance Method Details

#[](object) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/piggly/profile.rb', line 21

def [](object)
  case object
  when String
    @by_id[object] or
      raise "No tag with id #{object}"
  when Dumper::ReifiedProcedure,
       Dumper::SkeletonProcedure
    @by_procedure[object.oid] or
      raise "No tags for procedure #{object.signature}"
  end
end

#add(procedure, tags, cache = nil) ⇒ Object

Register a procedure and its list of tags



15
16
17
18
19
# File 'lib/piggly/profile.rb', line 15

def add(procedure, tags, cache = nil)
  tags.each{|t| @by_id[t.id] = t }
  @by_cache[cache] = tags unless cache.nil?
  @by_procedure[procedure.oid] = tags
end

#clearObject

Resets each tag’s coverage stats



64
65
66
# File 'lib/piggly/profile.rb', line 64

def clear
  @by_id.values.each{|x| x.clear }
end

#difference(procedure, tags) ⇒ String

Returns:

  • (String)


78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/piggly/profile.rb', line 78

def difference(procedure, tags)
  current  = Util::Enumerable.group_by(@by_procedure[procedure.oid]){|x| x.type }
  previous = Util::Enumerable.group_by(tags){|x| x.type }

  current.default  = []
  previous.default = []

  (current.keys | previous.keys).map do |type|
    pct = Util::Enumerable.sum(current[type]){|x| x.to_f } / current[type].size -
          Util::Enumerable.sum(previous[type]){|x| x.to_f } / previous[type].size

    "#{"%+0.1f" % pct}% #{type}"
  end.join(", ")
end

#empty?(tags) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/piggly/profile.rb', line 73

def empty?(tags)
  tags.all?{|t| t.to_f.zero? }
end

#notice_processor(config, stderr = $stderr) ⇒ Object

Build a notice processor function that records each tag execution

@return [Proc]


95
96
97
98
99
100
101
102
103
104
105
# File 'lib/piggly/profile.rb', line 95

def notice_processor(config, stderr = $stderr)
  pattern = /#{config.trace_prefix} (#{Tags::AbstractTag::PATTERN})(?: (.))?/

  lambda do |message|
    if m = pattern.match(message)
      ping(m.captures[0], m.captures[1])
    else
      stderr.puts("unknown trace: #{message}")
    end
  end
end

#ping(tag_id, value = nil) ⇒ Object

Record the execution of a coverage tag



34
35
36
# File 'lib/piggly/profile.rb', line 34

def ping(tag_id, value=nil)
  self[tag_id].ping(value)
end

#storeObject

Write coverage stats to the disk cache



69
70
71
# File 'lib/piggly/profile.rb', line 69

def store
  @by_cache.each{|cache, tags| cache[:tags] = tags }
end

#summary(procedure = nil) ⇒ Object

Summarizes coverage for each type of tag (branch, block, loop)

@return [Hash<Symbol, Hash[:count => Integer, :percent => Float]>]


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/piggly/profile.rb', line 40

def summary(procedure = nil)
  tags =
    if procedure
      if @by_procedure.include?(procedure.oid)
        @by_procedure[procedure.oid]
      else
        []
      end
    else
      @by_id.values
    end

  grouped = Util::Enumerable.group_by(tags){|x| x.type }

  summary = Hash.new{|h,k| h[k] = Hash.new }
  grouped.each do |type, ts|
    summary[type][:count]   = ts.size
    summary[type][:percent] = Util::Enumerable.sum(ts){|x| x.to_f } / ts.size
  end

  summary
end