Module: Cuboid::Support::Mixins::Profiler

Included in:
Cache::Base
Defined in:
lib/cuboid/support/mixins/profiler.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

@@on =
false

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.disable!Object



19
20
21
# File 'lib/cuboid/support/mixins/profiler.rb', line 19

def disable!
    @@on = false
end

.enable!Object



15
16
17
# File 'lib/cuboid/support/mixins/profiler.rb', line 15

def enable!
    @@on = true
end

.included(base) ⇒ Object



11
12
13
# File 'lib/cuboid/support/mixins/profiler.rb', line 11

def included( base )
    base.extend ClassMethods
end

.on?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/cuboid/support/mixins/profiler.rb', line 23

def on?
    @@on
end

.resultsObject



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/cuboid/support/mixins/profiler.rb', line 27

def results
    h = {}
    ObjectSpace.each_object( Class ) do |klass|
        next if !klass.included_modules.include? self
        next if klass.profile_data.empty?

        h[klass] = {
            sorted_total: klass.profile_data_total,
            sorted_avg:   klass.profile_data_avg
        }
    end
    h
end

Instance Method Details

#profile_proc(*args, &block) ⇒ Object



59
60
61
62
63
# File 'lib/cuboid/support/mixins/profiler.rb', line 59

def profile_proc( *args, &block )
    return block.call( *args ) if !Support::Mixins::Profiler.on?

    profile_wrap_proc( &block ).call *args
end

#profile_wrap_proc(&block) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/cuboid/support/mixins/profiler.rb', line 65

def profile_wrap_proc( &block )
    return block if !Support::Mixins::Profiler.on?

    proc do |*args|
        t = Time.now
        r = block.call( *args )

        loc = block.source_location.join( ':' )
        loc.gsub!( Options.paths.root, '' )

        data = self.class.profile_data[loc] ||= {
            total: 0,
            count: 0,
            avg:   0
        }

        data[:total] += Time.now - t
        data[:count] += 1
        data[:avg]    = data[:total] / data[:count]

        r
    end
end