Sha256: f476ceaa58639ebe19996edc6b7f9469e83d5a5cfa9491837628d5f32e3a8776

Contents?: true

Size: 1.95 KB

Versions: 5

Compression:

Stored size: 1.95 KB

Contents

################################################################################
#
#      Author: Zachary Patten <zachary@jovelabs.net>
#   Copyright: Copyright (c) Jove Labs
#     License: Apache License, Version 2.0
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#
################################################################################
require 'benchmark'

module ZTK

  # ZTK::Benchmark Error Class
  #
  # @author Zachary Patten <zachary@jovelabs.net>
  class BenchmarkError < Error; end

  # Benchmark Class
  #
  # @author Zachary Patten <zachary@jovelabs.net>
  class Benchmark

    class << self

      def bench(options={}, &block)
        !block_given? and raise BenchmarkError, "You must supply a block!"

        options = { :stdout => STDOUT, :logger => $logger, :message => nil, :mark => nil }.merge(options)

        stdout = options[:stdout]
        logger = options[:logger]
        message = options[:message]
        mark = options[:mark]

        logger and logger.debug { options.inspect }

        (!message.nil? && !mark.nil?) and stdout.print("#{message} ")
        benchmark = ::Benchmark.realtime do
          if message.nil?
            yield
          else
            ZTK::Spinner.spin do
              yield
            end
          end
        end

        (!message.nil? && !mark.nil?) and stdout.print("#{mark}\n" % benchmark)
        logger and logger.info { "#{message} #{mark}" }

        benchmark
      end

    end

  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ztk-0.2.4 lib/ztk/benchmark.rb
ztk-0.2.3 lib/ztk/benchmark.rb
ztk-0.2.2 lib/ztk/benchmark.rb
ztk-0.2.1 lib/ztk/benchmark.rb
ztk-0.2.0 lib/ztk/benchmark.rb