Sha256: 0ece9101f444896b3541b5769c122ba863c2c9eaab25d9e8fbe6d32e4589f04e

Contents?: true

Size: 1.38 KB

Versions: 7

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true
module Asciidoctor
  class Timings
    def initialize
      @log = {}
      @timers = {}
    end

    def start key
      @timers[key] = now
    end

    def record key
      @log[key] = (now - (@timers.delete key))
    end

    def time *keys
      time = keys.reduce(0) {|sum, key| sum + (@log[key] || 0) }
      time > 0 ? time : nil
    end

    def read
      time :read
    end

    def parse
      time :parse
    end

    def read_parse
      time :read, :parse
    end

    def convert
      time :convert
    end

    def read_parse_convert
      time :read, :parse, :convert
    end

    def write
      time :write
    end

    def total
      time :read, :parse, :convert, :write
    end

    def print_report to = $stdout, subject = nil
      to.puts %(Input file: #{subject}) if subject
      to.puts %(  Time to read and parse source: #{sprintf '%05.5f', read_parse.to_f})
      to.puts %(  Time to convert document: #{sprintf '%05.5f', convert.to_f})
      to.puts %(  Total time (read, parse and convert): #{sprintf '%05.5f', read_parse_convert.to_f})
    end

    private

    if (::Process.const_defined? :CLOCK_MONOTONIC, false) && (defined? ::Process.clock_gettime) == 'method'
      CLOCK_ID = ::Process::CLOCK_MONOTONIC
      def now
        ::Process.clock_gettime CLOCK_ID
      end
    else
      def now
        ::Time.now
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
asciidoctor-2.0.22 lib/asciidoctor/timings.rb
asciidoctor-2.0.21 lib/asciidoctor/timings.rb
asciidoctor-2.0.20 lib/asciidoctor/timings.rb
asciidoctor-2.0.19 lib/asciidoctor/timings.rb
asciidoctor-2.0.18 lib/asciidoctor/timings.rb
asciidoctor-2.0.17 lib/asciidoctor/timings.rb
asciidoctor-2.0.16 lib/asciidoctor/timings.rb