Class: PerfCheck::Server

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

Defined Under Namespace

Classes: Profile

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(perf_check) ⇒ Server

Returns a new instance of Server.



38
39
40
# File 'lib/perf_check/server.rb', line 38

def initialize(perf_check)
  @perf_check = perf_check
end

Instance Attribute Details

#perf_checkObject (readonly)

Returns the value of attribute perf_check.



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

def perf_check
  @perf_check
end

Class Method Details

.seed_random!Object



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

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

#environmentObject



151
152
153
# File 'lib/perf_check/server.rb', line 151

def environment
  perf_check.options.environment || "development"
end

#exitObject



95
96
97
98
99
100
101
# File 'lib/perf_check/server.rb', line 95

def exit
  if pid
    Process.kill('KILL', pid)
    sleep(1.5)
    File.delete(pidfile) if File.exist?(pidfile)
  end
end

#hostObject



139
140
141
# File 'lib/perf_check/server.rb', line 139

def host
  "127.0.0.1"
end

#latest_profiler_urlObject



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

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

#memObject



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

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

#pidObject



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

def pid
  File.read(pidfile).to_i if File.exists?(pidfile)
end

#portObject



143
144
145
# File 'lib/perf_check/server.rb', line 143

def port
  3031
end

#prepare_to_profileObject



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

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

#profileObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/perf_check/server.rb', line 67

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
  backtrace_file = response['X-PerfCheck-StackTrace']

  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
    result.server_memory = mem
    if backtrace_file
      result.backtrace = File.read(backtrace_file).lines.map(&:chomp)
    end
  end
rescue Errno::ECONNREFUSED => e
  raise Exception.new("Couldn't connect to the rails server -- it either failed to boot or crashed")
end

#restartObject



128
129
130
131
132
133
134
135
136
137
# File 'lib/perf_check/server.rb', line 128

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

#running?Boolean

Returns:

  • (Boolean)


147
148
149
# File 'lib/perf_check/server.rb', line 147

def running?
  @running
end

#startObject



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/perf_check/server.rb', line 103

def start
  perf_check_args = { 'PERF_CHECK' => '1', 'DISABLE_SPRING' => '1' }
  if perf_check.options.verify_no_diff
    perf_check_args['PERF_CHECK_VERIFICATION'] = '1'
  end
  unless perf_check.options.caching
    perf_check_args['PERF_CHECK_NOCACHING'] = '1'
  end

  app_root = Shellwords.shellescape(perf_check.app_root)
  Bundler.with_original_env do
    Dir.chdir app_root do
      pid = Process.spawn(
        perf_check_args,
        "bundle exec rails server -b #{host} -d -p #{port} -e #{environment}",
        [:out] => '/dev/null'
      )
      Process.wait pid
    end
  end
  sleep(1.5)

  @running = true
end