"Fossies" - the Fresh Open Source Software Archive

Member "asciidoctor-2.0.10/lib/asciidoctor/timings.rb" (1 Jun 2019, 1388 Bytes) of package /linux/www/asciidoctor-2.0.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Ruby source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 # frozen_string_literal: true
    2 module Asciidoctor
    3   class Timings
    4     def initialize
    5       @log = {}
    6       @timers = {}
    7     end
    8 
    9     def start key
   10       @timers[key] = now
   11     end
   12 
   13     def record key
   14       @log[key] = (now - (@timers.delete key))
   15     end
   16 
   17     def time *keys
   18       time = keys.reduce(0) {|sum, key| sum + (@log[key] || 0) }
   19       time > 0 ? time : nil
   20     end
   21 
   22     def read
   23       time :read
   24     end
   25 
   26     def parse
   27       time :parse
   28     end
   29 
   30     def read_parse
   31       time :read, :parse
   32     end
   33 
   34     def convert
   35       time :convert
   36     end
   37 
   38     def read_parse_convert
   39       time :read, :parse, :convert
   40     end
   41 
   42     def write
   43       time :write
   44     end
   45 
   46     def total
   47       time :read, :parse, :convert, :write
   48     end
   49 
   50     def print_report to = $stdout, subject = nil
   51       to.puts %(Input file: #{subject}) if subject
   52       to.puts %(  Time to read and parse source: #{'%05.5f' % read_parse.to_f})
   53       to.puts %(  Time to convert document: #{'%05.5f' % convert.to_f})
   54       to.puts %(  Total time (read, parse and convert): #{'%05.5f' % read_parse_convert.to_f})
   55     end
   56 
   57     private
   58 
   59     if (::Process.const_defined? :CLOCK_MONOTONIC, false) && (defined? ::Process.clock_gettime) == 'method'
   60       CLOCK_ID = ::Process::CLOCK_MONOTONIC
   61       def now
   62         ::Process.clock_gettime CLOCK_ID
   63       end
   64     else
   65       def now
   66         ::Time.now
   67       end
   68     end
   69   end
   70 end