Class: StackProf::Remote::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/stackprof/remote/client.rb

Overview

Client wraps the script that uses net/http to make the start/stop requests to a host running the StackProf::Remote::Middleware

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, wait) ⇒ Client

Returns a new instance of Client.



12
13
14
15
16
# File 'lib/stackprof/remote/client.rb', line 12

def initialize(host, wait)
  @host = host
  @wait = (wait || 30).to_i
  check_host
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



10
11
12
# File 'lib/stackprof/remote/client.rb', line 10

def host
  @host
end

Instance Method Details

#enter_consoleObject



60
61
62
# File 'lib/stackprof/remote/client.rb', line 60

def enter_console
  StackProf::CLI.start(result_path)
end

#fetch_resultsObject



41
42
43
44
45
46
47
# File 'lib/stackprof/remote/client.rb', line 41

def fetch_results
  @results = Net::HTTP.get(host, "/__stackprof__/stop")
  puts "[#{host}] Results: #{@results.bytesize / 1024}kb"
  if !@results
    raise "Could not retreive results"
  end
end

#result_pathObject



49
50
51
52
53
# File 'lib/stackprof/remote/client.rb', line 49

def result_path
  result_dir = File.expand_path('~/.sp')
  FileUtils.mkdir_p(result_dir)
  @result_path ||= File.expand_path(File.join(result_dir, "sp-#{@host}-#{Time.now.to_i}.dump"))
end

#runObject



18
19
20
21
22
23
24
# File 'lib/stackprof/remote/client.rb', line 18

def run
  start
  wait
  fetch_results
  save_results
  enter_console
end

#save_resultsObject



55
56
57
58
# File 'lib/stackprof/remote/client.rb', line 55

def save_results
  File.open(result_path, 'wb') {|f| f << @results }
  puts "Saved results to #{result_path}"
end

#startObject



26
27
28
29
30
31
32
33
34
# File 'lib/stackprof/remote/client.rb', line 26

def start
  puts "=== StackProf on #{host} ==="
  puts "Starting"
  result = Net::HTTP.get(host, "/__stackprof__/start")
  puts "[#{host}] #{result}"
  if result !~ /Started/
    raise "Did not start successfully"
  end
end

#waitObject



36
37
38
39
# File 'lib/stackprof/remote/client.rb', line 36

def wait
  puts "Waiting for #{@wait} seconds"
  sleep @wait
end