Class: PerfCheck::Server
- Inherits:
-
Object
- Object
- PerfCheck::Server
- Defined in:
- lib/perf_check/server.rb
Defined Under Namespace
Classes: Profile
Class Method Summary collapse
- .authorization(&block) ⇒ Object
- .authorization_action(method, login_route, &block) ⇒ Object
- .mounted_path(engine) ⇒ Object
- .recognize_path(path, opts = {}) ⇒ Object
- .seed_random! ⇒ Object
- .sign_cookie_data(key, data, opts = {}) ⇒ Object
Instance Method Summary collapse
- #exit ⇒ Object
- #host ⇒ Object
-
#initialize ⇒ Server
constructor
A new instance of Server.
- #latest_profiler_url ⇒ Object
- #login(login, route) ⇒ Object
- #mem ⇒ Object
- #pid ⇒ Object
- #port ⇒ Object
- #prepare_to_profile ⇒ Object
- #profile ⇒ Object
- #restart ⇒ Object
- #start ⇒ Object
Constructor Details
#initialize ⇒ Server
Returns a new instance of Server.
92 93 94 95 96 |
# File 'lib/perf_check/server.rb', line 92 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.(&block) define_method(:login, &block) end |
.authorization_action(method, login_route, &block) ⇒ Object
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 13 def self.(method, login_route, &block) Rails.application.reload_routes! p = PerfCheck::Server.recognize_path(login_route, :method => method) controller = "#{p[:controller]}_controller".classify.constantize action = p[:action] controller.send(:define_method, :get_perf_check_session, &block) controller.send(:define_method, action) do get_perf_check_session(params[:login], params[:route]) render :nothing => true end controller.send(:skip_before_filter, :verify_authenticity_token) do |login, route| http = Net::HTTP.new(host, port) if method == :post response = http.post(login_route, "login=#{login}") elsif method == :get response = http.get(login_route+"?login=#{login}") end response['Set-Cookie'] end end |
.mounted_path(engine) ⇒ Object
38 39 40 41 42 43 |
# File 'lib/perf_check/server.rb', line 38 def self.mounted_path(engine) route = Rails.application.routes.routes.detect do |route| route.app == engine end route && route.path end |
.recognize_path(path, opts = {}) ⇒ Object
45 46 47 48 49 50 51 52 53 |
# File 'lib/perf_check/server.rb', line 45 def self.recognize_path(path, opts={}) Rails::Engine.subclasses.each do |engine| if match = mounted_path(engine) =~ path path = path.sub(match.to_s, '') return engine.routes.recognize_path(path, opts) end end Rails.application.routes.recognize_path(path, opts) end |
.seed_random! ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/perf_check/server.rb', line 55 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 |
.sign_cookie_data(key, data, opts = {}) ⇒ Object
81 82 83 84 85 86 87 88 89 90 |
# File 'lib/perf_check/server.rb', line 81 def self.(key, data, opts={}) opts[:serializer] ||= Marshal secret = Rails.application.config.secret_token marshal = ActiveSupport::MessageVerifier.new(secret, :serializer => opts[:serializer]) marshal_value = marshal.generate(data) "#{key}=#{marshal_value}" end |
Instance Method Details
#exit ⇒ Object
145 146 147 148 149 150 151 |
# File 'lib/perf_check/server.rb', line 145 def exit p = pid if p Process.kill('QUIT', pid) sleep(1.5) end end |
#host ⇒ Object
171 172 173 |
# File 'lib/perf_check/server.rb', line 171 def host "127.0.0.1" end |
#latest_profiler_url ⇒ Object
116 117 118 119 120 121 122 123 124 |
# File 'lib/perf_check/server.rb', line 116 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
98 99 100 |
# File 'lib/perf_check/server.rb', line 98 def login(login, route) '' end |
#mem ⇒ Object
107 108 109 |
# File 'lib/perf_check/server.rb', line 107 def mem mem = `ps -o rss= -p #{pid}`.strip.to_f / 1024 end |
#pid ⇒ Object
102 103 104 105 |
# File 'lib/perf_check/server.rb', line 102 def pid pidfile = 'tmp/pids/server.pid' File.read(pidfile).to_i if File.exists?(pidfile) end |
#port ⇒ Object
175 176 177 |
# File 'lib/perf_check/server.rb', line 175 def port 3031 end |
#prepare_to_profile ⇒ Object
111 112 113 114 |
# File 'lib/perf_check/server.rb', line 111 def prepare_to_profile FileUtils.mkdir_p('tmp/perf_check/miniprofiler') Dir["tmp/perf_check/miniprofiler/*"].each{|x| FileUtils.rm(x) } end |
#profile ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/perf_check/server.rb', line 126 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 Profile.new.tap do |result| result.latency = latency result.profile_url = latest_profiler_url result.response_body = response.body result.response_code = response.code.to_i end end |
#restart ⇒ Object
160 161 162 163 164 165 166 167 168 169 |
# File 'lib/perf_check/server.rb', line 160 def restart if !@running $stderr.print "starting rails...\n" start else $stderr.print "re-starting rails...\n" exit start end end |
#start ⇒ Object
153 154 155 156 157 158 |
# File 'lib/perf_check/server.rb', line 153 def start system('rails server -b 127.0.0.1 -d -p 3031 >/dev/null') sleep(1.5) @running = true end |