Sha256: 507240b5643851b7b5e416309f51f50e15c276490b4906f7b08a444f4defb6b9

Contents?: true

Size: 1.36 KB

Versions: 21

Compression:

Stored size: 1.36 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: #{'%05.5f' % read_parse.to_f})
      to.puts %(  Time to convert document: #{'%05.5f' % convert.to_f})
      to.puts %(  Total time (read, parse and convert): #{'%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

21 entries across 21 versions & 2 rubygems

Version Path
asciidoctor-2.0.15 lib/asciidoctor/timings.rb
asciidoctor-2.0.14 lib/asciidoctor/timings.rb
asciidoctor-2.0.13 lib/asciidoctor/timings.rb
asciidoctor-2.0.12 lib/asciidoctor/timings.rb
asciidoctor-2.0.11 lib/asciidoctor/timings.rb
metanorma-cli-1.3.4 gems/ruby/2.6.0/gems/asciidoctor-2.0.10/lib/asciidoctor/timings.rb
metanorma-cli-1.3.3.1 gems/ruby/2.6.0/gems/asciidoctor-2.0.10/lib/asciidoctor/timings.rb
asciidoctor-2.0.10 lib/asciidoctor/timings.rb
asciidoctor-2.0.9 lib/asciidoctor/timings.rb
asciidoctor-2.0.8 lib/asciidoctor/timings.rb
asciidoctor-2.0.7 lib/asciidoctor/timings.rb
asciidoctor-2.0.6 lib/asciidoctor/timings.rb
asciidoctor-2.0.5 lib/asciidoctor/timings.rb
asciidoctor-2.0.4 lib/asciidoctor/timings.rb
asciidoctor-2.0.3 lib/asciidoctor/timings.rb
asciidoctor-2.0.2 lib/asciidoctor/timings.rb
asciidoctor-2.0.1 lib/asciidoctor/timings.rb
asciidoctor-2.0.0 lib/asciidoctor/timings.rb
asciidoctor-2.0.0.rc.3 lib/asciidoctor/timings.rb
asciidoctor-2.0.0.rc.2 lib/asciidoctor/timings.rb