Class: PerfCheck::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/perf_check/server.rb

Defined Under Namespace

Classes: ApplicationError, Profile

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeServer

Returns a new instance of Server.



13
14
15
16
17
# File 'lib/perf_check/server.rb', line 13

def initialize
  at_exit do
    exit
  end
end

Class Method Details

.authorization(&block) ⇒ Object



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

def self.authorization(&block)
  define_method(:login, &block)
end

Instance Method Details

#exitObject



70
71
72
73
74
75
76
# File 'lib/perf_check/server.rb', line 70

def exit
  p = pid
  if p
    Process.kill('QUIT', pid)
    sleep(1.5)
  end
end

#hostObject



96
97
98
# File 'lib/perf_check/server.rb', line 96

def host
  "127.0.0.1"
end

#latest_profiler_urlObject



37
38
39
40
41
42
43
44
45
# File 'lib/perf_check/server.rb', line 37

def latest_profiler_url
  mp_timer = Dir["tmp/perf_check/miniprofiler/mp_timers_*"].first
  if "#{mp_timer}" =~ /mp_timers_(\w+)/
    mp_link = "/mini-profiler-resources/results?id=#{$1}"
    FileUtils.mkdir_p('tmp/miniprofiler')
    FileUtils.mv(mp_timer, mp_timer.sub(/^tmp\/perf_check\//, 'tmp/'))
  end
  mp_link
end

#login(login, route) ⇒ Object



19
20
21
# File 'lib/perf_check/server.rb', line 19

def (, route)
  ''
end

#memObject



28
29
30
# File 'lib/perf_check/server.rb', line 28

def mem
  mem = `ps -o rss= -p #{pid}`.strip.to_f / 1024
end

#pidObject



23
24
25
26
# File 'lib/perf_check/server.rb', line 23

def pid
  pidfile = 'tmp/pids/server.pid'
  File.read(pidfile).to_i if File.exists?(pidfile)
end

#portObject



100
101
102
# File 'lib/perf_check/server.rb', line 100

def port
  3031
end

#prepare_to_profileObject



32
33
34
35
# File 'lib/perf_check/server.rb', line 32

def prepare_to_profile
  FileUtils.mkdir_p('tmp/perf_check/miniprofiler')
  Dir["tmp/perf_check/miniprofiler/*"].each{|x| FileUtils.rm(x) }
end

#profileObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/perf_check/server.rb', line 47

def profile
  http = Net::HTTP.new(host, port).tap{ |http| http.read_timeout = 1000 }
  response = nil
  prepare_to_profile

  latency = 1000 * Benchmark.measure do
    http.start
    response = yield(http)
    http.finish
  end.real

  case response.code
  when '200'
    Profile.new.tap do |result|
      result.latency = latency
      result.profile_url = latest_profiler_url
      result.response_body = response.body
    end
  else
    raise Server::ApplicationError.new(response) unless response.code == '200'
  end
end

#restartObject



85
86
87
88
89
90
91
92
93
94
# File 'lib/perf_check/server.rb', line 85

def restart
  if !@running
    $stderr.print "starting rails..."
    start
  else
    $stderr.print "re-starting rails..."
    exit
    start
  end
end

#startObject



78
79
80
81
82
83
# File 'lib/perf_check/server.rb', line 78

def start
  system('rails server -b 127.0.0.1 -d -p 3031 >/dev/null')
  sleep(1.5)

  @running = true
end