Skip to content

Commit

Permalink
Experimental memory profiling.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Mar 18, 2020
1 parent 46443dd commit 80f5686
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 1 deletion.
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -8,6 +8,7 @@ gemspec

group :development do
gem 'ruby-prof', platform: :mri
gem 'memory_profiler'
end

group :test do
Expand Down
2 changes: 1 addition & 1 deletion lib/falcon/command/proxy.rb
Expand Up @@ -47,7 +47,7 @@ def container_class
end

def container_options
{}
{count: 1}
end

def call
Expand Down
47 changes: 47 additions & 0 deletions lib/falcon/controller/proxy.rb
Expand Up @@ -88,7 +88,54 @@ def endpoint
)
end

def before_serve
# require 'memory_profiler'
# MemoryProfiler.start
end

def instance_ready(server, parent: Async::Task.current)
# parent.async do |task|
# task.sleep(20)
#
# Async.logger.info(self) {"Preparing memory report..."}
#
# require 'objspace'
# ObjectSpace.trace_object_allocations_start
#
# File.open("heap-#{Process.pid}.dump", 'w') do |file|
# ObjectSpace.dump_all(output: file)
# end
# end
#
# report = MemoryProfiler.stop
# report.pretty_print($stderr)
#
# ObjectSpace.each_object(Async::HTTP::Protocol::HTTP2::Server) do |server|
# Async.logger.info(server) {server.inspect}
# end
#
# clients = server.delegate.clients
# clients.each do |key, client|
# Async.logger.info(self) do |buffer|
# buffer.puts "Client for #{key}: #{client.pool}"
#
# pool = client.pool
# pool.resources.each do |connection, usage|
# buffer.puts "\t#{usage}: #{connection}"
# connection.streams.each do |key, stream|
# buffer.puts "\t\t#{key} -> #{stream}"
# end
# end
# end
# end
# end
end

def start
if GC.respond_to?(:compact)
GC.compact
end

configuration = @command.configuration

services = Services.new(configuration)
Expand Down
10 changes: 10 additions & 0 deletions lib/falcon/controller/serve.rb
Expand Up @@ -68,8 +68,16 @@ def name
"Falcon Server"
end

def before_serve
end

def instance_ready(server)
end

def setup(container)
container.run(name: self.name, restart: true, **@command.container_options) do |instance|
self.before_serve

Async do |task|
# Load one app instance per container:
app = self.load_app
Expand All @@ -94,6 +102,8 @@ def setup(container)

instance.ready!

self.instance_ready(server)

task.children.each(&:wait)
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/falcon/middleware/proxy.rb
Expand Up @@ -64,6 +64,7 @@ def initialize(app, hosts)
end

attr :count
attr :clients

def close
@clients.each_value(&:close)
Expand Down

0 comments on commit 80f5686

Please sign in to comment.