Class: RunLoop::Instruments

Inherits:
Object
  • Object
show all
Defined in:
lib/run_loop/instruments.rb

Overview

Note:

All instruments commands are run in the context of ‘xcrun`.

A class for interacting with the instruments command-line tool

Instance Method Summary collapse

Instance Method Details

#instruments_app_running?Boolean

Is the Instruments.app running?

If the Instruments.app is running, the instruments command line tool cannot take control of applications.



52
53
54
55
56
57
58
59
# File 'lib/run_loop/instruments.rb', line 52

def instruments_app_running?
  ps_output = `ps x -o pid,comm | grep Instruments.app | grep -v grep`.strip
  if ps_output[/Instruments\.app/, 0]
    true
  else
    false
  end
end

#instruments_pids(&block) ⇒ Array<Integer>

Note:

The ‘block` parameter is included for legacy API and will be deprecated. Replace your existing calls with with .each or .map. The block argument makes this method hard to mock.

Returns an Array of instruments process ids.



14
15
16
17
18
19
20
21
22
23
# File 'lib/run_loop/instruments.rb', line 14

def instruments_pids(&block)
  pids = pids_from_ps_output
  if block_given?
    pids.each do |pid|
      block.call(pid)
    end
  else
    pids
  end
end

#instruments_running?Boolean

Are there any instruments processes running?



27
28
29
# File 'lib/run_loop/instruments.rb', line 27

def instruments_running?
  instruments_pids.count > 0
end

#kill_instruments(xcode_tools = RunLoop::XCTools.new) ⇒ Object

Send a kill signal to any running ‘instruments` processes.

Only one instruments process can be running at any one time.



37
38
39
40
41
42
43
44
45
46
# File 'lib/run_loop/instruments.rb', line 37

def kill_instruments(xcode_tools = RunLoop::XCTools.new)
  kill_signal = kill_signal xcode_tools
  instruments_pids.each do |pid|
    terminator = RunLoop::ProcessTerminator.new(pid, kill_signal, 'instruments')
    unless terminator.kill_process
      terminator = RunLoop::ProcessTerminator.new(pid, 'KILL', 'instruments')
      terminator.kill_process
    end
  end
end

#spawn(automation_template, options, log_file) ⇒ Integer

TODO:

Do I need to enumerate the launch options in the docs?

TODO:

Should this raise errors?

TODO:

Is this jruby compatible?

Spawn a new instruments process in the context of ‘xcrun` and detach.



71
72
73
74
75
76
77
78
# File 'lib/run_loop/instruments.rb', line 71

def spawn(automation_template, options, log_file)
  splat_args = spawn_arguments(automation_template, options)
  logger = options[:logger]
  RunLoop::Logging.log_debug(logger, "xcrun #{splat_args.join(' ')} >& #{log_file}")
  pid = Process.spawn('xcrun', *splat_args, {:out => log_file, :err => log_file})
  Process.detach(pid)
  pid.to_i
end