Sha256: 06edf1beba5355b30107fbeb5675f0757618c1900145739f67caf0e61db0507e

Contents?: true

Size: 1.82 KB

Versions: 11

Compression:

Stored size: 1.82 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
        @@start_time  = Time.now.utc

        true
      end

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

        true
      end

      def reset
        @@start_time  = nil
        @@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
        exception = nil
        timer     = Timer.new(method_name, @@timer_stack.last)

        @@timer_stack.push(timer)
        timer.benchmark = ::Benchmark.realtime do
          begin
            result = yield
          rescue Exception => exception
          end
        end
        @@timer_stack.pop

        exception.nil? or raise exception

        result
      end

      def total_time
        if @@start_time.nil?
          raise ProfilerError, "You must start the profiler in order to calculate a total time!"
        else
          stop
          @@end_time - @@start_time
        end
      end

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

        stop

        if (Timer.count > 0)
          report_timers(options) and options.ui.stdout.puts
          results << report_timer_totals(options)
          results.last and options.ui.stdout.puts
          results << report_totals(options)
        else
          options.ui.stderr.puts("Nothing was profiled!")
          results = [ nil, nil ]
        end

        results
      end

    end

  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
ztk-3.3.2 lib/ztk/profiler/core.rb
ztk-3.3.1 lib/ztk/profiler/core.rb
ztk-3.3.0 lib/ztk/profiler/core.rb
ztk-3.2.6 lib/ztk/profiler/core.rb
ztk-3.2.5 lib/ztk/profiler/core.rb
ztk-3.2.4 lib/ztk/profiler/core.rb
ztk-3.2.3 lib/ztk/profiler/core.rb
ztk-3.2.2 lib/ztk/profiler/core.rb
ztk-3.2.1 lib/ztk/profiler/core.rb
ztk-3.2.0 lib/ztk/profiler/core.rb
ztk-3.1.0 lib/ztk/profiler/core.rb