Sha256: fbb0ec1556c5c4ff411acf02536347b7949c280b9a96fbe8bfe982acfaff8d7e

Contents?: true

Size: 1.34 KB

Versions: 1

Compression:

Stored size: 1.34 KB

Contents

module ZTK
  class Profiler

    # Profiler Core Functionality
    module Core
      require 'ztk/ui'

      @@start_time  ||= nil
      @@end_time    ||= nil
      @@timer_stack ||= Array.new

      def start
        reset

        true
      end

      def stop
        @@end_time ||= Time.now.utc

        true
      end

      def reset
        @@start_time  = Time.now.utc
        @@end_time    = nil
        @@timer_stack = Array.new
        Timer.reset

        true
      end

      def method_missing(method_name, *method_args)
        raise "You must supply a block to the profiler method #{method_name.inspect}!" unless block_given?

        @@start_time ||= Time.now.utc

        result = nil
        timer  = Timer.new(method_name, @@timer_stack.last)

        @@timer_stack.push(timer)
        timer.benchmark = ::Benchmark.realtime do
          result = yield
        end
        @@timer_stack.pop

        result
      end

      def total_time
        stop
        @@end_time - @@start_time
      end

      def report(options={})
        options = Base.build_config({}, options)

        stop

        results = Array.new

        report_timers(options)
        options.ui.stdout.puts
        results << report_timer_totals(options)
        options.ui.stdout.puts
        results << report_totals(options)

        results
      end

    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ztk-2.4.0 lib/ztk/profiler/core.rb