Skip to content

Commit 952ab04

Browse files
joaomdmourabf4
authored andcommitted
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.
1 parent 31a30c8 commit 952ab04

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,5 @@ end
4545

4646
group :development, :test do
4747
gem 'rubocop', '~> 0.36', require: false
48+
gem 'git'
4849
end

Rakefile

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,38 @@ end
7474

7575
desc 'CI test task'
7676
task :ci => [:default]
77+
78+
require 'git'
79+
require 'benchmark'
80+
Rake::TestTask.new :benchmark_tests do |t|
81+
t.libs << "test"
82+
t.test_files = FileList['test/**/*_benchmark.rb']
83+
t.ruby_opts = ['-r./test/test_helper.rb']
84+
t.verbose = true
85+
end
86+
87+
task :benchmark do
88+
@git = Git.init('.')
89+
ref = @git.current_branch
90+
91+
actual = run_benchmark_spec ref
92+
master = run_benchmark_spec 'master'
93+
94+
@git.checkout(ref)
95+
96+
puts "\n\nResults ============================\n"
97+
puts "------------------------------------~> (Branch) MASTER"
98+
puts master
99+
puts "------------------------------------\n\n"
100+
101+
puts "------------------------------------~> (Actual Branch) #{ref}"
102+
puts actual
103+
puts "------------------------------------"
104+
end
105+
106+
def run_benchmark_spec(ref)
107+
@git.checkout(ref)
108+
response = Benchmark.realtime { Rake::Task['benchmark_tests'].invoke }
109+
Rake::Task['benchmark_tests'].reenable
110+
response
111+
end
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
require 'test_helper'
2+
3+
module ActionController
4+
module Serialization
5+
class SerializerTest < ActionController::TestCase
6+
class PostController < ActionController::Base
7+
8+
def render_with_cache_enable
9+
comment = Comment.new({ id: 1, body: 'ZOMG A COMMENT' })
10+
author = Author.new(id: 1, name: 'Joao Moura.')
11+
post = Post.new({ id: 1, title: 'New Post', blog:nil, body: 'Body', comments: [comment], author: author })
12+
13+
render json: post
14+
end
15+
end
16+
17+
tests PostController
18+
19+
def test_render_with_cache_enable
20+
ActionController::Base.cache_store.clear
21+
get :render_with_cache_enable
22+
23+
expected = {
24+
id: 1,
25+
title: 'New Post',
26+
body: 'Body',
27+
comments: [
28+
{
29+
id: 1,
30+
body: 'ZOMG A COMMENT' }
31+
],
32+
blog: {
33+
id: 999,
34+
name: 'Custom blog'
35+
},
36+
author: {
37+
id: 1,
38+
name: 'Joao Moura.'
39+
}
40+
}
41+
42+
assert_equal 'application/json', @response.content_type
43+
assert_equal expected.to_json, @response.body
44+
45+
get :render_with_cache_enable
46+
assert_equal expected.to_json, @response.body
47+
end
48+
end
49+
end
50+
end

0 commit comments

Comments
 (0)