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
- #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
#exit ⇒ Object
96 97 98 99 100 101 102 |
# File 'lib/perf_check/server.rb', line 96 def exit p = pid if p Process.kill('QUIT', pid) sleep(1.5) end end |
#host ⇒ Object
131 132 133 |
# File 'lib/perf_check/server.rb', line 131 def host "127.0.0.1" end |
#latest_profiler_url ⇒ Object
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/perf_check/server.rb', line 57 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
47 48 49 |
# File 'lib/perf_check/server.rb', line 47 def mem mem = `ps -o rss= -p #{pid}`.strip.to_f / 1024 end |
#pid ⇒ Object
42 43 44 45 |
# File 'lib/perf_check/server.rb', line 42 def pid pidfile = "#{perf_check.app_root}/tmp/pids/server.pid" File.read(pidfile).to_i if File.exists?(pidfile) end |
#port ⇒ Object
135 136 137 |
# File 'lib/perf_check/server.rb', line 135 def port 3031 end |
#prepare_to_profile ⇒ Object
51 52 53 54 55 |
# File 'lib/perf_check/server.rb', line 51 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
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 94 |
# File 'lib/perf_check/server.rb', line 68 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
120 121 122 123 124 125 126 127 128 129 |
# File 'lib/perf_check/server.rb', line 120 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
139 140 141 |
# File 'lib/perf_check/server.rb', line 139 def running? @running end |
#start ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/perf_check/server.rb', line 104 def start ENV['PERF_CHECK'] = '1' if perf_check..verify_no_diff ENV['PERF_CHECK_VERIFICATION'] = '1' end unless perf_check..caching ENV['PERF_CHECK_NOCACHING'] = '1' end app_root = Shellwords.shellescape(perf_check.app_root) system("cd #{app_root} && bundle exec rails server -b 127.0.0.1 -d -p 3031 >/dev/null") sleep(1.5) @running = true end |