Class: SemiStatic::Statistics

Inherits:
Object
  • Object
show all
Defined in:
lib/semi-static/statistics.rb

Overview

Used to track statistics while generating the Site.

Instance Method Summary collapse

Constructor Details

#initializeStatistics

Initialize a new Statistics object.



7
8
9
# File 'lib/semi-static/statistics.rb', line 7

def initialize
    self.reset
end

Instance Method Details

#displayObject

Display the collected data to the user.



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
# File 'lib/semi-static/statistics.rb', line 32

def display
    # details = {}
    @data.each do |category,items|
        next if category == :site
        sum = 0; items.values.each { |time| sum += time }
        list = items.sort { |l,r| l.last <=> r.last }
        
        if list.length > 1
            printf "%10s c:%-3d sum:%9.6f min:%.6f max:%.6f avg:%.6f\n",
                   category, items.length, sum, list.first.last,
                   list.last.last, sum / items.length
            # details[category] = list.reverse.first(5).collect { |pair| { pair.first => pair.last } }
        else
            printf "%10s c:%-3d sum:%9.6f\n", category, items.length, sum
        end
    end
    puts '---'
    @data[:site].each { |cat,time| printf "%15s %9.6f\n", cat.to_s.capitalize, time }
    
    # unless details.empty?
    #     puts '---'
    #     puts
    #     puts details.to_yaml
    # end
end

#record(category, item) ⇒ Object

Record the time it takes for the block to execute.

category: The category of the action. item: The name of the action.

Raises:

  • (ArgumentError)


22
23
24
25
26
27
28
# File 'lib/semi-static/statistics.rb', line 22

def record(category, item)
    raise ArgumentError unless block_given?
    before = Time.now
    result = yield
    @data[category][item] = Time.now - before
    return result
end

#resetObject

Clears all recorded data.



13
14
15
# File 'lib/semi-static/statistics.rb', line 13

def reset
    @data = Hash.new { |hash,key| hash[key] = Hash.new }
end