Class: PerfCheck::Server
- Inherits:
-
Object
- Object
- PerfCheck::Server
- Defined in:
- lib/perf_check/server.rb
Defined Under Namespace
Classes: Profile
Instance Attribute Summary collapse
-
#perf_check ⇒ Object
readonly
Returns the value of attribute perf_check.
Class Method Summary collapse
Instance Method Summary collapse
- #environment ⇒ Object
- #exit ⇒ Object
- #host ⇒ Object
-
#initialize(perf_check) ⇒ Server
constructor
A new instance of Server.
- #latest_profiler_url ⇒ Object
- #mem ⇒ Object
- #pid ⇒ Object
- #port ⇒ Object
- #prepare_to_profile ⇒ Object
- #profile ⇒ Object
- #restart ⇒ Object
- #running? ⇒ Boolean
- #start ⇒ Object
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_check ⇒ Object (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
#environment ⇒ Object
151 152 153 |
# File 'lib/perf_check/server.rb', line 151 def environment perf_check..environment || "development" end |
#exit ⇒ Object
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 |
#host ⇒ Object
139 140 141 |
# File 'lib/perf_check/server.rb', line 139 def host "127.0.0.1" end |
#latest_profiler_url ⇒ Object
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 |
#mem ⇒ Object
46 47 48 |
# File 'lib/perf_check/server.rb', line 46 def mem `ps -o rss= -p #{pid}`.strip.to_f / 1024 end |
#pid ⇒ Object
42 43 44 |
# File 'lib/perf_check/server.rb', line 42 def pid File.read(pidfile).to_i if File.exists?(pidfile) end |
#port ⇒ Object
143 144 145 |
# File 'lib/perf_check/server.rb', line 143 def port 3031 end |
#prepare_to_profile ⇒ Object
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 |
#profile ⇒ Object
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 |
#restart ⇒ Object
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
147 148 149 |
# File 'lib/perf_check/server.rb', line 147 def running? @running end |
#start ⇒ Object
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..verify_no_diff perf_check_args['PERF_CHECK_VERIFICATION'] = '1' end unless perf_check..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 |