use puma as the rack handler + optimizations which benefits to the MongoDB adapter (20% better)
did
committed Aug 25, 2015
commit afd95e8da6c57a39619cd6b875390b41e62d84a4
Showing 5
changed files with
55 additions
and 28 deletions
Gemfile
+2
-1
| @@ | @@ -7,7 +7,8 @@ group :development do |
| # gem 'locomotivecms_models', '~> 0.0.1', path: '../models' | |
| # gem 'locomotivecms_models', '0.0.1.pre.alpha' | |
| # gem 'locomotivecms-liquid', path: '/Users/didier/Documents/LocomotiveCMS/gems/liquid' | |
| - | gem 'thin' |
| + | # gem 'thin' |
| + | gem 'puma' |
| # gem 'sprockets-sass', '~> 1.2.0' | |
| gem 'yui-compressor', '~> 0.12.0' | |
Gemfile.lock
+2
-7
| @@ | @@ -70,7 +70,6 @@ GEM |
| term-ansicolor (~> 1.3) | |
| thor (~> 0.19.1) | |
| crass (1.0.2) | |
| - | daemons (1.2.2) |
| diff-lcs (1.2.5) | |
| docile (1.1.5) | |
| domain_name (0.5.24) | |
| @@ | @@ -79,7 +78,6 @@ GEM |
| addressable (~> 2.3) | |
| multi_json (~> 1.0) | |
| rack (>= 1.3.0) | |
| - | eventmachine (1.0.7) |
| execjs (2.5.2) | |
| fast_stack (0.1.0) | |
| rake | |
| @@ | @@ -142,6 +140,7 @@ GEM |
| pry-byebug (3.1.0) | |
| byebug (~> 4.0) | |
| pry (~> 0.10) | |
| + | puma (2.12.3) |
| rack (1.6.1) | |
| rack-cache (1.2) | |
| rack (>= 0.4) | |
| @@ | @@ -201,10 +200,6 @@ GEM |
| stringex (2.5.2) | |
| term-ansicolor (1.3.0) | |
| tins (~> 1.0) | |
| - | thin (1.6.3) |
| - | daemons (~> 1.0, >= 1.0.9) |
| - | eventmachine (~> 1.0) |
| - | rack (~> 1.0) |
| thor (0.19.1) | |
| thread_safe (0.3.5) | |
| tilt (1.4.1) | |
| @@ | @@ -232,11 +227,11 @@ DEPENDENCIES |
| moped (~> 2.0.6) | |
| origin (~> 2.1.1) | |
| pry-byebug (~> 3.1.0) | |
| + | puma |
| rack-mini-profiler | |
| rack-test (~> 0.6.3) | |
| rake (~> 10.4.2) | |
| rspec (~> 3.3.0) | |
| stackprof | |
| - | thin |
| timecop (~> 0.7.4) | |
| yui-compressor (~> 0.12.0) | |
bin/steam.rb
+20
-6
| @@ | @@ -5,7 +5,6 @@ require 'bundler/setup' |
| Bundler.require | |
| - | require 'thin' |
| require 'optparse' | |
| server_options = { address: '0.0.0.0', port: 8080 } | |
| @@ | @@ -85,13 +84,28 @@ end |
| app = Locomotive::Steam::Server.to_app | |
| + | # Thin rack handler |
| # Note: alt thin settings (Threaded) | |
| - | server = Thin::Server.new(server_options[:address], server_options[:port], app) |
| - | server.threaded = true |
| - | server.start |
| + | # require 'thin' |
| + | # server = Thin::Server.new(server_options[:address], server_options[:port], app) |
| + | # server.threaded = true |
| + | # server.start |
| + | # Locomotive::Common::Logger.info 'Server started...' |
| # FIXME: Rack::Handler::Thin.run app (not threaded) | |
| # WEBRick rack handler | |
| # Rack::Handler::WEBrick.run app | |
| - | |
| - | Locomotive::Common::Logger.info 'Server started...' |
| + | # Locomotive::Common::Logger.info 'Server started...' |
| + | |
| + | # Puma rack handler |
| + | require 'puma' |
| + | server = ::Puma::Server.new(app) |
| + | server.add_tcp_listener server_options[:address], server_options[:port] |
| + | server.min_threads = 4 |
| + | server.max_threads = 16 |
| + | begin |
| + | Locomotive::Common::Logger.info 'Server started...' |
| + | server.run.join |
| + | rescue Interrupt |
| + | server.stop(true) |
| + | end |
locomotive/steam/models/entity.rb b/lib/locomotive/steam/models/entity.rb
+9
-8
| @@ | @@ -12,29 +12,30 @@ module Locomotive::Steam |
| end | |
| def method_missing(name, *args, &block) | |
| - | if attributes.include?(name) |
| - | self[name] |
| - | elsif name.to_s.end_with?('=') && attributes.include?(name.to_s.chop) |
| - | self[name.to_s.chop] = args.first |
| + | _name = name.to_s |
| + | if attributes.include?(_name) |
| + | self[_name] |
| + | elsif _name.end_with?('=') && attributes.include?(_name.chop) |
| + | self[_name.chop] = args.first |
| else | |
| super | |
| end | |
| end | |
| def respond_to?(name, include_private = false) | |
| - | attributes.include?(name) || super |
| + | attributes.include?(name.to_s) || super |
| end | |
| def _id | |
| - | self[:_id] |
| + | self['_id'] |
| end | |
| def []=(name, value) | |
| - | attributes[name.to_sym] = value |
| + | attributes[name] = value |
| end | |
| def [](name) | |
| - | attributes[name.to_sym] |
| + | attributes[name] |
| end | |
| def serialize | |
locomotive/steam/models/mapper.rb b/lib/locomotive/steam/models/mapper.rb
+22
-6
| @@ | @@ -19,6 +19,8 @@ module Locomotive::Steam |
| @default_attributes = [] | |
| @associations = [] | |
| + | @entity_map = {} |
| + | |
| instance_eval(&block) if block_given? | |
| end | |
| @@ | @@ -47,15 +49,17 @@ module Locomotive::Steam |
| end | |
| def to_entity(attributes) | |
| - | entity_klass.new(deserialize(attributes)).tap do |entity| |
| - | set_default_attributes(entity) |
| + | cache_entity(entity_klass, attributes) do |
| + | entity_klass.new(deserialize(attributes)).tap do |entity| |
| + | set_default_attributes(entity) |
| - | entity.localized_attributes = @localized_attributes_hash || {} |
| - | entity.associations = {} |
| + | entity.localized_attributes = @localized_attributes_hash || {} |
| + | entity.associations = {} |
| - | attach_entity_to_associations(entity) |
| + | attach_entity_to_associations(entity) |
| - | entity.base_url = @repository.base_url(entity) |
| + | entity.base_url = @repository.base_url(entity) |
| + | end |
| end | |
| end | |
| @@ | @@ -132,6 +136,18 @@ module Locomotive::Steam |
| end | |
| end | |
| + | def cache_entity(entity_klass, attributes, &block) |
| + | return yield if attributes['_id'].blank? |
| + | |
| + | key = "#{entity_klass.to_s}-#{attributes['_id']}" |
| + | |
| + | if (entity = @entity_map[key]).nil? |
| + | entity = @entity_map[key] = yield |
| + | end |
| + | |
| + | entity |
| + | end |
| + | |
| end | |
| end | |