diff --git a/Gemfile b/Gemfile index 9a386356f..52c2b21b7 100644 --- a/Gemfile +++ b/Gemfile @@ -46,4 +46,5 @@ end group :development, :test do gem 'rubocop', '~> 0.34.0', require: false + gem 'rugged' end diff --git a/Rakefile b/Rakefile index 58a49598e..fbb4a9333 100644 --- a/Rakefile +++ b/Rakefile @@ -48,3 +48,38 @@ task default: [:test, :rubocop] desc 'CI test task' task :ci => [:default] + +require 'rugged' +require 'benchmark' +Rake::TestTask.new :benchmark_tests do |t| + t.libs << "test" + t.test_files = FileList['test/**/*_benchmark.rb'] + t.ruby_opts = ['-r./test/test_helper.rb'] + t.verbose = true +end + +task :benchmark do + @repo = Rugged::Repository.new('.') + ref = @repo.head + + actual_branch = ref.name + + set_commit('master') + old_bench = Benchmark.realtime { Rake::Task['benchmark_tests'].execute } + + set_commit(actual_branch) + new_bench = Benchmark.realtime { Rake::Task['benchmark_tests'].execute } + + puts 'Results ============================' + puts "------------------------------------~> (Branch) MASTER" + puts old_bench + puts "------------------------------------" + + puts "------------------------------------~> (Actual Branch) #{actual_branch}" + puts new_bench + puts "------------------------------------" +end + +def set_commit(ref) + @repo.checkout ref +end diff --git a/test/benchmark/serialization_benchmark.rb b/test/benchmark/serialization_benchmark.rb new file mode 100644 index 000000000..8fccc44e8 --- /dev/null +++ b/test/benchmark/serialization_benchmark.rb @@ -0,0 +1,50 @@ +require 'test_helper' + +module ActionController + module Serialization + class SerializerTest < ActionController::TestCase + class PostController < ActionController::Base + + def render_with_cache_enable + comment = Comment.new({ id: 1, body: 'ZOMG A COMMENT' }) + author = Author.new(id: 1, name: 'Joao Moura.') + post = Post.new({ id: 1, title: 'New Post', blog:nil, body: 'Body', comments: [comment], author: author }) + + render json: post + end + end + + tests PostController + + def test_render_with_cache_enable + ActionController::Base.cache_store.clear + get :render_with_cache_enable + + expected = { + id: 1, + title: 'New Post', + body: 'Body', + comments: [ + { + id: 1, + body: 'ZOMG A COMMENT' } + ], + blog: { + id: 999, + name: 'Custom blog' + }, + author: { + id: 1, + name: 'Joao Moura.' + } + } + + assert_equal 'application/json', @response.content_type + assert_equal expected.to_json, @response.body + + get :render_with_cache_enable + assert_equal expected.to_json, @response.body + end + end + end +end