Sha256: 0793bfb3574ab701d73654b8b9bfec20f19f730ba2f3b8a1340a8109f58e8591

Contents?: true

Size: 1.82 KB

Versions: 34

Compression:

Stored size: 1.82 KB

Contents

module Cuboid
module Support
module Mixins

module Profiler

    @@on = false

    class <<self

        def included( base )
            base.extend ClassMethods
        end

        def enable!
            @@on = true
        end

        def disable!
            @@on = false
        end

        def on?
            @@on
        end

        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

    end

    module ClassMethods

        def profile_data
            @profile ||= {}
        end

        def profile_data_avg
            ::Hash[self.profile_data.sort_by { |_, d| d[:avg] }.reverse]
        end

        def profile_data_total
            ::Hash[self.profile_data.sort_by { |_, d| d[:total] }.reverse]
        end

    end

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

        profile_wrap_proc( &block ).call *args
    end

    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

end

end
end
end

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
cuboid-0.2.13 lib/cuboid/support/mixins/profiler.rb
cuboid-0.2.12 lib/cuboid/support/mixins/profiler.rb
cuboid-0.2.11 lib/cuboid/support/mixins/profiler.rb
cuboid-0.2.10 lib/cuboid/support/mixins/profiler.rb
cuboid-0.2.9 lib/cuboid/support/mixins/profiler.rb
cuboid-0.2.8 lib/cuboid/support/mixins/profiler.rb
cuboid-0.2.7 lib/cuboid/support/mixins/profiler.rb
cuboid-0.2.6 lib/cuboid/support/mixins/profiler.rb
cuboid-0.2.5 lib/cuboid/support/mixins/profiler.rb
cuboid-0.2.4.2 lib/cuboid/support/mixins/profiler.rb
cuboid-0.2.4.1 lib/cuboid/support/mixins/profiler.rb
cuboid-0.2.4 lib/cuboid/support/mixins/profiler.rb
cuboid-0.2.3 lib/cuboid/support/mixins/profiler.rb
cuboid-0.2.2 lib/cuboid/support/mixins/profiler.rb
cuboid-0.2.1 lib/cuboid/support/mixins/profiler.rb
cuboid-0.2 lib/cuboid/support/mixins/profiler.rb
cuboid-0.1.9.1 lib/cuboid/support/mixins/profiler.rb
cuboid-0.1.9 lib/cuboid/support/mixins/profiler.rb
cuboid-0.1.8 lib/cuboid/support/mixins/profiler.rb
cuboid-0.1.7 lib/cuboid/support/mixins/profiler.rb