Class: PerfCheck::Server

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

Defined Under Namespace

Classes: Profile

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeServer

Returns a new instance of Server.



35
36
37
38
39
# File 'lib/perf_check/server.rb', line 35

def initialize
  at_exit do
    exit rescue nil
  end
end

Class Method Details

.seed_random!Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/perf_check/server.rb', line 9

def self.seed_random!
  # Seed random
  srand(1)

  # SecureRandom cannot be seeded, so we have to monkey patch it instead :\
  def SecureRandom.hex(n=16)
    '4' * n
  end

  def SecureRandom.random_bytes(n=16)
    '4' * n
  end

  def SecureRandom.random_number(n=0)
    n > 4 ? 4 : n
  end

  def SecureRandom.urlsafe_base64(n=16, padding=false)
    '4' * (4*n / 3)
  end

  def SecureRandom.uuid
    "00000000-0000-0000-0000-000000000004"
  end
end

Instance Method Details

#exitObject



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

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

#hostObject



120
121
122
# File 'lib/perf_check/server.rb', line 120

def host
  "127.0.0.1"
end

#latest_profiler_urlObject



55
56
57
58
59
60
61
62
63
# File 'lib/perf_check/server.rb', line 55

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

#memObject



46
47
48
# File 'lib/perf_check/server.rb', line 46

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

#pidObject



41
42
43
44
# File 'lib/perf_check/server.rb', line 41

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

#portObject



124
125
126
# File 'lib/perf_check/server.rb', line 124

def port
  3031
end

#prepare_to_profileObject



50
51
52
53
# File 'lib/perf_check/server.rb', line 50

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

#profileObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/perf_check/server.rb', line 65

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

  http.start
  response = yield(http)
  http.finish

  latency = 1000 * response['X-Runtime'].to_f
  query_count = response['X-PerfCheck-Query-Count'].to_i

  Profile.new.tap do |result|
    result.latency = latency
    result.query_count = query_count
    result.profile_url = latest_profiler_url
    result.response_body = response.body
    result.response_code = response.code.to_i
  end
end

#restartObject



109
110
111
112
113
114
115
116
117
118
# File 'lib/perf_check/server.rb', line 109

def restart
  if !@running
    logger.info("starting rails...")
    start
  else
    logger.info("re-starting rails...")
    exit
    start
  end
end

#startObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/perf_check/server.rb', line 94

def start
  ENV['PERF_CHECK'] = '1'
  if PerfCheck.config.verify_responses
    ENV['PERF_CHECK_VERIFICATION'] = '1'
  end
  unless PerfCheck.config.caching
    ENV['PERF_CHECK_NOCACHING'] = '1'
  end

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

  @running = true
end