active_model_serializers/test/dummy/controllers.rb
Benjamin Fleischer 4cc454d49b Setup benchmarking structure
- Setup dummy app files in `test/dummy`
- Setup dummy test server `bin/serve_dummy
  - Note:  Serializer caching can be completely disabled by passing in
  `CACHE_ON=off bin/serve_dummy start` since Serializer#_cache is only
  set at boot.
- run with
  - ./bin/bench
  - `bin/bench` etc adapted from ruby-bench-suite
  - target files are `test/dummy/bm_*.rb`. Just add another to run it.
  - benchmark cache/no cache
  - remove rake dependency that loads unnecessary files
- remove git gem dependency
  - Running over revisions to be added in subsequent PR
2016-03-09 12:42:25 -06:00

78 lines
2.7 KiB
Ruby

class PostController < ActionController::Base
POST =
begin
if ENV['BENCH_STRESS']
comments = (0..50).map do |i|
Comment.new(id: i, body: 'ZOMG A COMMENT')
end
else
comments = [Comment.new(id: 1, body: 'ZOMG A COMMENT')]
end
author = Author.new(id: 42, name: 'Joao Moura.')
Post.new(id: 1337, title: 'New Post', blog: nil, body: 'Body', comments: comments, author: author)
end
def render_with_caching_serializer
toggle_cache_status
render json: POST, serializer: CachingPostSerializer, adapter: :json, meta: { caching: perform_caching }
end
def render_with_non_caching_serializer
toggle_cache_status
render json: POST, adapter: :json, meta: { caching: perform_caching }
end
def render_cache_status
toggle_cache_status
# Uncomment to debug
# STDERR.puts cache_store.class
# STDERR.puts cache_dependencies
# ActiveSupport::Cache::Store.logger.debug [ActiveModelSerializers.config.cache_store, ActiveModelSerializers.config.perform_caching, CachingPostSerializer._cache, perform_caching, params].inspect
render json: { caching: perform_caching, meta: { cache_log: cache_messages, cache_status: cache_status } }.to_json
end
def clear
ActionController::Base.cache_store.clear
# Test caching is on
# Uncomment to turn on logger; possible performance issue
# logger = DummyLogger.new
# ActiveSupport::Cache::Store.logger = logger # seems to be the best way
#
# the below is used in some rails tests but isn't available/working in all versions, so far as I can tell
# https://github.com/rails/rails/pull/15943
# ActiveSupport::Notifications.subscribe(/^cache_(.*)\.active_support$/) do |*args|
# logger.debug ActiveSupport::Notifications::Event.new(*args)
# end
render json: 'ok'.to_json
end
private
def cache_status
{
controller: perform_caching,
app: Rails.configuration.action_controller.perform_caching,
serializers: Rails.configuration.serializers.each_with_object({}) { |serializer, data| data[serializer.name] = serializer._cache.present? }
}
end
def cache_messages
ActiveSupport::Cache::Store.logger.is_a?(DummyLogger) && ActiveSupport::Cache::Store.logger.messages.split("\n")
end
def toggle_cache_status
case params[:on]
when 'on'.freeze then self.perform_caching = true
when 'off'.freeze then self.perform_caching = false
else nil # no-op
end
end
end
Rails.application.routes.draw do
get '/status(/:on)' => 'post#render_cache_status'
get '/clear' => 'post#clear'
get '/caching(/:on)' => 'post#render_with_caching_serializer'
get '/non_caching(/:on)' => 'post#render_with_non_caching_serializer'
end