From be29def820b66032594b6e9b03d5643d49641759 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Moura?= Date: Tue, 31 Mar 2015 20:41:50 -0300 Subject: [PATCH 1/2] AMS Benchmark tests #832 Adding a benchmak test structure to help contributors to keep track of how their PR will impact overall performance. It enables developers to create test inside of tests/benchmark. This implementation adds a rake task: ```rake benchmark``` that checkout one commit before, run the test of tests/benchmark, then mover back to the last commit and run it again. By comparing the benchmark results between both commits the contributor will notice if and how much his contribution will impact overall performance. --- .travis.yml | 6 ++- Gemfile | 3 +- Rakefile | 40 ++++++++++++++++-- test/benchmark/serialization_benchmark.rb | 50 +++++++++++++++++++++++ test/serializers/generators_test.rb | 7 ---- test/test_helper.rb | 2 + 6 files changed, 96 insertions(+), 12 deletions(-) create mode 100644 test/benchmark/serialization_benchmark.rb diff --git a/.travis.yml b/.travis.yml index 6ebfd7032..8de1f6b35 100644 --- a/.travis.yml +++ b/.travis.yml @@ -21,6 +21,10 @@ env: - "RAILS_VERSION=master" matrix: + include: + - rvm: jruby-19mode + gemfile: gemfiles/Gemfile.rails-3.2.x + env: "JRUBY_OPTS=-Xcext.enabled=true" allow_failures: - rvm: ruby-head - - env: "RAILS_VERSION=master" + - env: "RAILS_VERSION=master" \ No newline at end of file diff --git a/Gemfile b/Gemfile index baf12948d..2a9dd4e70 100644 --- a/Gemfile +++ b/Gemfile @@ -3,7 +3,8 @@ source 'https://rubygems.org' # Specify your gem's dependencies in active_model_serializers.gemspec gemspec -gem "minitest" +gem 'minitest' +gem 'rugged' version = ENV["RAILS_VERSION"] || "4.2" diff --git a/Rakefile b/Rakefile index 8a1f1e9e8..87560f2e2 100644 --- a/Rakefile +++ b/Rakefile @@ -1,7 +1,10 @@ -require "bundler/gem_tasks" - +require 'bundler/gem_tasks' +require 'rugged' +require 'benchmark' require 'rake/testtask' +task :default => :test + Rake::TestTask.new do |t| t.libs << "test" t.test_files = FileList['test/**/*_test.rb'] @@ -9,4 +12,35 @@ Rake::TestTask.new do |t| t.verbose = true end -task :default => :test +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 \ No newline at end of file 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 diff --git a/test/serializers/generators_test.rb b/test/serializers/generators_test.rb index c40d353e1..b6bdb175b 100644 --- a/test/serializers/generators_test.rb +++ b/test/serializers/generators_test.rb @@ -1,10 +1,3 @@ -class Foo < Rails::Application - if Rails.version.to_s.start_with? '4' - config.eager_load = false - config.secret_key_base = 'abc123' - end -end - Rails.application.load_generators require 'generators/serializer/serializer_generator' diff --git a/test/test_helper.rb b/test/test_helper.rb index a26a1aedf..e57cfa643 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -14,6 +14,8 @@ class Foo < Rails::Application config.action_controller.perform_caching = true config.active_support.test_order = :random ActionController::Base.cache_store = :memory_store + config.eager_load = false + config.secret_key_base = 'abc123' end end From e2ab1192bfbfe61269608129846c2f4ba10c6ed8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Moura?= Date: Mon, 4 May 2015 22:16:16 -0300 Subject: [PATCH 2/2] replacing Rugged dependency per Git gem --- .travis.yml | 15 +++++++-------- Gemfile | 2 +- Rakefile | 32 ++++++++++++++++---------------- 3 files changed, 24 insertions(+), 25 deletions(-) diff --git a/.travis.yml b/.travis.yml index 8de1f6b35..c10a97e75 100644 --- a/.travis.yml +++ b/.travis.yml @@ -15,16 +15,15 @@ install: - bundle install --retry=3 env: - - "RAILS_VERSION=4.0" - - "RAILS_VERSION=4.1" - - "RAILS_VERSION=4.2" - - "RAILS_VERSION=master" + global: + - JRUBY_OPTS=-Xcext.enabled=true + matrix: + - "RAILS_VERSION=4.0" + - "RAILS_VERSION=4.1" + - "RAILS_VERSION=4.2" + - "RAILS_VERSION=master" matrix: - include: - - rvm: jruby-19mode - gemfile: gemfiles/Gemfile.rails-3.2.x - env: "JRUBY_OPTS=-Xcext.enabled=true" allow_failures: - rvm: ruby-head - env: "RAILS_VERSION=master" \ No newline at end of file diff --git a/Gemfile b/Gemfile index 2a9dd4e70..b4d0083f6 100644 --- a/Gemfile +++ b/Gemfile @@ -4,7 +4,7 @@ source 'https://rubygems.org' gemspec gem 'minitest' -gem 'rugged' +gem 'git' version = ENV["RAILS_VERSION"] || "4.2" diff --git a/Rakefile b/Rakefile index 87560f2e2..de05f9f18 100644 --- a/Rakefile +++ b/Rakefile @@ -1,5 +1,5 @@ require 'bundler/gem_tasks' -require 'rugged' +require 'git' require 'benchmark' require 'rake/testtask' @@ -20,27 +20,27 @@ Rake::TestTask.new :benchmark_tests do |t| end task :benchmark do - @repo = Rugged::Repository.new('.') - ref = @repo.head + @git = Git.init('.') + ref = @git.current_branch - actual_branch = ref.name + actual = run_benchmark_spec ref + master = run_benchmark_spec 'master' - set_commit('master') - old_bench = Benchmark.realtime { Rake::Task['benchmark_tests'].execute } + @git.checkout(ref) - set_commit(actual_branch) - new_bench = Benchmark.realtime { Rake::Task['benchmark_tests'].execute } - - puts 'Results ============================' + puts "\n\nResults ============================\n" puts "------------------------------------~> (Branch) MASTER" - puts old_bench - puts "------------------------------------" + puts master + puts "------------------------------------\n\n" - puts "------------------------------------~> (Actual Branch) #{actual_branch}" - puts new_bench + puts "------------------------------------~> (Actual Branch) #{ref}" + puts actual puts "------------------------------------" end -def set_commit(ref) - @repo.checkout ref +def run_benchmark_spec(ref) + @git.checkout(ref) + response = Benchmark.realtime { Rake::Task['benchmark_tests'].invoke } + Rake::Task['benchmark_tests'].reenable + response end \ No newline at end of file