Class: Chronometer

Inherits:
Object
  • Object
show all
Defined in:
lib/chronometer.rb,
lib/chronometer/dsl.rb,
lib/chronometer/event.rb,
lib/chronometer/command.rb,
lib/chronometer/version.rb,
lib/chronometer/trace_event.rb

Defined Under Namespace

Modules: Command

Constant Summary collapse

VERSION =
File.read(File.expand_path('../../VERSION', __dir__)).strip.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&blk) ⇒ Chronometer

Returns a new instance of Chronometer.



17
18
19
20
21
22
23
24
# File 'lib/chronometer.rb', line 17

def initialize(&blk)
  dsl = DSL.new
  dsl.instance_exec(&blk)
  @events = dsl.events
  @tracepoints = dsl.tracepoints
  @trace_event_queue = Queue.new
  @trace_events = []
end

Instance Attribute Details

#trace_eventsObject (readonly)

Returns the value of attribute trace_events.



9
10
11
# File 'lib/chronometer.rb', line 9

def trace_events
  @trace_events
end

Class Method Details

.from_file(path, contents: File.read(path)) ⇒ Object



11
12
13
14
15
# File 'lib/chronometer.rb', line 11

def self.from_file(path, contents: File.read(path))
  new do
    instance_eval(contents, path)
  end
end

.timestamp_usObject



67
68
69
# File 'lib/chronometer.rb', line 67

def self.timestamp_us
  Time.now.utc.to_f.*(1_000_000).round
end

Instance Method Details

#associate_sub_slices!Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/chronometer.rb', line 37

def associate_sub_slices!
  parents = []
  @trace_events.each do |event|
    case event.event_type
    when :B
      parents << event
    when :E
      parent = parents.pop
      event.sub_slices.replace parent.sub_slices if parent
      parents.last.sub_slices << event unless parents.empty?
    end
  end
end

#drain!Object



31
32
33
34
35
# File 'lib/chronometer.rb', line 31

def drain!
  loop { @trace_events << @trace_event_queue.pop(true) }
rescue ThreadError
  nil
end

#install!Object



26
27
28
29
# File 'lib/chronometer.rb', line 26

def install!
  @events.each { |e| install_method_hook(e) }
  @tracepoints.each { |tp| install_tracepoint(tp) }
end

Raises:

  • (ArgumentError)


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/chronometer.rb', line 51

def print_trace_event_report(dest, metadata: {})
  raise ArgumentError, 'cannot manually specify :traceEvents' if .key?(:traceEvents)
  require 'json'
  File.open(dest, 'w') do |f|
    f << JSON.generate()
    f.seek(-1, :CUR) # remove closing }
    f << ',' unless .empty?
    f << '"traceEvents":['
    @trace_events.each_with_index do |te, i|
      f << ',' unless i == 0
      f << JSON.generate(te.to_h)
    end
    f << ']}'
  end
end

#register_trace_event(event) ⇒ Object



71
72
73
# File 'lib/chronometer.rb', line 71

def register_trace_event(event)
  @trace_event_queue << event
end