Sha256: 5cd191342ac5e1bc788db8ec59c57038f4750cd725ed2782761d2721e075e4fa

Contents?: true

Size: 1.5 KB

Versions: 2

Compression:

Stored size: 1.5 KB

Contents

module SemiStatic
    class Statistics
        def initialize
            self.reset
        end
        
        def reset
            @data = Hash.new { |hash,key| hash[key] = Hash.new }
        end
        
        def record(category, item)
            raise ArgumentError unless block_given?
            before = Time.now
            result = yield
            @data[category][item] = Time.now - before
            return result
        end
        
        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
    end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
zzot-semi-static-0.0.2 lib/semi-static/statistics.rb
zzot-zzot-semi-static-0.0.1 lib/semi-static/statistics.rb