Skip to content

Commit

Permalink
AMS Benchmark tests rails-api#832
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
joaomdmoura committed Apr 21, 2015
1 parent 820db0b commit 8a0ec1a
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 11 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ env:
- "RAILS_VERSION=4.0"
- "RAILS_VERSION=4.1"
- "RAILS_VERSION=master"
- "JRUBY_OPTS=-Xcext.enabled=true"

matrix:
allow_failures:
Expand Down
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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.1"

Expand Down
40 changes: 37 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,46 @@
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']
t.ruby_opts = ['-r./test/test_helper.rb']
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
50 changes: 50 additions & 0 deletions test/benchmark/serialization_benchmark.rb
Original file line number Diff line number Diff line change
@@ -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
7 changes: 0 additions & 7 deletions test/serializers/generators_test.rb
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
2 changes: 2 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class Foo < Rails::Application
if Rails.version.to_s.start_with? '4'
config.action_controller.perform_caching = true
ActionController::Base.cache_store = :memory_store
config.eager_load = false
config.secret_key_base = 'abc123'
end
end

Expand Down

0 comments on commit 8a0ec1a

Please sign in to comment.