Class: Dommy::Performance

Inherits:
Object
  • Object
show all
Defined in:
lib/dommy/performance.rb

Overview

‘window.performance` — User Timing API (mark / measure) plus a virtual `now` clock backed by the deterministic scheduler.

Spec:

- User Timing: https://www.w3.org/TR/user-timing/
- HRT (now):   https://www.w3.org/TR/hr-time/

Instance Method Summary collapse

Constructor Details

#initialize(window) ⇒ Performance

Returns a new instance of Performance.



11
12
13
14
# File 'lib/dommy/performance.rb', line 11

def initialize(window)
  @window = window
  @entries = []
end

Instance Method Details

#__js_call__(method, args) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/dommy/performance.rb', line 96

def __js_call__(method, args)
  case method
  when "now"
    now
  when "mark"
    mark(args[0], args[1])
  when "measure"
    measure(args[0], args[1], args[2])
  when "clearMarks"
    clear_marks(args[0])
  when "clearMeasures"
    clear_measures(args[0])
  when "getEntries"
    get_entries
  when "getEntriesByName"
    get_entries_by_name(args[0], args[1])
  when "getEntriesByType"
    get_entries_by_type(args[0])
  end
end

#__js_get__(key) ⇒ Object



87
88
89
90
91
92
93
94
# File 'lib/dommy/performance.rb', line 87

def __js_get__(key)
  case key
  when "now"
    now
  when "timeOrigin"
    0.0
  end
end

#clear_marks(name = nil) ⇒ Object Also known as: clearMarks



55
56
57
58
# File 'lib/dommy/performance.rb', line 55

def clear_marks(name = nil)
  @entries.reject! { |e| e.entry_type == "mark" && (name.nil? || e.name == name.to_s) }
  nil
end

#clear_measures(name = nil) ⇒ Object Also known as: clearMeasures



62
63
64
65
# File 'lib/dommy/performance.rb', line 62

def clear_measures(name = nil)
  @entries.reject! { |e| e.entry_type == "measure" && (name.nil? || e.name == name.to_s) }
  nil
end

#get_entriesObject Also known as: getEntries



69
70
71
# File 'lib/dommy/performance.rb', line 69

def get_entries
  @entries.dup
end

#get_entries_by_name(name, entry_type = nil) ⇒ Object Also known as: getEntriesByName



75
76
77
# File 'lib/dommy/performance.rb', line 75

def get_entries_by_name(name, entry_type = nil)
  @entries.select { |e| e.name == name.to_s && (entry_type.nil? || e.entry_type == entry_type.to_s) }
end

#get_entries_by_type(entry_type) ⇒ Object Also known as: getEntriesByType



81
82
83
# File 'lib/dommy/performance.rb', line 81

def get_entries_by_type(entry_type)
  @entries.select { |e| e.entry_type == entry_type.to_s }
end

#mark(name, options = nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/dommy/performance.rb', line 20

def mark(name, options = nil)
  start_time = options.is_a?(Hash) && options.key?("startTime") ? options["startTime"].to_f : now
  detail = options.is_a?(Hash) ? options["detail"] : nil
  entry = PerformanceEntry.new(
    name: name.to_s,
    entry_type: "mark",
    start_time: start_time,
    duration: 0.0,
    detail: detail
  )
  @entries << entry
  entry
end

#measure(name, start_or_options = nil, end_mark = nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/dommy/performance.rb', line 34

def measure(name, start_or_options = nil, end_mark = nil)
  if start_or_options.is_a?(Hash)
    start = resolve_time(start_or_options["start"])
    finish = resolve_time(start_or_options["end"])
  else
    start = resolve_time(start_or_options)
    finish = resolve_time(end_mark)
  end

  start ||= 0.0
  finish ||= now
  entry = PerformanceEntry.new(
    name: name.to_s,
    entry_type: "measure",
    start_time: start,
    duration: finish - start
  )
  @entries << entry
  entry
end

#nowObject



16
17
18
# File 'lib/dommy/performance.rb', line 16

def now
  @window.scheduler.now_ms.to_f
end