File: timer.rb

package info (click to toggle)
ruby-cucumber-core 1.5.0-3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 580 kB
  • sloc: ruby: 5,763; makefile: 2
file content (51 lines) | stat: -rw-r--r-- 1,085 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
require 'cucumber/core/test/result'

module Cucumber
  module Core
    module Test
      class Timer
        def start
          @start_time = time_in_nanoseconds
          self
        end

        def duration
          Result::Duration.new(nsec)
        end

        def nsec
          time_in_nanoseconds - @start_time
        end

        def sec
          nsec / 10 ** 9.0
        end

        private

        def time_in_nanoseconds
          MonotonicTime.time_in_nanoseconds
        end

        module MonotonicTime
          module_function

          if defined?(Process::CLOCK_MONOTONIC)
            def time_in_nanoseconds
              Process.clock_gettime(Process::CLOCK_MONOTONIC, :nanosecond)
            end
          elsif (defined?(RUBY_ENGINE) ? RUBY_ENGINE : 'ruby') == 'jruby'
            def time_in_nanoseconds
              java.lang.System.nanoTime()
            end
          else
            def time_in_nanoseconds
              t = Time.now
              t.to_i * 10 ** 9 + t.nsec
            end
          end
        end
      end
    end
  end
end